linesInPageAtTheMoment

linesInPageAtTheMoment

本文介绍了*** glibc检测到*** ./test:realloc():无效的旧尺寸:0x00007fff49779070 ***的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 任何评论为什么char **页面没有重新分配 #include< stdlib.h> void add(char ** page,char * line,int n) { char ** temp; if(temp = realloc(page,sizeof(char *)*(n + 1))) { // malloc后来添加行的东西 page = temp; } } int main(void ) { char * s; char * line =" test1"; char * page [] = {NULL}; add(page,line,1); //以后的免费记忆 } 解决方案 因为你骗了realloc()。 由于page不是NULL指针,realloc()认为它代表了一个 malloc()分配的空间块。并不是所有的赌注都没有。 因为你撒谎到realloc()。 由于页面不是NULL指针,因此realloc()认为它代表了malloc()分配的空间块b / b。所有赌注都没有。 Aha so char * page [] = {NULL};需要先替换一些malloc 。 回到绘图板。 因为你骗了realloc()。 从那以后page不是NULL指针,realloc()认为它代表了malloc()分配的空间块。所有赌注都没有。 不仅如此,但无论什么`add`都不会影响 main''s'页面的价值( C没有通过参考传递)。我怀疑 添加's`页面`应该是一个char ***。我相信这是一个案例, typedefing指针[或引入结构]是有道理的: typedef char * Line; / *一行是一个指向以null结尾的seq char * / typedef Line * Page; / *一个页面是指向行的序列* / void addLine(Page * refPage,line line,int linesInPageAtTheMoment) { 页面realloced = realloc(* refPage,sizeof(Line)*(linesInPageAtTheMoment + 1)); if(realloced)... * refPage = realloced; else ...处理失败...... } 或`addLine`可以返回新页面。 实际上我会有一个`struct page`,里面有一行`Line * lines`和一个 `int count`。事实上,因为通常更好地将 mallocated成倍增加,它会有一个'int count`和 一个`int limit`。 - Chris" mmm,奇妙的malloc" Dollin Hewlett-Packard Limited注册号: 注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰 Any comments why char **page doesn''t reallocate#include <stdlib.h>voidadd(char **page,char *line,int n){char **temp;if(temp=realloc(page,sizeof(char *)*(n+1))){//malloc stuff later to add linepage=temp;}}intmain(void){char *s;char *line="test1";char *page[]={NULL};add(page,line,1);//free memory later} 解决方案Because you lied to realloc().Since page is not a NULL pointer, realloc() believes it represents ablock of space allocated by malloc(). It doesn''t so all bets are off.Because you lied to realloc().Since page is not a NULL pointer, realloc() believes it represents ablock of space allocated by malloc(). It doesn''t so all bets are off.Aha so char *page[]={NULL}; needs to be replaced with some mallocfirst.Back to drawing board.Because you lied to realloc().Since page is not a NULL pointer, realloc() believes it represents ablock of space allocated by malloc(). It doesn''t so all bets are off.Not only that, but whatever `add` does won''t affect the value ofmain''s `page` (C not having pass-by-reference). I suspect thatadd''s `page` should be a char***. I believe this is a case wheretypedefing pointers [or introducing structs] makes sense:typedef char *Line; /* a line is a pointer to a null-terminated seq of char */typedef Line *Page; /* a page is a pointer to a seq of Line */void addLine( Page *refPage, Line line, int linesInPageAtTheMoment ){Page realloced = realloc( *refPage, sizeof(Line) * (linesInPageAtTheMoment + 1) );if (realloced) ... *refPage = realloced;else ... deal with failure ...}Or `addLine` could return the new Page.In practice I''d have a `struct page` with a `Line *lines` and an`int count` in it. In fact, because it''s usually better tomallocated multiplicatively, it would have an `int count` andan `int limit`.--Chris "mmm, marvelous malloc" DollinHewlett-Packard Limited registered no:registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England 这篇关于*** glibc检测到*** ./test:realloc():无效的旧尺寸:0x00007fff49779070 ***的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 14:37