| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Memory matters |
On: 17 Oct 03 17:30:30 Bo Simonsen wrote to Roger Scudder:
> RS> Freeing a structure should be nothing more than one line...
> RS> free(PointerToMyStructure);
> Well what then I've
> PointerToMyStructure = (Structure*) malloc(sizeof(Structure));
> PointerToMyStructure->Var1 = (char*) malloc(strlen(some_other_string)+1);
> Then I need to free Var1 and the whole structure.
That is true.
> But if I've alot of elements, I need a linkedlist anyhow, and then
global vars, would not effort me.
> To free them I still need:
> for(tmpStruct=firstStruct; tmpStruct; tmpStruct=tmpStruct->next)
> {
> free(tmpStruct);
> }
> Hmmm...
Well, you don't want to try to access tmpStruct after you have free'd it.
I would do something more like this...
#include "sniptype.h" /* for macro FREE(x) */
myStruct = myStruct->head;
while ( myStruct->next != NULL )
{
tmpStruct = MyStruct;
myStruct = myStruct->next;
FREE(tmpStruct);
}
FREE(myStruct);
Since you have a large number of pointers in your structure you should
probably have a function to release the memory. Based on the little bit
that I have seen, I would put the structures and any functions that access
them in a separate file.
-Roger
--- Spinone v0.1.79 Win32
* Origin: Scudder's Point (1:261/38.11)SEEN-BY: 633/267 270 @PATH: 261/38 123/500 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.