crash on realloc?

Started by
39 comments, last by SiCrane 18 years, 7 months ago
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.
Working on a fully self-funded project
Advertisement
Post the code.

ace
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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


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
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.
ok so half of my post are nonsense...
but am i not right about being able to use realloc on a freed pointer?
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.
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

This topic is closed to new replies.

Advertisement