不可能的泄漏realloc [英] Impossible Leak realloc

查看:101
本文介绍了不可能的泄漏realloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




任何人都可以告诉我这段代码有什么问题吗?


它泄漏了(加入绑定检查器),当它试图重新分配内存时。


代码不是纯C,但是经过微调,任何C编译器都会编译这个。


谢谢


Eitan Michaelson。


//////////// //////////////////////////////////////代码从这里开始

/ ////////////////////////////////////////////////// ////////////////////


//此代码尝试创建指向结构的指针,然后分配

该结构的缓冲区,所以它会保存一个结构数组。


//在每个循环中我试图重新分配结构数组所以它将

增长1,然后我正在尝试将数据添加到其中。


//其中一个结构元素是void *,而这又由

malloc分配。


#inclu de< stdio.h>

#include< malloc.h>

#include< string.h>


#define MAX_STRUCT_SIZE(50)

struct st_Test

{

char szChar [32];

int iInt ;

void * pVoid;

};

void main(无效)

{

//指向结构的指针

st_Test * pst_Test;

pst_Test = NULL; //指针无效

for(int i = 0; i< = MAX_STRUCT_SIZE; i ++)

{

if(!pst_Test) )//尚未分配

pst_Test =(st_Test *)malloc(1 * sizeof(st_Test));

else //调整大小1

pst_Test =(st_Test *)realloc(pst_Test,(i + 1)

* sizeof(st_Test)); ///这是泄漏/////////

//重置我们得到的缓冲区

memset(& pst_Test [i],'' \''',sizeof(st_Test));

//将一些数据添加到分配的结构中

pst_Test [i] .iInt = 64738;

strcpy(pst_Test [i] .szChar," poke 53280,1");

//为void *元素分配缓冲区//////这是

其中绑定检查器发现问题///

if(!pst_Test [i] .pVoid)

pst_Test [i] .pVoid =(char * )malloc(32);

strcpy((char *)pst_Test [i] .pVoid," commodre 64");

}

///////////////////////////////////////////// /////结束

//////////////////////////////////// ///////////////////////////////////


[见< a rel =nofollowhref =http://www.gotw.ca/resources/clcm.htmtarget =_ blank> http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hi,

Can any one tell me what''s wrong with this code?

It leaks (acceding to bound checker), when it attempts to reallocate memory.

The code is not pure C, but with minor adjustments any C compiler would
compile this.

Thank you

Eitan Michaelson.

////////////////////////////////////////////////// Code Starts Here
///////////////////////////////////////////////////////////////////////

// This code attempts to create a pointer to a structure,and then allocate a
buffer for that structure, so it would hold an array of structures.

// in each loop Ia??m trying to reallocate the structure array so it would
grow by 1, and then I''m trying to add data into it.

// one of the structure elements is a void*, which in turn is allocated by
malloc.


#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_STRUCT_SIZE (50)
struct st_Test
{
char szChar[32];
int iInt;
void *pVoid;
};
void main(void)
{
// Pointer to the structure
st_Test *pst_Test;
pst_Test = NULL; // pointer not valid yet
for(int i = 0;i <=MAX_STRUCT_SIZE;i++)
{
if(!pst_Test) // not yet allocated
pst_Test = (st_Test*)malloc(1 * sizeof(st_Test));
else // Resize by 1
pst_Test = (st_Test*)realloc(pst_Test,(i+1)
*sizeof(st_Test)); /// This is the Leak /////////
// Reset the buffer we got
memset(&pst_Test[i],''\0'',sizeof(st_Test));
// Add some data to the allocated struct
pst_Test[i].iInt = 64738;
strcpy(pst_Test[i].szChar,"poke 53280,1");
// allocate buffer for the void* element ////// This is
where bound checker found the problem ///
if(!pst_Test[i].pVoid)
pst_Test[i].pVoid = (char*)malloc(32);
strcpy((char*)pst_Test[i].pVoid,"commodre 64");
}

////////////////////////////////////////////////// End
///////////////////////////////////////////////////////////////////////

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Eitan Michaelson写道:
Eitan Michaelson wrote:


任何人都能告诉我什么这段代码错了吗?

当它试图重新分配内存时,它会泄漏(加入绑定检查器)。
Hi,

Can any one tell me what''s wrong with this code?

It leaks (acceding to bound checker), when it attempts to reallocate
memory.




您发布的代码从不调用free()所以大概是所有内容都被泄露了。你需要释放所有元素的pVoid成员和

然后释放pst_Text本身。


(注意:我的新闻阅读器找不到alt.comp .lang.c所以我放弃了它。)


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



The code you posted never calls free() so presumably everything is
leaked. You need to free the pVoid members of all the elements and
then free pst_Text itself.

(Note: My newsreader couldn''t find alt.comp.lang.c so I''ve dropped it.)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


在文章< bd ********** @ news2.netvision.net.il>,

on 2003年6月27日07:01:37 -0400,

Eitan Michaelson< m _ ***** @ netvision.net.il>写道:
In article <bd**********@news2.netvision.net.il>,
on 27 Jun 2003 07:01:37 -0400,
Eitan Michaelson <m_*****@netvision.net.il> wrote:


任何人都可以告诉我这段代码有什么问题吗?

它泄漏了(加入)当它试图重新分配内存时。
Hi,

Can any one tell me what''s wrong with this code?

It leaks (acceding to bound checker), when it attempts to reallocate
memory.




你的代码中没有单独的免费电话,所以它当然是泄密。


-

Light认为它比任何东西都快,但它错了。无论

光速多快,它发现黑暗总是先到达那里,

并等待它。 - Terry Pratchett,收割者Man


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



There isn''t a single call to free in your code, so of course it leaks.

--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Eitan Michaelson写道:
Eitan Michaelson wrote:

任何人都能告诉我这段代码有什么问题吗?

当它试图重新分配内存时它会泄漏(加入绑定检查器)。

Can any one tell me what''s wrong with this code?

It leaks (acceding to bound checker), when it attempts to
reallocate memory.




它包含无效的包含< malloc.h>并且无效(对于C99,以及

以便在消息中显示)//评论表格。缩进是过度的
。你应该首先将它转换为可编辑的形式,在

标准C中,然后发布它。确保行一般不超过65

字符,但80或更多字符的任何内容确实

不属于新闻组。


我们为什么要趟过你创造的一团糟?


-

查克F(cb ******** @ yahoo .com)(cb********@worldnet.att.net)

适用于咨询/临时嵌入式和系统。

< http:/ /cbfalconer.home.att.net>使用worldnet地址!

[见 http:// www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



It contains invalid includes <malloc.h> and invalid (for C99, and
for display in a message) // comment forms. The indentation is
excessive. You should first convert it to a compilable form, in
standard C, and then post it. Ensure that lines do not exceed 65
characters in general, but anything of 80 or more characters does
not belong in newsgroups.

Why should we wade through a mess that you have created?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于不可能的泄漏realloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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