使用realloc释放内存的麻烦 [英] trouble in freeing memory using realloc

查看:179
本文介绍了使用realloc释放内存的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的程序有一个指向在本地声明的结构的指针

函数,使用malloc分配内存并重新分配

稍后使用realloc,当我尝试在函数结束时释放内存时,程序会崩溃。在调试时,我发现指针指向的

地址在其中一个
realloc调用期间发生了变化。但根据realloc的文档,

地址的更改是完全正常的。我的一些同事说,释放中的一些错误是由于realloc中的一些错误而导致内存失败的原因。我觉得很难相信,但是b $ b相信。它是依赖于编译器(IDE是MS Visual

Studion .NET 2003)还是我的代码可能有问题?

解决方案




cijo写道:



我的程序有一个指针指向在一个
函数内部声明的结构,使用malloc分配内存并稍后使用realloc重新分配,当我尝试在函数结束时释放内存时,程序崩溃。在调试时,我发现指向指向的
地址在其中一个重新分配调用期间发生了变化。但是根据realloc的文档,
地址的变化是完全正常的。我的一些同事说,释放记忆的失败是由于''realloc'中的一些错误。我觉得很难相信。它是依赖于编译器(IDE是MS Visual
Studion .NET 2003)还是我的代码可能有问题?




可能有您的代码有问题。


尝试将问题提炼为简短但完整的

和可编辑的示例,以显示问题,并发布

那个例子。令人惊讶的是,当我不需要咨询它时,我的水晶球有多好......


-
< a href =mailto:Er ********* @ sun.com> Er ********* @ sun.com


cijo写道:


我的程序有一个指向一个结构的指针,该结构在一个
函数内部声明到哪个内存使用malloc进行分配并稍后使用realloc重新分配,当我尝试在函数结束时释放内存时,程序会崩溃。在调试时,我发现指向指向的
地址在其中一个重新分配调用期间发生了变化。但是根据realloc的文档,
地址的变化是完全正常的。我的一些同事说,释放记忆的失败是由于''realloc'中的一些错误。我觉得很难相信。它是依赖于编译器(IDE是MS Visual
Studion .NET 2003)还是我的代码可能有问题?




向我们展示最简单的可编辑代码片段,展示了您所描述的

行为 - 否则*任何人*如何*告诉我们发生了什么。


请注意,然而,与malloc / realloc / free相关的失败通常是由于您的免费商店损坏,通常是因为在分配的内存范围之外写了



HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays


希望这有助于......:

typedef struct rangeElt_t

{

unsigned long startRange;

unsigned long endRange;

unsigned long startGIndex;

} rangeElt;


rangeElt * rangeEltBuf = NULL;


int _npdf_decodeCharmap(int ttfid,FILE * fcmapOut)

{

... .....

........

rangeEltBuf =(rangeElt *)malloc(CHUNK_SIZE *

sizeof(rangeElt ));

if(rangeEltBuf == NULL)

{

/ *错误处理* /

}


for(j = 1; j<一世; j ++)/ *我在运行时确定的值* /

{

numRange ++;

if(CHUNK_SIZE - numRange == 1)

{

rangeEltBuf =(rangeElt *)realloc(rangeEltBuf,((numRange +

CHUNK_SIZE)* sizeof(rangeElt)));

if(rangeEltBuf == NULL)

{

/ *错误处理* /

}

}

..................


}


if(rangeEltBuf!= NULL)

{

free(rangeEltBuf); / *程序在这里失败* /

rangeEltBuf = NULL;

}


}


Hi,

My program has a pointer to a structure declared locally inside a
function to which memory is allocated using malloc and reallocated
later using realloc, and when I try to free the memory at the end of
the function, the program breaks down. While debugging, I find that the
address to which the pointer is pointing is changed during one of the
realloc calls. But as per the documentation on realloc, the change of
address is perfectly normal. Some of my colleagues say the failure in
''free''ing the memory is due to some bug in ''realloc''. I find it hard to
believe though. Is it dependent on the compiler (the IDE is MS Visual
Studion .NET 2003) or could there be something wrong with my code?

解决方案



cijo wrote:

Hi,

My program has a pointer to a structure declared locally inside a
function to which memory is allocated using malloc and reallocated
later using realloc, and when I try to free the memory at the end of
the function, the program breaks down. While debugging, I find that the
address to which the pointer is pointing is changed during one of the
realloc calls. But as per the documentation on realloc, the change of
address is perfectly normal. Some of my colleagues say the failure in
''free''ing the memory is due to some bug in ''realloc''. I find it hard to
believe though. Is it dependent on the compiler (the IDE is MS Visual
Studion .NET 2003) or could there be something wrong with my code?



There could be something wrong with your code.

Try distilling the problem down to a short but complete
and compilable example that shows the trouble, and post
that example. It is astonishing how much better my crystal
ball works when I don''t need to consult it ...

--
Er*********@sun.com


cijo wrote:

Hi,

My program has a pointer to a structure declared locally inside a
function to which memory is allocated using malloc and reallocated
later using realloc, and when I try to free the memory at the end of
the function, the program breaks down. While debugging, I find that the
address to which the pointer is pointing is changed during one of the
realloc calls. But as per the documentation on realloc, the change of
address is perfectly normal. Some of my colleagues say the failure in
''free''ing the memory is due to some bug in ''realloc''. I find it hard to
believe though. Is it dependent on the compiler (the IDE is MS Visual
Studion .NET 2003) or could there be something wrong with my code?



Show us the smallest compilable snippet of code that exhibits the
behavior you describe -- otherwise how could *anyone* tell what''s going on.

Be advised, however, that failures related to malloc/realloc/free are
usually due to the corruption of your free store, usually due to writing
outside the bounds of allocated memory.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


Hope this helps...:
typedef struct rangeElt_t
{
unsigned long startRange;
unsigned long endRange;
unsigned long startGIndex;
}rangeElt;

rangeElt *rangeEltBuf = NULL;

int _npdf_decodeCharmap(int ttfid, FILE *fcmapOut)
{
........
........
rangeEltBuf = (rangeElt *)malloc(CHUNK_SIZE *
sizeof(rangeElt));
if(rangeEltBuf == NULL)
{
/* error handling */
}

for (j = 1; j < i; j++) /*value of i determined at run time */
{
numRange++;
if(CHUNK_SIZE - numRange == 1)
{
rangeEltBuf = (rangeElt *)realloc(rangeEltBuf, ((numRange+
CHUNK_SIZE) * sizeof(rangeElt)));
if(rangeEltBuf == NULL)
{
/* error handling */
}
}
..................

}

if(rangeEltBuf != NULL)
{
free(rangeEltBuf); /* the program fails here */
rangeEltBuf = NULL;
}

}


这篇关于使用realloc释放内存的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