DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #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
  #2   (View Single Post)  
Old 31st August 2008
Tom Tom is offline
Port Guard
 
Join Date: Aug 2008
Location: Poland
Posts: 22
Default

I think it might be a problem in style you have reallocated a memory.

Code:
new_pointer = realloc(pointer, (pointer_size+100)*sizeof(int));
Regards.
Reply With Quote
  #3   (View Single Post)  
Old 31st August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

Quote:
Originally Posted by Silverleaf View Post
new_pointer = realloc(pointer, pointer_size+100);
new_pointer = realloc(pointer, (pointer_size+100) * sizeof(int));
Reply With Quote
  #4   (View Single Post)  
Old 31st August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

^
Reply With Quote
  #5   (View Single Post)  
Old 1st September 2008
Silverleaf Silverleaf is offline
Real Name: Luke Maurits
New User
 
Join Date: Aug 2008
Location: Adelaide, Australia
Posts: 2
Thumbs up

Quote:
Originally Posted by ephemera View Post
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.
Reply With Quote
  #6   (View Single Post)  
Old 1st September 2008
anemos's Avatar
anemos anemos is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Ελλάδα
Posts: 53
Default

Quote:
The Following User Says Thank You to ephemera For This Useful Post:
Silverleaf (Today)
Doesn't Tom deserve a thank you as well?
Reply With Quote
  #7   (View Single Post)  
Old 1st September 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

yeah, Tom deserves the thanks. (we posted at the same time)
Reply With Quote
Reply

Tags
dynamic memory allocation, realloc, valgrind

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:14 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick