realloc的问题 [英] problem with realloc

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

问题描述

干草,我正在做这个节目。在将数据结构读入数组(指针 - bp2)的

函数中有重新分配的问题,这个

在读取第二条记录后发生。当打电话给realloc时。

i无法弄清楚出了什么问题,认为它正在逼近

释放bp2。

和称为堆腐败的东西。


book * LoadBookData(未签名*大小)

{

FILE * fp;

int n = 0;

book * bp2 = NULL;


//打开书本数据文件

fp = fopen(" book.bin"," rb");

if(fp == NULL)

{

bp2 =(book *)calloc(0,sizeof(book));

返回bp2;

}

<<<<<<<<<<<<<<<< HERE>>>>>>>>>> >>>>>

bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror("错误[realloc - LoadBookData()]");

退出(ERR_REALOC); }


//从文件中读取数据

while(fread(bp2,sizeof(book),1,fp)== 1)

{

if(ferror(fp)){perror(" ERROR [book.bin - read - LoadBookData()]");

退出(ERR_FREAD); }

bp2 = bp2 - n;

n ++;


bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror(" ERROR [realloc - LoadBookData()]");

exit(ERR_REALOC); }


bp2 = bp2 + n;

}


//关闭书籍数据文件

if(fclose(fp)== EOF)

{perror(" ERROR [book.bin - close - LoadBookData()]]");

退出(ERR_FCLOSE); }


* size = n;


返回bp2;

}

解决方案

5月3日晚上7:21,Igal< igal.al ... @ gmail.comwrote:


hay,我正在做这个节目。在将数据结构读入数组(指针 - bp2)的

函数中有重新分配的问题,这个

在读取第二条记录后发生。当打电话给realloc时。

i无法弄清楚出了什么问题,认为它正在逼近

释放bp2。

和称为堆腐败的东西。


book * LoadBookData(未签名*大小)

