MHB Confused by Memory Management? Let's Figure it Out

AI Thread Summary
To properly manage memory in a program, it is essential to deallocate all dynamically allocated data structures at the end of execution. This involves using the `free()` function on any memory allocated with `calloc()` or `malloc()`. A separate deallocation function should be created rather than calling the allocation function again. For example, a function to destroy a linked list would iterate through the nodes, freeing each one. Similarly, for tree structures, a recursive function can be implemented to free each node. It is important to note that structures not allocated dynamically do not require manual deallocation, as they are automatically managed by the system. Understanding the three ways structures can be created—global variables, local stack variables, and dynamic variables—is crucial for effective memory management.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

At the end of a program I have to delete all the data structures and to free all the allocated memory.

What am I supposed to do?? (Wondering)
 
Technology news on Phys.org
mathmari said:
Hey! :o

At the end of a program I have to delete all the data structures and to free all the allocated memory.

What am I supposed to do?? (Wondering)

Hi! (Smile)

Iterate through all data structures.
Anything that has been allocated with calloc() or malloc(), should be deallocated with free(). (Sweating)
 
I like Serena said:
Hi! (Smile)

Iterate through all data structures.
Anything that has been allocated with calloc() or malloc(), should be deallocated with free(). (Sweating)

For example, at a function ( Constitution() ) there is the following:

[m]plansys_t *plansystem = calloc(1, sizeof(plansys_t));[/m]So, do I have to write now a function, call the function Constitution() and write

[m]free(plansystem)[/m]

?? (Wondering)
 
mathmari said:
For example, at a function ( Constitution() ) there is the following:

[m]plansys_t *plansystem = calloc(1, sizeof(plansys_t));[/m]So, do I have to write now a function, call the function Constitution() and write

[m]free(plansystem)[/m]

?? (Wondering)

You should write a function, but I don't think it should call the function Constitution().
The function Constitution() allocates and initializes.
You need another function to deallocate. (Wasntme)
 
I like Serena said:
You need another function to deallocate. (Wasntme)

How could I do that?? (Wondering)
 
mathmari said:
How could I do that?? (Wondering)

Suppose we had a function that allocates a list of NODE's and initializes it with 1, 2, 3.

Then, to deallocate it, we would need something like:
Code:
void DestroyList(NODE *head) {
  while (head != NULL) {
    NODE *next = head->next;
    free(head);
    head = next;
  }
}
(Thinking)
 
I like Serena said:
Suppose we had a function that allocates a list of NODE's and initializes it with 1, 2, 3.

Then, to deallocate it, we would need something like:
Code:
void DestroyList(NODE *head) {
  while (head != NULL) {
    NODE *next = head->next;
    free(head);
    head = next;
  }
}
(Thinking)

I wrote two functions to deallocate the lists of asteroid_t and plansys_t.

By deallocating anything that has been allocated with calloc() or malloc(), with free(), have I deleted all the data structures and the whole allocated memory?? (Wondering)
 
mathmari said:
I wrote two functions to deallocate the lists of asteroid_t and plansys_t.

By deallocating anything that has been allocated with calloc() or malloc(), with free(), have I deleted all the data structures and the whole allocated memory?? (Wondering)

Yes. (Happy)
 
I like Serena said:
Yes. (Happy)

How have we deleted in that way the data structures?? (Wondering)
 
  • #10
mathmari said:
How have we deleted in that way the data structures?? (Wondering)

Structures that have not been alloc'ed do not need to be deleted.
It's automatic. (Mmm)

For reference, there are 3 ways that structures can be created.

  1. If you declare data outside all functions, it is a global variable, such as your StarS array.
    Anything there is created before your program even starts, and is destroyed after your program ends.
  2. If you declare data inside a function, it is a local stack variable, which is destroyed when the matching closing brace comes by.
  3. If you declare a pointer and want it to point to something, typically you allocate it with calloc/malloc, making it a dynamic variable.
    A dynamic variable is destroyed when you call free() on it.
(Nerd)
 
  • #11
I like Serena said:
Structures that have not been alloc'ed do not need to be deleted.
It's automatic. (Mmm)

For reference, there are 3 ways that structures can be created.

  1. If you declare data outside all functions, it is a global variable, such as your StarS array.
    Anything there is created before your program even starts, and is destroyed after your program ends.
  2. If you declare data inside a function, it is a local stack variable, which is destroyed when the matching closing brace comes by.
  3. If you declare a pointer and want it to point to something, typically you allocate it with calloc/malloc, making it a dynamic variable.
    A dynamic variable is destroyed when you call free() on it.
(Nerd)

I understand! Thank you very much! (Mmm)
 
  • #12
I like Serena said:
Suppose we had a function that allocates a list of NODE's and initializes it with 1, 2, 3.

Then, to deallocate it, we would need something like:
Code:
void DestroyList(NODE *head) {
  while (head != NULL) {
    NODE *next = head->next;
    free(head);
    head = next;
  }
}
(Thinking)

When we have a function that allocates a tree of NODE's, do we deallocate it using the following??

Code:
void DeleteTree(NODE *tree) {
     if(tree == NULL) return;
     DeleteTree(tree->lc);
     DeleteTree(tree->rc);
     free(tree);
}

(Wondering)
 
Back
Top