Sign in to follow this  

HEAP: Heap Free block

This topic is 4691 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

I'm attempting to make the memory for my data in my program dynamic. I have a data structure I'll call sObject. sObject is declared as sObject* Object so I can dynamically allocate Object as the number of objects I have grows and shrinks. Within sObject, I have a 2 dimentional array of floats, float** Data, which I also want to be dynamic. When I go to allocate new memory when I want to grow the size of Object, I do this:
sObject *temp;
temp = new sObject[NewSize];
On the new operation, I get a pop-up window with the following error:
User breakpoint called from code at 0x7c901230
In the debug window, it says:
HEAP[programname.exe]: HEAP:  Free Heap block 21f93b08 modified at 21f93b18 after it was freed
The last piece of code executed before this all happens is:
return HeapAlloc(_crtheap, 0, size);
from VC98\CRT\SRC\MALLOC.C Anyone know what I'm doing wrong and what I can do to fix this problem?

Share this post


Link to post
Share on other sites
Sounds like heap corruption. This probably occurred in an earlier point of the program. Your object allocation is probably fine. Go through earlier points in the program and check pointers and heap reading/writing. There is a really useful CRT debugging function that checks if the heap is corrupted (I forget exactly what it is, shouldn't be hard to find), try calling that before you allocate this memory and I bet it says that it's corrupted.

Share this post


Link to post
Share on other sites
You were correct, I was corrupting the heap stack earlier. This leads into asking how to properly allocate memory dynamically. Using the same example.

I've got a structure sObject that contains a 2 dimentional array of floats, a couple unsigned ints, and another structure I'll call Struct1. Struct1 contains another 2 dimentional array of floats and two more unsigned ints. I want the 2 dimentional array of floats in both Struct1 and sObject to be dynamically allocatable, as well as sObject itself. (sObject is declared in the main data structure of my program). So it looks something like:

typedef struct
{
float Data[SOME_NUMBER][SOME_NUMBER];
unsigned int X;
unsigned int Y;
} Struct1;

typedef struct
{
float Data[SOME_NUMBER][SOME_NUMBER];
unsigned int X;
unsigned int Y;

Struct1 MoreData;
} sObject;

typedef struct
{
// Main data structure
...

sObject Object[SOME_NUMBER];
} MainData;

I want to make Object dynamic, Data to be dynamic, and MoreData to be dynamic. How do I do this?

Share this post


Link to post
Share on other sites

This topic is 4691 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this