Won't get realloc to work - glibc: invalid old size
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Won't get realloc to work - glibc: invalid old size
Hi!
I am having quite some trouble getting a very simple C program to work right. The line that makes problems is the one with the realloc statement. The output I get is this one:
The program itself is something like this: (I made it a little bit shorter for presentation purposes)
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct sDot {...} Dot;
int CreateDot(Dot *D, int d) {
if (d==1) D=(Dot *)malloc(d*d * sizeof(Dot));
else D=(Dot *)realloc(D,d*d*sizeof(Dot));
[...]
}
int main(void) {
Dot *D;
int d;
for (d=1; d<5; d++) CreateDot(D,d);
return 0;
}
I already looked in this forum and on the Internet for possible solutions, but either I have not tried hard enough (which I can't believe) or it is quite a challenge finding something. I would appreciate it a lot, if somebody could help me.
Just wanted to add a few comments for those working on similar structures: After the program compiled, there was another problem I had.
In "CreateDot", I allocated memory for the variable D and filled D with values. Then I did some actions with D in the main function and came back to "CreateDot" to allocate more space and fill the new fields. However, as I found out after some trouble-shooting, the values of D was not present any more after leaving function "CreateDot". I solved the problem as follows (just the relevant parts in addition to my previous posts):
Code:
int CreateDot(Dot **D, int d) {
if (d==1)
*D=malloc(d*d * sizeof(Dot));
if(d>1)
*D=realloc(D,d*d*sizeof(Dot));
if(*D==NULL)
{
perror("error allocating D");
exit(1);
}
// Fill *D with values
return d;
}
int main(void) {
Dot *D=NULL;
int d=0;
for (d=1; d<5; d++) {
CreateDot(&D,d);
// Do something with D
}
free(D);
return 0;
}
Hi,
I know only the basic C, C++ and java. So please anybody give me a brief descriptionof hexadecimal values in memory map(information from wikipedia-in case of native debugger programs, it is mapping between loaded executable/library files ).
thanks and regards
jaison steephen
Last edited by jaisonsteephen; 06-09-2011 at 12:05 AM.
There is a cleaner way of doing this, which is to make CreateDot() return the pointer to the structure it creates. I have changed the parameter to D1 to avoid confusion, but it is not necessary. In effect, D1 acts as a local variable initialised to the value of D.
Dot * CreateDot(Dot *D1, int d)
{
if (d==1) D1=malloc(d*d * sizeof(Dot));
if (d>1) D1=realloc(D,d*d*sizeof(Dot));
return D1;
}
int main(void) {
Dot *D=NULL;
int d=0;
for (d=1; d<5; d++) {
D = CreateDot(D,d);
// Do something with D
}
free(D);
return 0;
}
Note that your original problem was precisely because D was not altered by the call of CreateDot(), so that at the second call realloc() was passed an invalid pointer: had D received the pointer delivered by malloc() there would have been no problem.
Last edited by Peverel; 06-08-2011 at 05:40 PM.
Reason: clarity
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.