NODES 행으로 행을 생성하는 코드를 작성했으며 각 행에는 사용자가 지정한 열 수가 있습니다 (아래 node_degree 참조). 그런 다음 행렬의 각 항목을 값으로 채 웁니다. 값은 중요하지 않습니다.행렬에 대한 메모리 할당
Geany를 사용하여 Fedora에서 프로그램을 빌드 할 때 오류 메시지가 표시되지 않습니다. 내가 프로그램을 실행할 때, 내가 얻을 :
./geany 실행 script.sh 행 5 : 3586 분할 오류 (덤프 핵심 ) "./ ad_matrix_outside_main
참고 : ad_matrix_outside_main는 이름입니다 여기
프로그램 인 C 파일의 :.#include <stdio.h>
#include <stdlib.h>
#define NODES (10)
#define CONNECTED (5)
int main()
{
int i, j;
double **matrix;
matrix = (double **)malloc(sizeof(double *)*NODES);
int node_degree[CONNECTED]; //Example: 5 nodes are connected (have non-zero degree).
for(i=0; i<CONNECTED; i++)
{
printf("Enter degree of node %d\n", i); //Index is node, value is degree.
scanf("%d", &node_degree[i]);
}
for(i=0; i<CONNECTED; i++)
{
matrix[i] = (double*)malloc(sizeof(double)* (node_degree[i] + 1));
}
for(i=0; i<NODES; i++)
{
for(j=0; j<node_degree[i]; j++)
{
matrix[i][j] = j; //j can be the node that node i connected to.
}
matrix[i][j] = NODES;
}
for(i=0; i<NODES; i++)
{
free(matrix[i]);
}
free(matrix);
return(0);
}
오류의 원인이 될 수있는 어떤 나는 내 표기법을 느끼고있다? 문제 일 수 있습니다.
, 당신은 -g'의 gcc 옵션'사용하여 코드를 컴파일 한 후 bash는 명령'ulimit를 -c unlimited'를 사용하여 코어 덤프를 활성화하고 바이너리를 실행해야 당신이하고자 있도록 코어 덤프를 얻으십시오. 그러면 gdb와'backtrace'를 사용하여 충돌의 실제 이유를 알 수 있습니다. – VoidPointer