[英]C struct pointer Seg.fault when malloc()
I'm trying to implement the ringbuffer from this post https://stackoverflow.com/a/827749 and the only code I've added is a main that looks like this. 我正在尝试从这篇文章https://stackoverflow.com/a/827749实现环形缓冲区,我添加的唯一代码是一个看起来像这样的主体。
int main(int argc, char** argv) {
circular_buffer *my_buff;
cb_init(my_buff, 16, sizeof(char));
return (EXIT_SUCCESS);
}
I get a SIGSEV (Segmentation fault) error as soon as I try to run this code though. 尝试运行此代码后,我立即收到SIGSEV(细分错误)错误。 By the looks of it it happens on the first row in cb_init() where malloc() is called.
从外观上看,它发生在cb_init()的第一行中,其中malloc()被调用。
You need to allocate memory for my_buff
您需要为
my_buff
分配内存
At the moment you're passing an uninitialised pointer into cb_init
which is then dereferenced. 目前,您正在将未初始化的指针传递到
cb_init
,然后将其取消引用。
But I'm sure you must have realised this because I'm sure you will have tried running the code in a debugger... 但是我确定您一定已经意识到这一点,因为我确定您将尝试在调试器中运行代码...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.