Archived

This topic is now archived and is closed to further replies.

Freeing Memory

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

Recommended Posts

Ok, i have an array of 8 pointers int *MyList[8]; and i use calloc to allocate memory for those pointers for (int i = 0; i < 8; i++) MyList = (int*)calloc(...); but when i try to free the memory i get these strange errors that i''ve never seen before for (int i = 0; i < 8; i++) free(MyList[i]); anyone have any ideas what im doin wrong?

Share on other sites
What strange errors do you get?

Share on other sites
Debug Error!

Program path to program

DAMAGE: after Normal Block (#37) at some memory address

[edited by - Noobster on March 26, 2003 10:46:05 AM]

Share on other sites
--
int *MyList[8];

and i use calloc to allocate memory for those pointers
for (int i = 0; i < 8; i++)
MyList = (int*)calloc(...);

but when i try to free the memory i get these strange errors that i''ve never seen before
for (int i = 0; i < 8; i++)
free(MyList);
--
Why would you have a loop when allocating the memory? Take that out, or don''t use calloc. I''ve never used calloc, so I''m not sure exactly how it works. Why do you have "int i" in your loops? That''s illegal in C (though not in C++), but redefining variables like that is still illegal in C++ as far as I know.

Try this:
#define ELEMENTS 8

int **MyList;
MyList = (int *) calloc(ELEMENTS, sizeof(int));

However, since I''ve never used calloc I''d just do:
#define ELEMENTS 8
int i;
for (i = 0; i < ELEMENTS; ++i) {
MyList[i] = (int *) malloc(sizeof(int));
memset(MyList[i], 0, sizeof(int));
}

..do some stuff..

for (i = 0, i < ELEMENTS; ++i) {
free(MyList[i]);
}

Share on other sites
Did you do anything with the memory in between?

That error usually means you wrote past the end of your allocated memory.

Share on other sites
The error occurs when i try to free the memory, if i take the free statement out it works fine.

Share on other sites
If you take the free out you''re creating a memory leak which isn''t fine.

In your code you don''t appear to be doing anything obviously wrong. My only guess is you''re writing past the end of one of those chunks of memory you allocated. If you do nothing besides get the memory and then free it, does it crash?

Share on other sites
Original post by Noobster
quote:

and i use calloc to allocate memory for those pointers
for (int i = 0; i < 8; i++)
MyList[i] = (int*)calloc(...);

Why are you reallocating memory in a for loop? Your int *MyList[8] already set aside memory for you. You don''t need that for loop to set aside more memory. You are allocating memory twice.

And I''m not sure how the free function works, but I imagine there is probably an overloaded version that takes the variable, along with how much space to deallocate. Seach MSDN or your help files for the different free functions.

Share on other sites
It sets aside space for the pointers, but those pointers are pointing to who knows what. I believe the code he posted is fine.

Share on other sites
I really don''t think calloc will work how he''s using it. Either way, it shouldn''t be in a loop. calloc sets aside memory for an array of objects, you shouldn''t be using it in a loop unless your array has more than two dimensions.

I also don''t *think* calloc is supposed to be used with a two dimensional array with one dimension being static. I think you need a dynamic array (void **blah, not void *blah[1]) I could most definitely be wrong, though.

Either way, calloc is being used incorrectly. You should be using malloc in a loop.

Share on other sites
Actually, maybe it''d work right if you did:

&MyList = calloc(8, sizeof(int));

Give it a shot if you''re bent on using calloc.

Also, calloc doesn''t reallocate memory. It allocates memory for an array. realloc() reallocates memory.

Share on other sites
AP #1: calloc calls malloc. You''re allowed to make 1d arrays with malloc, calloc makes it so that you don''t have to think (even though it''s just a simple multiply.)

AP #2: NOOOOOOOOOOOOOOOO!!!!!!!!! DON''T EVER DO THAT!!!!!!!!

Share on other sites
int *MyList[8];

Try this....

for (int i = 0; i < 8; ++i)
MyList = (int*)calloc(...); // You forgot to index it when
//you allocated the memory.

for (i = 0; i < 8; ++i)
free(MyList[i]);

-Frank

Share on other sites
Sure he did, the forum just though that the [i] meant he wanted italic text.

    int *MyList[8];for (int i = 0; i < 8; i++) MyList[i] = (int*)calloc(100, sizeof(int)); // I have no idea how many elements you want so I'm guessing.for (int i = 0; i < 8; i++) free(MyList[i]);

 formatting. And yes, the forum is alive and thinks. It watchs your every move so don't let your guard down for a second.

[edit2] that didn't work. curse my teachers browser. I have no idea if that will look right or not.

[edited by - smart_idiot on March 26, 2003 12:12:09 PM]

Share on other sites
Hey i just noticed this message in the debug tab when im compiling...

memory check error at 0x00790088 = 0x01, should be 0xFD

maybe that has something to do with it?

Share on other sites
You could [should] just use C++ and use ''new''.

Share on other sites
As was mentioned earlier in this post, there is no reason to use calloc , free or either FOR loop. You have already allocated space for an array of 8 integer pointers with
 int *MyList[8];

You only need calloc and free if you are allocating the array at run time. i.e.

  int *MyList;MyList = calloc(8, sizeof(*int) ) ; // might need int* cast here//  do your stufffree(MyList);

Share on other sites
"zackriggle You could [should] just use C++ and use ''new''."

Why all people "should" use C++, what means this C++ stupid fever in ALL gamedev forums,Why not we can use C, Why we must to make all with classes blah blah blah.

I hate the posts anti-C and anti-ASM

Share on other sites
quote:
Original post by Noobster
Hey i just noticed this message in the debug tab when im compiling...

memory check error at 0x00790088 = 0x01, should be 0xFD

maybe that has something to do with it?

If you get those errors it''s because YOU are doing something radically wrong. It''s commonly called as memory stamping, where bits of the memory that are not supposed to change are being overwritten (like indexing and writing too far in an array or with a bad index).

What you did wrong

int *MyList[8];
for (int i = 0; i < 8; i++)
MyList = (int*)calloc(...);
for (int i = 0; i < 8; i++)
free(MyList);

you try to free the MyList[8] array, (and 8 times!), where it is not possible. Mylist[8] is allocated statically. It like you were doing

int MyList[8];
MyList = (int*)calloc(...);
free(MyList);

the only dynamic memory part are the ELEMENTS of MyList[8], not the array. And also, you overwrite the MyList array by doing a calloc() on it, which is also a bad idea.

you should do

int *MyList[8];
for (int i = 0; i < 8; i++)
MyList = (int*)calloc(...);
for (int i = 0; i < 8; i++)
free(MyList[i]);

you should always call a free() in respect to an identical calloc(), and you should never call a calloc() before freeing the pointer beforehand (if it''s not NULL), to avoid memory leaks. use NULL as much as you can, it helps a lot for debugging.

the code would become

int *MyList[8];
for (int i = 0; i < 8; i++)
{
MyList[i] = NULL;
}
.........
.........
.........
for (int i = 0; i < 8; i++)
{
if (MyList[i] != NULL)
free(MyList[i]);

MyList[i] = (int*)calloc(...);
}
.........
.........
.........
for (int i = 0; i < 8; i++)
{
if (MyList[i] != NULL)
free(MyList[i]);
MyList[i] = NULL;
}

If you use C++, I would recommend to have a look at the memwatch library. a simple new/delete operator overiding that checks for memory leaks and stamping.

• Forum Statistics

• Total Topics
628696
• Total Posts
2984265

• 18
• 9
• 13
• 13
• 11