Jump to content
  • Advertisement
Sign in to follow this  
Madster

crash on realloc?

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I'm getting a crash on realloc. I'm using SDL and the parachute isn't deployed, so it isn't a segfault. Are there any limitations on realloc? I realise that I'm probably overwriting over a buffer's limits somewhere, but still it strikes me as strange that it's crashing in the call to realloc, since at that moment it shouldn't matter how i'm dealing with the buffers Are there memory limits inherent to realloc? I'm only going around 250Mb of total data. thanks.

Share this post


Link to post
Share on other sites
Advertisement
realloc should return 0 if anything goes wrong, in which case the original pointer you gave it will still be intact.

Your problem is probably due to either buffer under/overflow, the pointer wasn't initialized, it's already been freed, or you didn't get the memory from malloc, calloc, or realloc.

It's probably buffer overflow, it's somewhat easy to mess up it's internal book keeping that way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Madster
I realise that I'm probably overwriting over a buffer's limits somewhere
Bingo! There's your most probable cause.

At the risk of sounding like a broken record, I suggest making good use of asserts, especially when it comes to accessing arrays etc.

Share this post


Link to post
Share on other sites
i use realloc out of habbit just incase you didnt free something and malloc something that wasnt freed will crash

realloc keeps the original pointer unless the space wasnt big enough and it moves the entire memory segment (slow)
somewhere else

unless you are continuously enlarging or shrinking something i would just go malloc free

and realloc something that has been freed doesnt cause an error, it simply behaves as malloc

you may have realloc in a continuous loop and may be calling it too much over time, try just making a really big pointer and waiting till your done with it to mess with free or realloc, putting realloc in a loop does cause errors misteriously, its supposed to work but sometimes it doesnt


Share this post


Link to post
Share on other sites
i think it may be because calling it over and over again real fast doesnt give the operating sstem long enough to finish marking that previously used segment of ram as free, hince using up all the ram, give it time to free the memory

Share this post


Link to post
Share on other sites
Quote:
Original post by FreeTutorialNewbie
i think it may be because calling it over and over again real fast doesnt give the operating sstem long enough to finish marking that previously used segment of ram as free, hince using up all the ram, give it time to free the memory


That's complete and utter nonsense.

Quote:
Original post by FreeTutorialNewbie
and realloc something that has been freed doesnt cause an error, it simply behaves as malloc

you may have realloc in a continuous loop and may be calling it too much over time, try just making a really big pointer and waiting till your done with it to mess with free or realloc, putting realloc in a loop does cause errors misteriously, its supposed to work but sometimes it doesnt


And so is that.

Share this post


Link to post
Share on other sites
Function: realloc

Synopsis

#include <stdlib.h>
void *realloc(void *ptr, size_t size);
Description

The realloc function changes the size of the object pointed to by ptr to the size specified by size. The contents of the object shall be unchanged up to the lesser of the new and old sizes. If the new size is larger, the value of the newly allocated portion of the object is indeterminate. If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined. If the space cannot be allocated, the object pointed to by ptr is unchanged. If the realloc function returns a null pointer when size is zero and ptr is not a null pointer, the object it pointed to has been freed.

Returns

The realloc function returns either a null pointer or a pointer to the possibly moved allocated space. If the object has moved, ptr is a pointer that refers to freed space.

Share this post


Link to post
Share on other sites
Quote:
Original post by FreeTutorialNewbie
Otherwise, if ptr does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!