malloc()重用地址吗? [英] Does malloc() reuse addresses?
问题描述
大家好,
我的问题是:
如果我用malloc()分配一些内存然后释放它(使用
free()),是否有可能后续的malloc()会在同一个起始地址分配memort并将返回相同的
指针作为前一个malloc()。我想在
上确认当指针用于确定数据结构实例时,这是否实际上是一个问题 - 就像在这个例子中一样:
int isInstanceValid(myStrict * inst)
{
int i;
for(i = 0; i< instCount; ++ i)
if(instances [i] == inst)
返回1;
返回0;
}
在这个例子中,如果一个实例被释放,并且指向它的指针变为
无效,并且稍后在列表中分配一个新结构,
函数将返回指针有效,尽管它实际上是
而不是最初引用的实例。 />
Hi all,
my question is:
if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc(). I would like to have confirmation on
whether this is practically a concern when pointers are used to
uniquely identify data structure instances - like in this example:
int isInstanceValid(myStrict* inst)
{
int i;
for (i=0; i<instCount; ++i)
if (instances[i] == inst)
return 1;
return 0;
}
In this example, if an instance is freed, and a pointer to it becomes
non-valid, and later a new structure is allocated in the list, the
function will return that the pointer is valid, although it is actually
not the instance that was originally referred.
推荐答案
" avasilev" < al ******** @ gmail.com在留言中写道
新闻:11 ********************* @ h48g2000cwc.googlegro ups.com ...
"avasilev" <al********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
大家好,
我的问题是:
如果我用malloc()分配一些内存并稍后释放它(使用
free()),是否有可能导致一个后续的malloc()将
在相同的起始地址分配memort,并返回与前一个malloc()相同的
指针。
Hi all,
my question is:
if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
是的。它一直在发生。
Yes. It happens all the time.
" avasilev" < al ******** @ gmail.comwrites:
"avasilev" <al********@gmail.comwrites:
如果我用malloc()分配一些内存然后释放它(使用
free()),有可能一个后来的malloc()会在同一个起始地址分配memort并且会返回相同的
指针作为前一个malloc()。
if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
是。
Yes.
我想确认这是否是
$ b当指针被用于唯一的
标识数据结构实例时,$ b几乎是一个问题 - 就像在这个例子中一样:
I would like to have confirmation on whether this is
practically a concern when pointers are used to uniquely
identify data structure instances - like in this example:
这是一个不正确的做法严格来说,使用指向释放内存的指针执行任何操作的行为都会产生未定义的行为。
你最好关闭使用计数器标记每个新结构
使用唯一的序列号然后比较那些独特的
序列号。
-
我们把[最好的]汇编程序员放在走廊里的一个小玻璃柜里
靠近出口标志。案件上的标志上写着,如果是优化
问题,请打破玻璃。同时,问题解决者正在忙于用最适合的语言工作
手头的工作。 --Richard Riehle
It''s an incorrect approach. Strictly speaking the behavior of
doing anything with a pointer to freed memory yields undefined
behavior.
You''re better off using a counter to stamp each new structure
with a unique serial number and then comparing those unique
serial numbers.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.'' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
avasilev写道:
avasilev wrote:
大家好,
我的问题是:
如果我用malloc()分配一些内存然后释放它(使用
free()),是否有可能后续的malloc()将在同一个起始地址分配memort并返回与前一个malloc相同的
指针( )。
Hi all,
my question is:
if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
它可能;你不能依赖它。
It might; you cannot depend on it though.
我想确认
当指针用于<时,这是否真的是一个问题br />
唯一标识数据结构实例 - 如下例所示:
int isInstanceValid(myStrict * inst)
{
int i;
for(i = 0; i< instCount; ++ i)
if(instances [i] == inst)
返回1;
返回0;
}
在本例中,如果是实例被释放,并且指向它的指针变为
无效,稍后在列表中分配新结构,
函数将返回指针有效,虽然它实际上是
而不是最初提到的实例。
I would like to have confirmation on
whether this is practically a concern when pointers are used to
uniquely identify data structure instances - like in this example:
int isInstanceValid(myStrict* inst)
{
int i;
for (i=0; i<instCount; ++i)
if (instances[i] == inst)
return 1;
return 0;
}
In this example, if an instance is freed, and a pointer to it becomes
non-valid, and later a new structure is allocated in the list, the
function will return that the pointer is valid, although it is actually
not the instance that was originally referred.
是
鹅,
yes
goose,
这篇关于malloc()重用地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!