*** glibc检测到*** ./test:realloc():无效的旧尺寸:0x00007fff49779070 *** [英] *** glibc detected *** ./test: realloc(): invalid old size: 0x00007fff49779070 ***

查看:54
本文介绍了*** glibc检测到*** ./test:realloc():无效的旧尺寸:0x00007fff49779070 ***的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何评论为什么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);

//以后的免费记忆

}

解决方案

7月16日13:48,gert< gert.cuyk ... @ gmail.comwrote:


>

#include< stdlib.h>


void

add( char ** page,char * line,int n)

{

char ** temp;

if(temp = realloc(page,page sizeof(char *)*(n + 1)))

{

// malloc stuf f后来添加行

page = temp;

}


}


int

main(无效)

{

char * s;

char * line =" test1" ;;

char * page [] = {NULL};

add(page,line,1);

//以后的可用内存


}


任何评论为什么char **页面没有重新分配



因为你骗了realloc()。


由于page不是NULL指针,realloc()认为它代表了一个

malloc()分配的空间块。并不是所有的赌注都没有。


7月16日下午3:04,mark_blue ... @ pobox.com写道:


7月16日13:48,gert< gert.cuyk ... @ gmail.comwrote:


#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);

//以后的可用内存


}

任何评论为什么char **页面没有重新分配



因为你撒谎到realloc()。


由于页面不是NULL指针,因此realloc()认为它代表了malloc()分配的空间块b / b。所有赌注都没有。



Aha so char * page [] = {NULL};需要先替换一些malloc



回到绘图板。


< a href =mailto:ma ********** @ pobox.com> ma ********** @ pobox.com 写道:


7月16日13:48,gert< gert.cuyk ... @ gmail.comwrote:


> >
#include< stdlib.h>

void
add(char ** page,char * line,int n)

char ** temp;
if(temp = realloc(page,sizeof(char *)*(n + 1)))
{malloc stuff稍后添加行 page = temp;
}


int
main(无效)
{
char * s;
char * line =" test1";
char * page [] = {NULL};
add(page,line,1);
//以后的空闲内存

}


>任何评论为什么char **页面没有重新分配



因为你骗了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>

void
add(char **page,char *line,int n)
{
char **temp;
if(temp=realloc(page,sizeof(char *)*(n+1)))
{
//malloc stuff later to add line
page=temp;
}
}

int
main(void)
{
char *s;
char *line="test1";
char *page[]={NULL};
add(page,line,1);
//free memory later
}

解决方案

On 16 Jul, 13:48, gert <gert.cuyk...@gmail.comwrote:

>
#include <stdlib.h>

void
add(char **page,char *line,int n)
{
char **temp;
if(temp=realloc(page,sizeof(char *)*(n+1)))
{
//malloc stuff later to add line
page=temp;
}

}

int
main(void)
{
char *s;
char *line="test1";
char *page[]={NULL};
add(page,line,1);
//free memory later

}

Any comments why char **page doesn''t reallocate

Because you lied to realloc().

Since page is not a NULL pointer, realloc() believes it represents a
block of space allocated by malloc(). It doesn''t so all bets are off.


On Jul 16, 3:04 pm, mark_blue...@pobox.com wrote:

On 16 Jul, 13:48, gert <gert.cuyk...@gmail.comwrote:

#include <stdlib.h>

void
add(char **page,char *line,int n)
{
char **temp;
if(temp=realloc(page,sizeof(char *)*(n+1)))
{
//malloc stuff later to add line
page=temp;
}

}

int
main(void)
{
char *s;
char *line="test1";
char *page[]={NULL};
add(page,line,1);
//free memory later

}
Any comments why char **page doesn''t reallocate


Because you lied to realloc().

Since page is not a NULL pointer, realloc() believes it represents a
block of space allocated by malloc(). It doesn''t so all bets are off.

Aha so char *page[]={NULL}; needs to be replaced with some malloc
first.
Back to drawing board.


ma**********@pobox.com wrote:

On 16 Jul, 13:48, gert <gert.cuyk...@gmail.comwrote:

>>
#include <stdlib.h>

void
add(char **page,char *line,int n)
{
char **temp;
if(temp=realloc(page,sizeof(char *)*(n+1)))
{
//malloc stuff later to add line
page=temp;
}

}

int
main(void)
{
char *s;
char *line="test1";
char *page[]={NULL};
add(page,line,1);
//free memory later

}

>Any comments why char **page doesn''t reallocate


Because you lied to realloc().

Since page is not a NULL pointer, realloc() believes it represents a
block 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 of
main''s `page` (C not having pass-by-reference). I suspect that
add''s `page` should be a char***. I believe this is a case where
typedefing 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 to
mallocated multiplicatively, it would have an `int count` and
an `int limit`.

--
Chris "mmm, marvelous malloc" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


这篇关于*** glibc检测到*** ./test:realloc():无效的旧尺寸:0x00007fff49779070 ***的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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