Jump to content
  • Advertisement

Archived

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

Noobster

Freeing Memory

This topic is 5714 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
--
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Did you do anything with the memory in between?

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites

  • 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!