自己编写的程序,并且我已经建立这种结构波段故障使用malloc()用C
struct position_found
{
int row;
int column;
struct position_found *next;
};
typedef struct position_found position_found, *position_found_ptr;
,然后我使用该函数来创建一个新的节点类型position_found
position_found_ptr new_position_found_node(int row, int column)
{
position_found_ptr x;
x=(position_found_ptr)malloc(sizeof(position_found));
if(x==NULL)
{
printf("out of memory");
exit(2);
}
x->row=row;
x->column=column;
x->next=NULL;
return x;
}
的问题在于x =(position_found_ptr)malloc(sizeof(position_found));提供seg错误,但是如果我在此之前打印某些东西,例如printf(“k”),malloc将正常工作,并且我的程序将继续。我试过在测试程序中使用它自己的功能,它完美地工作。你有什么想法发生了什么?
是的,您在程序中调用了未定义的行为。 –
你有没有包含stdlib.h? –
通过'valgrind'或其他内存调试器运行你的代码。它应该可以帮助你追踪真正的问题。 – FatalError