Freeing Memory

Started by
17 comments, last by Noobster 21 years ago
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.
Advertisement
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!!!!!!!!
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.
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);<br><br><br><br><br><br>-Frank<br><br> </i>
-----------------------Who Flung Poo?
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]);  


[edit] 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]
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.
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?
maybe i have bad ram
You could [should] just use C++ and use ''new''.
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);  

"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
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?
maybe i have bad ram


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);<br><br>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. <br><br>the code would become<br><br><br>int *MyList[8];<br>for (int i = 0; i < 8; i++)<br>{<br> MyList = NULL;<br>}<br>………<br>………<br>………<br>for (int i = 0; i < 8; i++)<br>{<br> if (MyList != NULL)<br> free(MyList);<br><br> MyList = (int*)calloc(…);<br>}<br>………<br>………<br>………<br>for (int i = 0; i < 8; i++)<br>{<br> if (MyList != NULL)<br> free(MyList);<br> MyList = NULL;<br>}<br><br>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. </i>

Everything is better with Metal.

This topic is closed to new replies.

Advertisement