LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   *** glibc detected *** ./input: realloc(): invalid next size: (https://www.linuxquestions.org/questions/programming-9/%2A%2A%2A-glibc-detected-%2A%2A%2A-input-realloc-invalid-next-size-550683/)

custode 05-02-2007 11:22 AM

*** glibc detected *** ./input: realloc(): invalid next size:
 
hi,

in the program below i tried to allocate memory for pointers, it reads chars and put in data than put data in person that it points data and person has many datas,it's an array of pointers to datas,then i put the persons in all array that contains pointers to persons.

the problem is i am not sure where to free my allocated memories. the error message from glibc is
*** glibc detected *** ./input: realloc(): invalid next size: 0x0804a018
i think its about memory leak.Am i right?

i am new in here if i posted somewhere wrong sorry,and i'll be happy if you tell me anymhere else to ask that question.
thanks

my program is:

#include<stdio.h>
#include<stdlib.h>

int main()
{
char *first,*data,**pfirst,**person,ch,***afirst,***all;
int ndata,nch,nper;

afirst=(char***)malloc(sizeof(char**));
all=afirst;
nper=1;

while(1)
{
scanf("%c",&ch);
if(ch=='\n')break;

ndata=1;
pfirst=(char**)malloc(sizeof(char*));
person=pfirst;


while(1)
{

first=(char*)malloc(sizeof(char));
data=first;
nch=1;
while(ch!=' '&&ch!='\n')
{

*first=ch;
data=(char*)realloc(data,++nch);
first=data+nch-1;
scanf("%c",&ch);
}

*first='\0';

*pfirst=data;
if(ch=='\n')break;
person=(char**)realloc(person,++ndata);
pfirst=person+ndata-1;
scanf("%c",&ch);
}

*afirst=person;
all=(char***)realloc(all,++nper);
afirst=all+nper-1;
}

printf("%s\n",all[0][0]);
return 0;
}

wjevans_7d1@yahoo.co 05-02-2007 09:26 PM

It's not a memory leak problem. Memory leaks are situations where you allocate memory that you do not free when you're finished with it. The only bothersome symptom of a memory leak is that you use more and more memory.

No, what you probably have is a more frustrating problem. You're probably stomping on memory you shouldn't be stomping on. The most common causes of this are wandering outside the proper bounds for an array, using memory you've already freed, and freeing a block of memory more than once (after allocating it just once).

For help with this frustrating problem, pick one of these:

Code:

http://directory.fsf.org/ElectricFence.html
http://valgrind.org/

I've only used the first one myself. It has saved me oodles of time with this sort of problem.

Hope this helps.

xhi 05-03-2007 01:41 PM

welcome to LQ. when you post code you should wrap it in [code][/code] tags.. this preserves the formatting. you can edit the post you made and add the code tags

as for your problem, if you load your program into gdb, and step through it it should lead you to the offending line(s).

wjevans_7d1@yahoo.co 05-04-2007 02:45 PM

Quote:

if you load your program into gdb, and step through it it should lead you to the offending line(s).
Not in this case, not without additional help, if it's a problem with stomping on memory you shouldn't be stomping on, because the stomping is done at some unknown point in the program, and the error is usually discovered way later.

The purpose of the packages I recommended is to provide that additional help, and to cause your program to fail at that earlier point. That's where gdb will help you.

xhi 05-04-2007 03:25 PM

Quote:

Originally Posted by wjevans_7d1@yahoo.co
Not in this case, not without additional help, if it's a problem with stomping on memory you shouldn't be stomping on, because the stomping is done at some unknown point in the program, and the error is usually discovered way later.

The purpose of the packages I recommended is to provide that additional help, and to cause your program to fail at that earlier point. That's where gdb will help you.

eh, maybe so. i didnt run it through a debugger and check.. that was just a 2 second evaluation based on the fact that there is no check on the pointer realloc returns, and that everything is running in a questionable looping structure with incrementing size for reallocs..

anyhow. valgrind is good. theres also another mem manager out there i used a lot, Fluid Studios Memory Manager.. aka 'MMGR'


All times are GMT -5. The time now is 03:28 PM.