简体   繁体   English

“被释放的指针未分配”是什么意思?

[英]What does “pointer being freed was not allocated” mean exactly?

I have trouble with a program where im trying to copy a string to a structs string variable in c. 我有一个程序的问题,我试图将字符串复制到c中的结构字符串变量。 After copying the string im trying to free the temporary string variable which is copied to the structs string variable. 复制字符串后我试图释放临时字符串变量,该变量被复制到structs字符串变量。 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. 我不明白到底发生了什么。

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

Only pointers returned by calls to malloc() , realloc() or calloc() can be passed to free() (dynamically allocated memory on the heap). 只有通过调用malloc()realloc()calloc()返回的指针才能传递给free() (堆上动态分配的内存)。 From section 7.20.3.2 The free function of C99 standard: 从第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. free函数导致ptr指向的空间被释放,即可用于进一步分配。 If ptr is a null pointer, no action occurs. 如果ptr是空指针,则不执行任何操作。 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. 否则,如果参数与之前由calloc,malloc或realloc函数返回的指针不匹配,或者如果通过调用free或realloc释放了空间,则行为未定义。

In the posted code, str is 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。

Be careful. 小心。 This code shows confusion about two things: 这段代码显示了两件事的混乱:

  1. The difference between stack and heap memory 堆栈和堆内存之间的区别
  2. The operation of strcpy strcpy的操作

Point 1 第1点

This has already been answered in a way, but I'll expand a little: 这已经在某种程度上得到了回答,但我会稍微扩展一下:

The heap is where dynamic memory is given to your process. 是为您的进程提供动态内存的地方。 When you call malloc (and related functions), memory is returned on the heap. 当您调用malloc (和相关函数)时,将在堆上返回内存。 You must free this memory when you are done with it. 完成后,您必须free此内存。

The stack is 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 str variable is an example of something that is on the stack. 你的str变量是堆栈中某个东西的一个例子。

Point 2 第2点

I'd like to know what that c member of your matrix array is. 我想知道你的矩阵阵列的c成员是什么。 If it is a pointer, then you might be confused about what strcpy does. 如果它是一个指针,那么你可能会对strcpy作用感到困惑。 The function only copies bytes of a string from one part of memory to another. 该函数仅将字符串的字节从内存的一部分复制到另一部分。 So the memory has to be available. 所以内存必须可用。

If c is a char array (with sufficient number of elements to hold the string), this is okay. 如果c是一个char数组(有足够数量的元素来保存字符串),这没关系。 But if c is a pointer, you must have allocated memory for it already if you want to use strcpy . 但是如果c是一个指针,如果你想使用strcpy ,你必须已经为它分配了内存。 There is an alternative function strdup which allocates enough memory for a string, copies it, and returns a pointer. 还有一个替代函数strdup ,它为字符串分配足够的内存,复制它并返回一个指针。 You are responsible for free ing that pointer when you no longer need it. 当您不再需要时,您负责free该指针。

It used free without using malloc . 它使用free而不使用malloc You can't release memory that hasn't been allocated. 您无法释放尚未分配的内存。

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() . 使用malloc()realloc()等函数分配内存,然后使用free()释放。 Since this memory was not allocated that way, passing the address to free() results in the error. 由于此内存未以这种方式分配,因此将地址传递给free()导致错误。

You did not use malloc() to allocate space in the heap for str . 您没有使用malloc()在堆中为str分配空间。 Therefore, str is allocated on the stack and cannot be deallocated with free() , but will be deallocated when str goes out of scope, ie after the function containing the declaration returns. 因此, str在堆栈上分配,不能用free()解除分配,但是当str超出范围时,即在包含声明的函数返回之后,将释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM