LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-07-2007, 11:50 AM   #1
ruh31
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Ubuntu
Posts: 51

Rep: Reputation: 15
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:

Code:
*** glibc detected *** ./test: realloc(): invalid old size: 0xbfddf548 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7eadb0c]
/lib/tls/i686/cmov/libc.so.6(realloc+0x106)[0xb7eafa66]
/lib/tls/i686/cmov/libc.so.6(realloc+0x3c)[0xb7eaf99c]
./test[0x8048483]
./test[0x804853d]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7e58050]
./test[0x80483b1]
======= Memory map: ========
[...]
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.

Thanks a lot!
 
Old 12-07-2007, 12:03 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
This runs okay for me:
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct sDot {int a; char b[32];} Dot;

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);	
  }
  return d;
}

int main(void) {
  Dot *D=NULL;
  int d=0;

  for (d=1; d<5; d++) CreateDot(D,d);
  free(D);
  return 0;
}
 
Old 12-07-2007, 12:10 PM   #3
ruh31
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Ubuntu
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks so very much!

The "magic" line was this one:

Code:
Dot *D=NULL;
My program works fine, too, if I initialize the Dot Pointer with NULL. Thanks again, also for the extremely fast answer!!!
 
Old 12-14-2007, 04:02 AM   #4
ruh31
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Ubuntu
Posts: 51

Original Poster
Rep: Reputation: 15
Hi!

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;
}
Maybe this helps somebody ...
 
Old 06-08-2011, 12:08 AM   #5
jaisonsteephen
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
Much more explanation please

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.
 
Old 06-08-2011, 05:39 PM   #6
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
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
 
  


Reply



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
*** glibc detected *** ./input: realloc(): invalid next size: custode Programming 4 05-04-2007 03:25 PM
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration