realloc问题,腐败最后一项 [英] realloc problem, corrupt last item

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

问题描述

干草,我的书添加功能有这个问题,这个看起来怎么样?
它看起来好像


book * AddBook(book * bp,unsigned *尺寸){

....

//然后我使用realloc为bp中的新项目分配空间

指针

bp =(book *)realloc(bp,sizeof(book));

....

}

当我这样做时,下一个会被添加到这个正确的空间中。但是让我们说'b $ b说* bp持有2本书类型的物品,第一项是好的,第二项将

获得垃圾内容,然后第三项(新的)重新分配一个)

将获得新数据。

i只是不知道最后一个项目是否会被破坏,这也是

发生了更多项目数组。


i知道使用链接列表是一个更好的解决方案,但我仍然是

不知道如何正确使用它们。

hay, i have this werid problem with my book adding function, this how
it looks

book* AddBook(book *bp, unsigned *size) {
....
//then i use realloc to allocate space for the new item in the bp
pointer
bp = (book*)realloc(bp, sizeof(book));
....
}

when i do this, the next them is added in this right space. but let''s
say *bp hold 2 book type items, first item will be ok, second one will
get garbage content, then the third item (the new reallocated one)
will get the new data.
i just have no idea will the last item gets corruped, this also
happens with more items in array.

i know that to use linked lists is a lot better solution, but i still
dunno how to use them right just yet.

推荐答案

Igal说:
Igal said:

hay,我的书添加功能有这个问题,这个怎么样

它的样子


book * AddBook(book * bp,unsigned * size){

...

//然后我使用realloc为bp中的新项目分配空间

指针

bp =(book *)realloc(bp,sizeof (书));

...

}
hay, i have this werid problem with my book adding function, this how
it looks

book* AddBook(book *bp, unsigned *size) {
...
//then i use realloc to allocate space for the new item in the bp
pointer
bp = (book*)realloc(bp, sizeof(book));
...
}



您还没有提供足够的信息我有效地帮助你

。在一般原则上,丢失演员,用sizeof * bp替换sizeof(书)

,使用temp来捕获realloc的结果,以防它b / b
失败并返回NULL ,并确保你有#included< stdlib.h>。注意

bp是调用函数指针的副本;原件不会自动更新
,所以如果你想保留新值,你必须为b
提供一种机制让调用者将这个新值分配给

原始指针。


如需更具体的帮助,请发布更具体的代码。 " ..."什么都不说

很有用,而且你没有显示主叫代码。


< snip>

-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

You have not provided sufficient information for me to help you
effectively. On general principles, lose the cast, replace sizeof(book)
with sizeof *bp, use a temp to catch the result of realloc in case it
fails and returns NULL, and make sure you have #included <stdlib.h>. Note
that bp is a copy of the calling function''s pointer; the original will not
be updated automatically, so if you want to keep the new value you must
provide a mechanism for the caller to assign this new value to the
original pointer.

For more specific help, post more specific code. "..." doesn''t say anything
useful, and you haven''t shown the calling code.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


你没有提供足够的信息来帮助你
You have not provided sufficient information for me to help you

有效。在一般原则上,丢失演员,用sizeof * bp替换sizeof(书)

,使用temp来捕获realloc的结果,以防它b / b
失败并返回NULL ,并确保你有#included< stdlib.h>。注意

bp是调用函数指针的副本;原件不会自动更新
,所以如果你想保留新值,你必须为b
提供一种机制让调用者将这个新值分配给

原始指针。
effectively. On general principles, lose the cast, replace sizeof(book)
with sizeof *bp, use a temp to catch the result of realloc in case it
fails and returns NULL, and make sure you have #included <stdlib.h>. Note
that bp is a copy of the calling function''s pointer; the original will not
be updated automatically, so if you want to keep the new value you must
provide a mechanism for the caller to assign this new value to the
original pointer.



这里是完整的功能。

i这样称呼:


bp = AddBook(bp,& BOOK_SIZE);


/ * ########################## ###

#添加书本功能#

########################## ### * /

book * AddBook(book * bp,unsigned * size)

