Confused by Memory Management? Let's Figure it Out

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 3K views
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)
 
Physics 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)
 
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)
 
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)
 
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)