Confused by Memory Management? Let's Figure it Out

Click For Summary

Discussion Overview

The discussion revolves around memory management in programming, specifically focusing on how to properly deallocate memory for data structures at the end of a program. Participants explore various methods for freeing allocated memory, including the use of functions to handle deallocation for different data structures like lists and trees.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants suggest iterating through all data structures and using free() for anything allocated with calloc() or malloc().
  • There is a proposal to create a separate function for deallocation rather than calling the allocation function again.
  • A participant provides an example of a function to deallocate a linked list, questioning if this approach is sufficient for all data structures.
  • Another participant explains that structures not allocated with dynamic memory do not require deletion, as they are automatically managed by the system.
  • There is a discussion about the different ways structures can be created, including global variables, local stack variables, and dynamic variables.
  • A participant inquires about the correct method to deallocate a tree of nodes, suggesting a recursive approach.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding memory management, with some agreeing on the need for separate deallocation functions while others question the sufficiency of their approaches. The discussion includes multiple competing views on how to handle different data structures and their deallocation.

Contextual Notes

Participants reference specific functions and data structures, but there are unresolved details regarding the implementation of deallocation functions for various data types, particularly trees.

Who May Find This Useful

Programmers and students interested in memory management, particularly in languages that require manual memory allocation and deallocation, may find this discussion relevant.

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)
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
86
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K