{

FILE * fp;

int n = 0;

book * bp2 = NULL;


//打开书本数据文件

fp = fopen(" book.bin"," rb");

if(fp == NULL)

{

bp2 =(book *)calloc(0,sizeof(book));

返回bp2;



不要施放calloc。我不确定calloc(0,N)的结果是什么,

但是如果它们与malloc()类似,那么那个指针实际上不是

可靠。

您最有可能想要这样的东西:

返回calloc(0,sizeof book);


}

<<<<<<<<<<<<<<< ;<<此处>>>>>>>>>>>>>>



用/ * * /
注释你的代码


bp2 = realloc(bp2,sizeof(book) );



这实际上是一个malloc(),因为bp2之前被初始化为NULL,

和realloc(NULL,N)是等于malloc(N)


if(bp2 == NULL){perror(" ERROR [realloc - LoadBookData()]");

退出(ERR_REALOC); }



您确定要退出吗?

ERR_REALOC的价值是多少?


>

//从文件中读取数据

while(fread(bp2,sizeof(book) ,1,fp)== 1)

{

if(ferror(fp)){perror(" ERROR [book.bin - read - LoadBookData()] ");



fread()如果出现

流中的错误,则无法返回计数(第三个参数)。


exit(ERR_FREAD); }



这里的内存泄漏,你不能释放`bp2''。


bp2 = bp2 - n;

n ++;



为什么?


>

bp2 = realloc(bp2,sizeof(book));



如果`bp2''没有指向realloc,malloc或

calloc返回的指针,realloc()将产生未定义的结果(除非`bp2'的值

为NULL)

但我*看*你要做什么,这里是实际的逻辑:

size_t n = 0;

返回:

if(fread(& bp2 [n],sizeof bp2 [0],1,fp )!= 1){/ *处理错误或EOF

* /}

n ++;

bp2 = realloc(bp2,n + 1) * sizeof bp2 [0]);

if(bp2 == NULL){/ * ... * /}

转到后面;




Igal < ig ******** @ gmail.comwrote in message

news:53 ********************** ************ @ e39g2000 hsf.googlegroups.com ...


hay,我正在做这个程序。在将数据结构读入数组(指针 - bp2)的

函数中有重新分配的问题,这个

在读取第二条记录后发生。当打电话给realloc时。

i无法弄清楚出了什么问题,认为它正在逼近

释放bp2。

和称为堆腐败的东西。


book * LoadBookData(未签名*大小)

{

FILE * fp;

int n = 0;

book * bp2 = NULL;


//打开书本数据文件

fp = fopen(" book.bin"," rb");

if(fp == NULL)

{

bp2 =(book *)calloc(0,sizeof(book));

返回bp2;

}

<<<<<<<<<<<<<<<< HERE>>>>>>>>>> >>>>>

bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror("错误[realloc - LoadBookData()]");

退出(ERR_REALOC); }


//从文件中读取数据

while(fread(bp2,sizeof(book),1,fp)== 1)

{

if(ferror(fp)){perror(" ERROR [book.bin - read - LoadBookData()]");

退出(ERR_FREAD); }

bp2 = bp2 - n;

n ++;


bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror(" ERROR [realloc - LoadBookData()]");

exit(ERR_REALOC); }


bp2 = bp2 + n;

}


//关闭书籍数据文件

if(fclose(fp)== EOF)

{perror(" ERROR [book.bin - close - LoadBookData()]]");

退出(ERR_FCLOSE); }


* size = n;


返回bp2;

}`



说嘿。


Jim


Igal写道:


hay,我正在做这个程序。在将数据结构读入数组(指针 - bp2)的

函数中有重新分配的问题,这个

在读取第二条记录后发生。当打电话给realloc时。

i无法弄清楚出了什么问题,认为它正在逼近

释放bp2。

和称为堆腐败的东西。


book * LoadBookData(未签名*大小)

{

FILE * fp;

int n = 0;

book * bp2 = NULL;


//打开书本数据文件

fp = fopen(" book.bin"," rb");

if(fp == NULL)

{

bp2 =(book *)calloc(0,sizeof(book));

返回bp2;

}

<<<<<<<<<<<<<<<< HERE>>>>>>>>>> >>>>>

bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror("错误[realloc - LoadBookData()]");

退出(ERR_REALOC); }


//从文件中读取数据

while(fread(bp2,sizeof(book),1,fp)== 1)

{

if(ferror(fp)){perror(" ERROR [book.bin - read - LoadBookData()]");

退出(ERR_FREAD); }

bp2 = bp2 - n;

n ++;


bp2 = realloc(bp2,sizeof(book));

if(bp2 == NULL){perror(" ERROR [realloc - LoadBookData()]");

exit(ERR_REALOC); }


bp2 = bp2 + n;

}


//关闭书籍数据文件

if(fclose(fp)== EOF)

{perror(" ERROR [book.bin - close - LoadBookData()]]");

退出(ERR_FCLOSE); }


* size = n;


返回bp2;

}



calloc的两个参数相乘以形成

分配的大小。 calloc(0 * sizeof(书))得出大小0.


-

Joe Wright

一切都应该是尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---


hay, i''m doing this program. having problem wiht realloc in the
function that reads data structures into array (pointer - bp2), this
happens after reading the second record. when call to realloc.
i can''t figure out what''s wrong, think it''s soming got to do with
freeing bp2.
and something called "corruption of the heap".

book* LoadBookData(unsigned *size)
{
FILE* fp;
int n = 0;
book *bp2 = NULL;

//open book data file
fp=fopen("book.bin","rb");
if (fp == NULL)
{
bp2 = (book*)calloc(0, sizeof(book));
return bp2;
}
<<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

//read data from file
while(fread(bp2,sizeof(book),1,fp) == 1)
{
if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
exit(ERR_FREAD); }
bp2 = bp2 - n;
n++;

bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

bp2 = bp2 + n;
}

//close book data file
if (fclose(fp)==EOF)
{ perror("ERROR [book.bin - close - LoadBookData()]]");
exit(ERR_FCLOSE); }

*size = n;

return bp2;
}

解决方案

On May 3, 7:21 pm, Igal <igal.al...@gmail.comwrote:

hay, i''m doing this program. having problem wiht realloc in the
function that reads data structures into array (pointer - bp2), this
happens after reading the second record. when call to realloc.
i can''t figure out what''s wrong, think it''s soming got to do with
freeing bp2.
and something called "corruption of the heap".

book* LoadBookData(unsigned *size)
{
FILE* fp;
int n = 0;
book *bp2 = NULL;

//open book data file
fp=fopen("book.bin","rb");
if (fp == NULL)
{
bp2 = (book*)calloc(0, sizeof(book));
return bp2;

Don''t cast calloc. I''m not sure what the results of calloc(0, N) are,
but if they are similar of malloc(), then that pointer is not really
reliable.
You most likely want something like this:
return calloc(0, sizeof book);

}
<<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>

Comment your code with /* */

bp2 = realloc(bp2, sizeof(book));

That''s actually a malloc(), since bp2 was initialized to NULL before,
and realloc(NULL, N) is equal to malloc(N)

if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

Are you sure you want to exit here? And what is the value of
ERR_REALOC?

>
//read data from file
while(fread(bp2,sizeof(book),1,fp) == 1)
{
if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");

fread() can''t return the count (third parameter) if an error in the
stream occurs.

exit(ERR_FREAD); }

Memory leak here, you don''t free `bp2''.

bp2 = bp2 - n;
n++;

Why?

>
bp2 = realloc(bp2, sizeof(book));

If `bp2'' doesn''t point to a pointer returned by realloc, malloc or
calloc, realloc() will produce undefined results (unless `bp2''s value
is NULL)
But I *see* what you are trying to do, here''s the actual logic:
size_t n = 0;
back:
if(fread(&bp2[n], sizeof bp2[0], 1, fp) != 1) { /* handle error or EOF
*/ }
n++;
bp2 = realloc(bp2, n+1 * sizeof bp2[0]);
if(bp2 == NULL) { /* ... */ }
goto back;



"Igal" <ig********@gmail.comwrote in message
news:53**********************************@e39g2000 hsf.googlegroups.com...

hay, i''m doing this program. having problem wiht realloc in the
function that reads data structures into array (pointer - bp2), this
happens after reading the second record. when call to realloc.
i can''t figure out what''s wrong, think it''s soming got to do with
freeing bp2.
and something called "corruption of the heap".

book* LoadBookData(unsigned *size)
{
FILE* fp;
int n = 0;
book *bp2 = NULL;

//open book data file
fp=fopen("book.bin","rb");
if (fp == NULL)
{
bp2 = (book*)calloc(0, sizeof(book));
return bp2;
}
<<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

//read data from file
while(fread(bp2,sizeof(book),1,fp) == 1)
{
if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
exit(ERR_FREAD); }
bp2 = bp2 - n;
n++;

bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

bp2 = bp2 + n;
}

//close book data file
if (fclose(fp)==EOF)
{ perror("ERROR [book.bin - close - LoadBookData()]]");
exit(ERR_FCLOSE); }

*size = n;

return bp2;
}`



Say hey.

Jim


Igal wrote:

hay, i''m doing this program. having problem wiht realloc in the
function that reads data structures into array (pointer - bp2), this
happens after reading the second record. when call to realloc.
i can''t figure out what''s wrong, think it''s soming got to do with
freeing bp2.
and something called "corruption of the heap".

book* LoadBookData(unsigned *size)
{
FILE* fp;
int n = 0;
book *bp2 = NULL;

//open book data file
fp=fopen("book.bin","rb");
if (fp == NULL)
{
bp2 = (book*)calloc(0, sizeof(book));
return bp2;
}
<<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

//read data from file
while(fread(bp2,sizeof(book),1,fp) == 1)
{
if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
exit(ERR_FREAD); }
bp2 = bp2 - n;
n++;

bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

bp2 = bp2 + n;
}

//close book data file
if (fclose(fp)==EOF)
{ perror("ERROR [book.bin - close - LoadBookData()]]");
exit(ERR_FCLOSE); }

*size = n;

return bp2;
}

The two arguments to calloc are multiplied to form the size of the
allocation. calloc(0 * sizeof (book)) yields size 0.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


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

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