[英]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)
, afterptr
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
aftermalloc
.我应该做的是在
malloc
之后创建一个ptr
的副本。 This new pointer should be used instead in thefree
function call.应该在
free
函数调用中使用这个新指针。 Is that right?是对的吗?
You have the following choice here: 您有以下选择:
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
呼叫。
Make a copy, use the original variable to point to different locations of the malloc
ed data. 制作副本,使用原始变量指向
malloc
ed数据的不同位置。 Call free
using copy. 使用副本
free
拨打电话
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
function7.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
orrealloc
, the behavior is undefined...如果参数与先前由内存管理函数返回的指针不匹配,或者如果通过调用
free
或realloc
释放了空间,则行为未定义
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 byptr
to be deallocated, that is, made available for further allocation.free
函数导致ptr
指向的空间被释放,即可用于进一步分配。 Ifptr
is a null pointer, no action occurs.如果
ptr
是空指针,则不执行任何操作。 Otherwise, if the argument does not match a pointer earlier returned by thecalloc
,malloc
, orrealloc
function, or if the space has been deallocated by a call tofree
orrealloc
, the behavior is undefined.否则, 如果参数与之前由
calloc
,malloc
或realloc
函数返回的指针不匹配,或者如果通过调用free
或realloc
释放了空间, 则行为未定义。
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.