我使用这样的东西来分配内存与一个函数(在C中)
void myfunction(struct mystruct** ss) {
// some code
*ss = malloc( 1024 * sizeof (struct mystruct) );
// some code
}
int main()
{
struct mystruct **x;
*x = NULL;
myfunction(x);
return 0;
}
但是我有隔离错误。这段代码有什么问题?
在struct mystruct **x;
之后,变量x
未初始化。像你的程序在*x = NULL;
中那样从它中读取是非法的。
你可能想写:
int main()
{
struct mystruct *x;
x = NULL;
myfunction(&x);
return 0;
}
但是不可能确定,因为您的程序没有做任何有意义的事情。注意,x = NULL;
是不必要的:x
将在myfunction()
内部初始化。
你永远不会为底层指针做任何存储,有**
和对象的存储,但没有*
…
struct mystruct **x,*y;
x = &y;
myfunction(x);
return 0;