简体   繁体   English

是否应该将指针传递给free()指向已分配空间的开头?

[英]Should the pointer being passed to free() point to the start of the allocated space?

Say I used malloc to request a free space. 假设我使用malloc请求一个可用空间。 The variable ptr points to the created memory space. 变量ptr指向创建的内存空间。 In my code the value of ptr is changed to access different locations in my allocated space. 在我的代码中, ptr的值被更改为访问我分配的空间中的不同位置。

Calling free(ptr) , after ptr has been changed, would not be correct, no?. ptr改变之后调用free(ptr)是不正确的,不是吗? What I should do instead is to create a replica of ptr after malloc . 我应该做的是在malloc之后创建一个ptr的副本。 This new pointer should be used instead in the free function call. 应该在free函数调用中使用这个新指针。 Is that right? 是对的吗?

Cheers 干杯

Calling free(ptr) , after ptr has been changed, would not be correct, no? ptr改变之后调用free(ptr)是不正确的,不是吗?

That is the correct understanding. 这是正确的理解。 It would not be correct. 这不正确。

What i should do instead is to create a replica of ptr after malloc . 我应该做的是在malloc之后创建一个ptr的副本。 This new pointer should be used instead in the free function call. 应该在free函数调用中使用这个新指针。 Is that right? 是对的吗?

You have the following choice here: 您有以下选择:

  1. Keep the original variable and use a copy to point to different locations of the malloc ed data. 保留原始变量并使用副本指向malloc ed数据的不同位置。 Call free using original variable. 使用原始变量free呼叫。

  2. Make a copy, use the original variable to point to different locations of the malloc ed data. 制作副本,使用原始变量指向malloc ed数据的不同位置。 Call free using copy. 使用副本free拨打电话

  3. Keep the original pointer. 保留原始指针。 Use an index to access different locations of the malloc ed data. 使用索引访问malloc ed数据的不同位置。 Call free using original variable. 使用原始变量free呼叫。

Regardless of the way you access different locations of the data returned by malloc , the value of the pointer that was returned by malloc must be used in the call to free . 不管你访问返回的数据的不同位置的方式malloc ,这是由返回指针的值malloc必须调用来free

Yes, your understanding and suggested solution are correct. 是的,您的理解和建议的解决方案是正确的。 Moreover, according to the C11 standard, 而且,根据C11标准,

7.22.3.3 The free function 7.22.3.3 free功能

...if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc , the behavior is undefined ...如果参数与先前由内存管理函数返回的指针不匹配,或者如果通过调用freerealloc释放了空间,则行为未定义

So trying to free somewhere in the middle of the memory you allocated causes undefined behavior and likely breaks your program. 所以试图在你分配的内存中间某处free会导致未定义的行为,并可能会破坏你的程序。

As per Chapter 7.20.3.2, c99 standard, 2nd paragraph [emphasis mine] 根据第7.20.3.2章, c99标准,第2段[强调我的]

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. 否则, 如果参数与之前由callocmallocrealloc函数返回的指针不匹配,或者如果通过调用freerealloc释放了空间, 则行为未定义。

So, you cannot pass a pointer to free() which is not returned by malloc() or family. 因此,您无法将指针传递给malloc()或family不返回的free() You're very right. 你说得对。

To get your job done, at the beginning, you've to make a copy of the orginal pointer returned by malloc() and once you're done with using the memory, you need to call free() with the previously stored value. 为了完成你的工作,在开始时,你要复制malloc()返回的原始指针,一旦你完成了使用内存,你需要用之前存储的值调用free()

Yes. 是。 You understood right. 你明白了。 When ptr is changed to point to different location then calling free will memory allocated by malloc will not be deallocated. ptr被更改为指向不同的位置时,调用free将将malloc分配的malloc不会被释放。
You need to keep a copy of the returned pointer. 您需要保留返回指针的副本。

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

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