LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-02-2007, 11:22 AM   #1
custode
LQ Newbie
 
Registered: May 2007
Posts: 1

Rep: Reputation: 0
*** 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;
}
 
Old 05-02-2007, 09:26 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 05-03-2007, 01:41 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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).

Last edited by xhi; 05-03-2007 at 01:42 PM.
 
Old 05-04-2007, 02:45 PM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 05-04-2007, 03:25 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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'
 
  


Reply

Tags
invalid, realloc, size


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error running a C program, glibc detected *** free(): invalid next size (normal) mesh2005 Programming 3 11-08-2006 01:52 PM
*** glibc detected *** free(): invalid next size (normal): 0x0000000000503e70 *** vbreddy Programming 2 04-10-2006 06:27 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - General 2 03-03-2006 12:25 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - Software 1 03-02-2006 08:23 AM


All times are GMT -5. The time now is 01:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration