This code snippet works fine on its own , but once I integrate it to my complete main()
function, I get issues with free(0
ing, sometimes failing at the first iteration, sometimes failing at a random I and sometimes when I try to free the int
pointer pointer.
这个代码片段本身可以正常工作,但是一旦我将它集成到我的完整main()函数中,我就会遇到免费问题(0,有时在第一次迭代时失败,有时会失败,有时我会尝试释放int指针指针。
int main()
{
int** GPRIListedCoords = calloc(sizeof(int*), GPRIMaxVal);
int i;
for(i = 0; i < GPRIMaxVal; i++)
GPRIListedCoords[i] = calloc(sizeof(int) , 2);
for( i = 0; i < GPRIMaxVal; i++ )
free(GPRIListedCoords[i]);
free(GPRIListedCoords);
return 0;
}
I create / free the char pointers ( to pointers ) in the same way with no issues.
我以相同的方式创建/释放char指针(指针),没有任何问题。
I tried catching any null pointers, but I'm not getting any.
我尝试捕获任何空指针,但我没有得到任何。
GDB Stack info: http://i.snag.gy/Z3ZqV.jpg
GDB Stack信息:http://i.snag.gy/Z3ZqV.jpg
2 个解决方案
#1
This code sample looks fine. I think the problem is in the other case, where you do something with the allocated pointers. as per the man page of free()
(emphasis mine)
此代码示例看起来很好。我认为问题出在另一种情况下,你用分配的指针做一些事情。根据free()的手册页(强调我的)
void free(void *ptr);
void free(void * ptr);
The
free()
function frees the memory space pointed to byptr
, which must have been returned by a previous call tomalloc()
,calloc()
orrealloc()
. Otherwise, or iffree(ptr)
has already been called before, undefined behavior occurs. If ptr isNULL
, no operation is performed.free()函数释放ptr指向的内存空间,该内存空间必须由之前调用malloc(),calloc()或realloc()返回。否则,或者如果之前已经调用了free(ptr),则会发生未定义的行为。如果ptr为NULL,则不执行任何操作。
So, in that case, if the calloc()
-ed pointer is changed, you'll face undefined behaviour.
因此,在这种情况下,如果更改了calloc() - ed指针,您将面临未定义的行为。
You can use a debugger (like, gdb
on linux) to inspect the allocated memory locations and see if the pointer is changed or not.
您可以使用调试器(例如,linux上的gdb)来检查分配的内存位置,并查看指针是否已更改。
#2
This code looks fine. However, if any part of your code is invoking undefined behaviour, then it could be damaging the internal malloc data structures, which is then causing a crash here.
这段代码看起来不错。但是,如果代码的任何部分正在调用未定义的行为,那么它可能会破坏内部malloc数据结构,从而导致崩溃。
Can you run your program on Linux? I find using valgrind (a Linux tool) can often shake out such bugs.
你可以在Linux上运行你的程序吗?我发现使用valgrind(Linux工具)可以经常摆脱这样的错误。
#1
This code sample looks fine. I think the problem is in the other case, where you do something with the allocated pointers. as per the man page of free()
(emphasis mine)
此代码示例看起来很好。我认为问题出在另一种情况下,你用分配的指针做一些事情。根据free()的手册页(强调我的)
void free(void *ptr);
void free(void * ptr);
The
free()
function frees the memory space pointed to byptr
, which must have been returned by a previous call tomalloc()
,calloc()
orrealloc()
. Otherwise, or iffree(ptr)
has already been called before, undefined behavior occurs. If ptr isNULL
, no operation is performed.free()函数释放ptr指向的内存空间,该内存空间必须由之前调用malloc(),calloc()或realloc()返回。否则,或者如果之前已经调用了free(ptr),则会发生未定义的行为。如果ptr为NULL,则不执行任何操作。
So, in that case, if the calloc()
-ed pointer is changed, you'll face undefined behaviour.
因此,在这种情况下,如果更改了calloc() - ed指针,您将面临未定义的行为。
You can use a debugger (like, gdb
on linux) to inspect the allocated memory locations and see if the pointer is changed or not.
您可以使用调试器(例如,linux上的gdb)来检查分配的内存位置,并查看指针是否已更改。
#2
This code looks fine. However, if any part of your code is invoking undefined behaviour, then it could be damaging the internal malloc data structures, which is then causing a crash here.
这段代码看起来不错。但是,如果代码的任何部分正在调用未定义的行为,那么它可能会破坏内部malloc数据结构,从而导致崩溃。
Can you run your program on Linux? I find using valgrind (a Linux tool) can often shake out such bugs.
你可以在Linux上运行你的程序吗?我发现使用valgrind(Linux工具)可以经常摆脱这样的错误。