|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
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"); } 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! 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! 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) 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! |
|
|||
I think it might be a problem in style you have reallocated a memory.
Code:
new_pointer = realloc(pointer, (pointer_size+100)*sizeof(int)); |
|
|||
Duh, what a stupid mistake. Thanks ephemera! This has, of course, fixed things perfectly and valgrind now reports zero errors. I think I'll make a habit of avoiding calloc in favour of malloc in future on account of this, so that always using sizeof becomes a habit.
|
Tags |
dynamic memory allocation, realloc, valgrind |
|
|