将自定义结构作为参数传递导致分段错误
Passing custom structure as argument causing segmentation fault
正在为 CS class 项目实现图表我声明了这两个结构
typedef struct person {
char name[128];
int age;
} Person;
typedef struct graph {
int knots;
int adjacencies[500][500];
Person personList[500];
} Graph;
void insertPerson(Graph *g, Person p) {
g->personList[knots] = p;
(g->knots)++;
}
void writeAdjacencies(Graph g) {
for(int i = 0; i < MAXDIM; i++) {
for(int j = 0; j < MAXDIM; j++) {
printf("%d ", g.adjacencies[i][j]);
}
printf("\n");
}
}
一切都很好,除了在尝试创建菜单功能(读取输入然后决定是添加新人还是删除现有人等)时传递图形指针然后使用它的指向实例似乎正在生成分段错误。
void checkInput(int input, Graph *g) {
int temp;
char tempName[128];
if(input == 1 ) {
printf("\n Name: ");
scanf(" %[^\n]", tempName);
printf("%s\n", tempName);
Person p;
strcpy(p.name, tempName);
/* takes graph pointer a person and just adds person to personList and increases knots
it's working as intended
*/
insertPerson(g, p);
/* Goes through each element in the matrix and prints it
this is the one causing problems
*/
writeAdjacencies(*g);
}
}
代码工作正常,直到我将 *g
作为参数传递 - 也就是说,如果我将 writeAdjacencies(*g)
放在注释下,它不会造成任何麻烦。
我不明白这是什么问题?我感觉好像我在使用指针,就像我在这一年中所做的那样,并且它起作用了。也许我可以传递一个空指针?但是我认为我确实在主函数中初始化了它。
main.c :
Graph graph;
for(int i = 0; i < MAXDIM; i++) {
for(int j = 0; j < MAXDIM; j++) {
g.adjacencies[i][j] = 0;
}
}
g.knots = 0;
checkInput(0, &g);
欢迎任何帮助!
编辑:包括两个缺失的函数
忽略您的 scanf
代码中潜在的缓冲区溢出,您的 Graph
结构对于堆栈来说太大了(包含 250,000 个整数)。请改用动态内存分配。例如:
typedef struct {
int** adjacencies;
int n;
} Graph;
void graph_init(Graph* g, int n) {
g->n = n;
g->adjacencies = malloc(n * sizeof(int*));
for (int i = 0; i < n; ++i) {
g->adjacencies[i] = malloc(n * sizeof(int));
}
}
void graph_free(Graph* g) {
for (int i = 0; i < g->n; ++i) {
free(g->adjacencies[i]);
}
free(g->adjacencies);
g->n = 0;
}
正在为 CS class 项目实现图表我声明了这两个结构
typedef struct person {
char name[128];
int age;
} Person;
typedef struct graph {
int knots;
int adjacencies[500][500];
Person personList[500];
} Graph;
void insertPerson(Graph *g, Person p) {
g->personList[knots] = p;
(g->knots)++;
}
void writeAdjacencies(Graph g) {
for(int i = 0; i < MAXDIM; i++) {
for(int j = 0; j < MAXDIM; j++) {
printf("%d ", g.adjacencies[i][j]);
}
printf("\n");
}
}
一切都很好,除了在尝试创建菜单功能(读取输入然后决定是添加新人还是删除现有人等)时传递图形指针然后使用它的指向实例似乎正在生成分段错误。
void checkInput(int input, Graph *g) {
int temp;
char tempName[128];
if(input == 1 ) {
printf("\n Name: ");
scanf(" %[^\n]", tempName);
printf("%s\n", tempName);
Person p;
strcpy(p.name, tempName);
/* takes graph pointer a person and just adds person to personList and increases knots
it's working as intended
*/
insertPerson(g, p);
/* Goes through each element in the matrix and prints it
this is the one causing problems
*/
writeAdjacencies(*g);
}
}
代码工作正常,直到我将 *g
作为参数传递 - 也就是说,如果我将 writeAdjacencies(*g)
放在注释下,它不会造成任何麻烦。
我不明白这是什么问题?我感觉好像我在使用指针,就像我在这一年中所做的那样,并且它起作用了。也许我可以传递一个空指针?但是我认为我确实在主函数中初始化了它。
main.c :
Graph graph;
for(int i = 0; i < MAXDIM; i++) {
for(int j = 0; j < MAXDIM; j++) {
g.adjacencies[i][j] = 0;
}
}
g.knots = 0;
checkInput(0, &g);
欢迎任何帮助!
编辑:包括两个缺失的函数
忽略您的 scanf
代码中潜在的缓冲区溢出,您的 Graph
结构对于堆栈来说太大了(包含 250,000 个整数)。请改用动态内存分配。例如:
typedef struct {
int** adjacencies;
int n;
} Graph;
void graph_init(Graph* g, int n) {
g->n = n;
g->adjacencies = malloc(n * sizeof(int*));
for (int i = 0; i < n; ++i) {
g->adjacencies[i] = malloc(n * sizeof(int));
}
}
void graph_free(Graph* g) {
for (int i = 0; i < g->n; ++i) {
free(g->adjacencies[i]);
}
free(g->adjacencies);
g->n = 0;
}