C语言 “被释放的指针未分配”究竟是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13148119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "pointer being freed was not allocated" mean exactly?
提问by patriques
I have trouble with a program where im trying to copy a string to a structs string variable in c. After copying the string im trying to free the temporary string variable which is copied to the structs string variable. But when i try to free the string the program turns on me and says that "pointer being freed was not allocated". I don't understand exactly what is going on.
我在尝试将字符串复制到 c 中的结构字符串变量的程序中遇到问题。复制字符串后,我试图释放复制到结构字符串变量的临时字符串变量。但是当我尝试释放字符串时,程序打开我并说“没有分配被释放的指针”。我不明白到底发生了什么。
char str[]=" "; //temporary string to copy to structs string
str[3]=s; //putting a char s in middle
strcpy(matrix[i-1][j].c, str); //copying the string
free(str); //freeing str that now is useless when copied
回答by hmjd
Only pointers returned by calls to malloc(), realloc()or calloc()can be passed to free()(dynamically allocated memory on the heap). From section 7.20.3.2 The free functionof C99 standard:
只有通过调用返回的指针malloc(),realloc()或calloc()可以传递给free()(堆上动态分配的内存)。来自第7.20.3.2节C99 标准的免费功能:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
free 函数导致 ptr 指向的空间被释放,即可供进一步分配。如果 ptr 是空指针,则不会发生任何操作。否则,如果参数与先前由 calloc、malloc 或 realloc 函数返回的指针不匹配,或者如果空间已通过调用 free 或 realloc 释放,则行为未定义。
In the posted code, stris not dynamically allocated but is allocated on the stack and is automatically released when it goes out of scope and does not need to be free()d.
贴出的代码中,str不是动态分配而是在栈上分配,超出范围自动释放,不需要free()d。
回答by paddy
Be careful. This code shows confusion about two things:
当心。这段代码显示了对两件事的混淆:
- The difference between stack and heap memory
- The operation of
strcpy
- 栈内存和堆内存的区别
- 的操作
strcpy
Point 1
第 1 点
This has already been answered in a way, but I'll expand a little:
这已经以某种方式得到了回答,但我会扩展一点:
The heapis where dynamic memory is given to your process. When you call malloc(and related functions), memory is returned on the heap. You must freethis memory when you are done with it.
该堆就是动态内存是给你的进程。当您调用malloc(和相关函数)时,内存在堆上返回。free当你完成它时,你必须拥有这个记忆。
The stackis part of your process's running state. It is where ordinary variables are stored. When you call a function, its variables are pushed onto the stack and popped back off automatically when the function exits. Your strvariable is an example of something that is on the stack.
该堆栈是你的进程运行状态的一部分。它是存储普通变量的地方。当你调用一个函数时,它的变量被压入堆栈,并在函数退出时自动弹出。您的str变量是堆栈上的一个例子。
Point 2
第 2 点
I'd like to know what that cmember of your matrix array is. If it is a pointer, then you might be confused about what strcpydoes. The function only copies bytes of a string from one part of memory to another. So the memory has to be available.
我想知道c矩阵数组的那个成员是什么。如果它是一个指针,那么您可能会对它的作用感到困惑strcpy。该函数仅将字符串的字节从内存的一部分复制到另一部分。所以内存必须可用。
If cis a chararray (with sufficient number of elements to hold the string), this is okay. But if cis a pointer, you musthave allocated memory for it already if you want to use strcpy. There is an alternative function strdupwhich allocates enough memory for a string, copies it, and returns a pointer. You are responsible for freeing that pointer when you no longer need it.
如果c是一个char数组(有足够数量的元素来保存字符串),这是可以的。但是,如果c是一个指针,你必须已如果你想使用分配的内存吧strcpy。有一个替代函数strdup可以为字符串分配足够的内存,复制它并返回一个指针。free当您不再需要它时,您有责任使用该指针。
回答by paddy
It used freewithout using malloc. You can't release memory that hasn't been allocated.
它使用free而不使用malloc. 您无法释放尚未分配的内存。
回答by Jonathan Wood
Your code doesn't allocate any memory for the string. It simply copies a string from one string to the memory used by another (a string of spaces).
您的代码不会为字符串分配任何内存。它只是将一个字符串从一个字符串复制到另一个字符串(空格字符串)使用的内存中。
Memory is allocated using functions like malloc(), realloc(), etc. and then freed with free(). Since this memory was not allocated that way, passing the address to free()results in the error.
存储器是使用类似功能分配malloc(),realloc()等,然后与释放free()。由于此内存不是以这种方式分配的,因此将地址传递给会free()导致错误。
回答by ctype.h
You did not use malloc()to allocate space in the heap for str. Therefore, stris allocated on the stack and cannot be deallocated with free(), but will be deallocated when strgoes out of scope, i.e. after the function containing the declaration returns.
您没有使用malloc()在堆中为 分配空间str。因此,str在堆栈上分配并且不能被释放free(),但是当str超出范围时会被释放,即在包含声明的函数返回之后。

