- 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 youeffectively. On general principles, lose the cast, replace sizeof(book)with sizeof *bp, use a temp to catch the result of realloc in case itfails and returns NULL, and make sure you have #included <stdlib.h>. Notethat bp is a copy of the calling function''s pointer; the original will notbe updated automatically, so if you want to keep the new value you mustprovide a mechanism for the caller to assign this new value to theoriginal pointer.For more specific help, post more specific code. "..." doesn''t say anythinguseful, 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 itfails and returns NULL, and make sure you have #included <stdlib.h>. Notethat bp is a copy of the calling function''s pointer; the original will notbe updated automatically, so if you want to keep the new value you mustprovide a mechanism for the caller to assign this new value to theoriginal 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 arraybool 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 numberwhile(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 numbertoo long, 9 digit max.\n"); continue; }//check each letter in string if a digit, if all are then check =TRUE, else check = FALSEi=0;while(str_cn[i]){if(isdigit(str_cn[i])) { check = TRUE; i++; continue; }else { check = FALSE; printf("[E] catalog number not valid, tryagain.\n"); break; }}if(check == TRUE) { temp.cn = atol(str_cn); } //if all OK, put valuein 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 howit looksbook* AddBook(book *bp, unsigned *size) {...//then i use realloc to allocate space for the new item in the bppointerbp = (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 newsize. Since you are passing sizeof( book ) you are only allocating enoughspace for 1 item.You probably meant something like:bp = realloc( bp, sizeof( book ) * *size );orbp = realloc( bp, sizeof( book ) * (*size + 1) )or something. I''m not sure how many additional items you want to allocatefor 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''ssay *bp hold 2 book type items, first item will be ok, second one willget 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 alsohappens with more items in array.i know that to use linked lists is a lot better solution, but i stilldunno how to use them right just yet. - Jim Langston ta ******* @ rocketmail.com 这篇关于realloc问题,腐败最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-11 03:46