LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-17-2010, 05:12 AM   #1
neelpatel
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Rep: Reputation: 0
Subsequent Calls to Realloc Cause Memory Dump


I'm trying to write a C program that extends an array to any user inputed size.

Code:
if (arraysize ==  0) {
                        arraysize = (int) pos + 1;
                        a = (int *) calloc (arraysize,sizeof(int));
                        for (i = 0 ; i < arraysize ; i++ )
                                a[i] = -1;
                        a[pos] = val;
                } else if ( pos >= arraysize ) {
                        newarraysize = (size_t) pos + 1;
                        a = (int *) realloc (a,newarraysize);
                        if (a == NULL) {
                                printf("Cannot allocate memory\n");
                                free(a);
                                a = NULL;
                                exit(1);
                        }
                        for (i = arraysize ; i < newarraysize ; i++ )
                                a[i] = -1;
                        a[pos] = val;
                        arraysize = newarraysize;

User inputs integers pos and val. An example interaction:


Quote:
Enter a position:
1
Enter a value:
1
Another assignment (y or n)?
y

array[0] = [No value assigned]
array[1] = 1
Enter a position:
2
Enter a value:
2
Another assignment (y or n)?
y

array[0] = [No value assigned]
array[1] = 1
array[2] = 2
Enter a position:
3
Enter a value:
3
Another assignment (y or n)?
y

array[0] = [No value assigned]
array[1] = 1
array[2] = 2
array[3] = 3
Enter a position:
4
Enter a value:
4
Another assignment (y or n)?
y

*** glibc detected *** ./a.out: realloc(): invalid next size: 0x09d9d008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0x17aff1]
/lib/tls/i686/cmov/libc.so.6[0x1804d0]
/lib/tls/i686/cmov/libc.so.6(realloc+0xdd)[0x18085d]
./a.out[0x804873f]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126b56]
./a.out[0x8048541]
======= Memory map: ========
00110000-0024e000 r-xp 00000000 08:01 1066 /lib/tls/i686/cmov/libc-2.10.1.so
0024e000-0024f000 ---p 0013e000 08:01 1066 /lib/tls/i686/cmov/libc-2.10.1.so
0024f000-00251000 r--p 0013e000 08:01 1066 /lib/tls/i686/cmov/libc-2.10.1.so
00251000-00252000 rw-p 00140000 08:01 1066 /lib/tls/i686/cmov/libc-2.10.1.so
00252000-00255000 rw-p 00000000 00:00 0
005a5000-005c0000 r-xp 00000000 08:01 1183 /lib/ld-2.10.1.so
005c0000-005c1000 r--p 0001a000 08:01 1183 /lib/ld-2.10.1.so
005c1000-005c2000 rw-p 0001b000 08:01 1183 /lib/ld-2.10.1.so
00818000-00834000 r-xp 00000000 08:01 1035 /lib/libgcc_s.so.1
00834000-00835000 r--p 0001b000 08:01 1035 /lib/libgcc_s.so.1
00835000-00836000 rw-p 0001c000 08:01 1035 /lib/libgcc_s.so.1
00c83000-00c84000 r-xp 00000000 00:00 0 [vdso]
08048000-08049000 r-xp 00000000 08:01 153265 /home/neel/cs/lab05/exercise1/a.out
08049000-0804a000 r--p 00000000 08:01 153265 /home/neel/cs/lab05/exercise1/a.out
0804a000-0804b000 rw-p 00001000 08:01 153265 /home/neel/cs/lab05/exercise1/a.out
09d9d000-09dbe000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b7702000-b7703000 rw-p 00000000 00:00 0
b770f000-b7713000 rw-p 00000000 00:00 0
bfcdf000-bfcf4000 rw-p 00000000 00:00 0 [stack]
Aborted
The program dumps with that sequence of inputs everytime, but might dump an input before or after if different positions are requested. Interestingly, when I tested pos = 2000..2008, I got no dumps. So is realloc somehow trying to extend the array into bad space?

Full source is available on request.


Thanks in advance,
Neel
 
Old 02-17-2010, 05:28 AM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Quote:
Code:
                        a = (int *) realloc (a,newarraysize);
The size argument you pass to realloc is wrong. realloc expects the size in bytes. But an integer is not only one byte in size. Replace it with:
Code:
                        a = (int *) realloc (a,sizeof(int)*newarraysize);
Because the size of the memory associated with the array is too small, you're overwriting some of glibc's internal structures, that are used to keep track of the memory blocks. This results in the error.
 
1 members found this post helpful.
Old 02-17-2010, 05:32 AM   #3
neelpatel
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Original Poster
Rep: Reputation: 0
*hits self in head*

Thanks you so much. I've spent forever thinking that it just didn't like realloc to be called recursively and was wondering if it somehow was moving the a pointer around without freeing memory, or just generally memory leaking somehow.


Solved.
 
  


Reply


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
SLES 9 Memory Dump help djcrash1981 Suse/Novell 6 01-10-2010 06:43 AM
how to get memory info using system calls or files in linux richmike3 Linux - General 1 12-14-2009 06:17 AM
BASH - problem with subsequent calls to function with getopts dkrysak Programming 3 03-25-2009 04:28 PM
How Dump Live Process Memory kramer2718 Linux - General 2 12-08-2008 12:24 AM
Memory dump command? fanoliv Linux - General 1 03-29-2008 01:56 AM


All times are GMT -5. The time now is 01:22 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration