*** 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; } |
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 Hope this helps. |
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). |
Quote:
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. |
Quote:
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. |