Thread: C realloc help
View Single Post
  #1   (View Single Post)  
Old 31st August 2008
Silverleaf Silverleaf is offline
Real Name: Luke Maurits
New User
 
Join Date: Aug 2008
Location: Adelaide, Australia
Posts: 2
Unhappy C realloc help

Greetings all,

I'm having a little bit of trouble doing my first dynamic memory allocations in C after a good 5 years. Python has spoiled me and this stuff is coming back to me slowly!

Here is a short and simple "toy" program which I believe captures the essence of the part of my real program which is causing problems:

Code:
#include<stdlib.h>
#include<stdio.h>

int main() {
        int *pointer = NULL;
        int *new_pointer = NULL;
        int pointer_size;
        int i;

        pointer = calloc(100, sizeof(int));
        pointer_size = 100;
        for(i=0;i<1000;i++) {
                if(i == pointer_size) {
                        new_pointer = realloc(pointer, pointer_size+100);
                        if(new_pointer == NULL) {
                                printf("Bad memory stuff!\n");
                                return -1;
                        } else {
                                printf("Good memory stuff!\n");
                                pointer = new_pointer;
                                pointer_size+=100;
                        }
                }
                pointer[i] = i;
                printf("Successfully stored integer %d!\n", i);
        }
        printf("All done!\n");
}
The basic idea is simply this - I start off with "pointer", which points to a chunk of memory suitable for storing 100 integers. I want to eventually store 1000 consecutive integers in a contiguous chunk of memory, but I don't know this in advance so I want to steadily grow that 100 int chunk up to 1000 in 100 integer intervals. I check the return value of "realloc" to make sure nothing goes wrong at any of these intervals.

When I compile the code above (with a simple "gcc realloctest.c -o realloctest", no optimisations or the like), everything seems to run smoothly. My output ends with:

Code:
Successfully stored integer 995!
Successfully stored integer 996!
Successfully stored integer 997!
Successfully stored integer 998!
Successfully stored integer 999!
All done!
and if I grep the output for the word "memory" I see 9 lines of "Good memory stuff!", suggesting that realloc is doing its job properly each time.

Now for the problematic part:

If I run this very same program through Valgrind (with "valgrind --leak-check=yes ./realloctest"), things do not run anywhere near as smoothly. Around the time of the first call to "realloc" I see this:

Code:
Successfully stored integer 95!
Successfully stored integer 96!
Successfully stored integer 97!
Successfully stored integer 98!
Successfully stored integer 99!
Good memory stuff!
==4191== Invalid write of size 4
==4191==    at 0x80484ED: main (in /home/luke/Research/CRTMarkvovModel/code/realloctest)
==4191==  Address 0x417a1b8 is not stack'd, malloc'd or (recently) free'd
Successfully stored integer 100!
Successfully stored integer 101!
Successfully stored integer 102!
Successfully stored integer 103!
Successfully stored integer 104!
Successfully stored integer 105!
Later on, before the second call to "malloc":

Code:
Successfully stored integer 195!
Successfully stored integer 196!
Successfully stored integer 197!
Successfully stored integer 198!
Successfully stored integer 199!

valgrind: m_mallocfree.c:210 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 106, hi = 8650752.
Probably caused by overrunning/underrunning a heap block's bounds.

==4191==    at 0x3801A40D: (within /usr/lib/valgrind/x86-linux/memcheck)
==4191==    by 0x3801A6FE: (within /usr/lib/valgrind/x86-linux/memcheck)
==4191==    by 0x38023C5F: (within /usr/lib/valgrind/x86-linux/memcheck)
==4191==    by 0x380027E9: (within /usr/lib/valgrind/x86-linux/memcheck)
==4191==    by 0x38039085: (within /usr/lib/valgrind/x86-linux/memcheck)
==4191==    by 0x3804D2C8: (within /usr/lib/valgrind/x86-linux/memcheck)

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable
==4191==    at 0x4023E8C: realloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==4191==    by 0x80484A6: main (in /home/luke/Research/CRTMarkvovModel/code/realloctest)
at which point the program terminates.

Can someone please advise me on what's going wrong, here? I am baffled by the supposed "illegal write" when storing the 101st integer. Valgrind claims that "Address 0x417a1b8 is not stack'd, malloc'd or (recently) free'd", but the presence of a "Good memory stuff!" immediately before that line suggests to me that the relevant address certainly should have been properly allocated.

Thanks in advance to anyone who can help me on this, if more information is needed to diagnose the problem, just ask!
Reply With Quote