{

book temp;

unsigned n = * size; // n =书籍数组的大小

bool check = FALSE;

char str_cn [80];

int i;


printf("%d",(int)(sizeof(bp)/ sizeof(bp [0])));


n ++;

bp =(book *)realloc(bp,sizeof(book));

if(bp == NULL){perror(" ERROR [realloc - AddBook() ]");

退出(ERR_REALOC); }

printf(" \ n [Add Book to Catalog] \ n");


//获取目录号

while(check!= TRUE)

{

printf(" Book Catalog Number:");

_flushall() ;

得到(str_cn);


//检查字符串是否超过9位数。如果更长则错误。

if(strlen(str_cn)9){check = FALSE; printf([E]目录号

太长,最多9位数\ nn);继续; }


//检查字母串中的每个字母是否有数字,如果全部则检查=

TRUE,否则检查= FALSE

i = 0;

while(str_cn [i])

{

if(isdigit(str_cn [i])){check =真正;我++;继续; }

else {check = FALSE; printf([E]目录号无效,再次尝试

。\ n");打破; }

}


if(check == TRUE){temp.cn = atol(str_cn); } //如果一切正常,把价值

放在str_cn中

}

check = FALSE;


...这里我将数据输入temp,并检查验证...


* size = n;


bp [n -1] .cn = temp.cn;

bp [n-1] .Units = temp.Units;

bp [n-1] .Year = temp。年份;

bp [n-1] .Price.CostPrice = temp.Price.CostPrice;

bp [n-1] .Price.RetailPrice = temp.Price。 RetailPrice;


strcpy(bp [n-1] .Publisher,temp.Publisher);

strcpy(bp [n-1] .BookName,temp .BookName);

strcpy(bp [n-1] .Author.Author1,temp.Author.Author1);

strcpy(bp [n-1] .Author .Author2,temp.Author.Author2);

strcpy(bp [n-1] .Author.Author3,temp.Author.Author3);


返回bp;

}

here''s the full function.
i call it like this:

bp = AddBook(bp, &BOOK_SIZE);

/*#############################
# Add Book Function #
#############################*/
book* AddBook(book *bp, unsigned *size)
{
book temp;
unsigned n = *size; //n = size of book array
bool check = FALSE;
char str_cn[80];
int i;

printf("%d", (int)(sizeof(bp)/sizeof(bp[0])));

n++;
bp = (book*)realloc(bp, sizeof(book));
if (bp == NULL) { perror("ERROR [realloc - AddBook()]");
exit(ERR_REALOC); }
printf("\n[Add Book to Catalog]\n");

//get catalog number
while(check != TRUE)
{
printf("Book Catalog Number: ");
_flushall();
gets(str_cn);

//check if string is longer then 9 digits. if longer then error.
if(strlen(str_cn) 9) { check = FALSE; printf("[E] catalog number
too long, 9 digit max.\n"); continue; }

//check each letter in string if a digit, if all are then check =
TRUE, else check = FALSE
i=0;
while(str_cn[i])
{
if(isdigit(str_cn[i])) { check = TRUE; i++; continue; }
else { check = FALSE; printf("[E] catalog number not valid, try
again.\n"); break; }
}

if(check == TRUE) { temp.cn = atol(str_cn); } //if all OK, put value
in str_cn
}
check = FALSE;

... here i get data into temp, and check validation ...

*size = n;

bp[n-1].cn = temp.cn;
bp[n-1].Units = temp.Units;
bp[n-1].Year = temp.Year;
bp[n-1].Price.CostPrice = temp.Price.CostPrice;
bp[n-1].Price.RetailPrice = temp.Price.RetailPrice;

strcpy(bp[n-1].Publisher, temp.Publisher);
strcpy(bp[n-1].BookName, temp.BookName);
strcpy(bp[n-1].Author.Author1, temp.Author.Author1);
strcpy(bp[n-1].Author.Author2, temp.Author.Author2);
strcpy(bp[n-1].Author.Author3, temp.Author.Author3);

return bp;
}


Igal写道:
Igal wrote:

hay,我有这个问题与我的书添加功能有关,这个怎么样看起来很好看?


book * AddBook(book * bp, unsigned * size){

...

//然后我使用realloc为bp中的新项目分配空间

指针

bp =(book *)realloc(bp,sizeof(book));
hay, i have this werid problem with my book adding function, this how
it looks

book* AddBook(book *bp, unsigned *size) {
...
//then i use realloc to allocate space for the new item in the bp
pointer
bp = (book*)realloc(bp, sizeof(book));



realloc接受指向prevoiusly分配的块的指针,以及新的
大小。由于你传递的是sizeof(书),你只需要为1件物品分配足够的

空间。


你可能意味着:

bp = realloc(bp,sizeof(book)* * size);



bp = realloc(bp,sizeof(book)*(* size + 1 ))

或者别的什么。我不确定你要分配多少额外的物品

也不知道大小代表什么尺寸(新尺寸或旧尺寸)。

realloc accepts the pointer to the prevoiusly allocated block, and the new
size. Since you are passing sizeof( book ) you are only allocating enough
space for 1 item.

You probably meant something like:
bp = realloc( bp, sizeof( book ) * *size );
or
bp = realloc( bp, sizeof( book ) * (*size + 1) )
or something. I''m not sure how many additional items you want to allocate
for nor what size represents (new size or old size).


...

}


当我这样做时,下一个被添加到这个合适的空间。但是让我们说'b $ b说* bp持有2本书类型的物品,第一项是好的,第二项将

获得垃圾内容,然后第三项(新的)重新分配一个)

将获得新数据。

i只是不知道最后一个项目是否会被破坏,这也是

发生了更多项目在数组中。


i知道使用链表是一个更好的解决方案,但我仍然是

dunno如何正确使用它们。
...
}

when i do this, the next them is added in this right space. but let''s
say *bp hold 2 book type items, first item will be ok, second one will
get garbage content, then the third item (the new reallocated one)
will get the new data.
i just have no idea will the last item gets corruped, this also
happens with more items in array.

i know that to use linked lists is a lot better solution, but i still
dunno how to use them right just yet.




-

Jim Langston
ta ******* @ rocketmail.com


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

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