Confused by Memory Management? Let's Figure it Out

In summary: When we have a function that allocates a tree of NODE's, do we deallocate it using the following?? void DeleteTree(NODE *tree) { if(tree == NULL) return; DeleteTree(tree->lc); DeleteTree(tree->rc); free(tree);} Yes. (Happy)Yes. (Happy)
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
mathmari said:
Hey! :eek:

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)
 
  • #3
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)
 
  • #4
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)
 
  • #5
I like Serena said:
You need another function to deallocate. (Wasntme)

How could I do that?? (Wondering)
 
  • #6
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)
 
  • #7
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)
 
  • #8
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)
 
  • #9
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)
 

Related to Confused by Memory Management? Let's Figure it Out

1. What is memory management?

Memory management is the process of managing and organizing a computer's memory, which is used to store data and instructions while a program is running. This involves allocating memory to different programs and processes, ensuring that they have enough memory to run efficiently, and releasing memory when it is no longer needed.

2. Why is memory management important?

Memory management is important because it helps optimize a computer's performance. By properly managing memory, a computer can efficiently run multiple programs and processes without running out of memory or slowing down. It also helps prevent errors and crashes caused by insufficient memory.

3. How does memory management work?

Memory management works by dividing a computer's memory into different sections, such as the stack, heap, and data segments. The operating system then allocates and deallocates memory to programs as needed, keeping track of which parts of memory are in use and which can be freed up.

4. What are some common memory management techniques?

Some common memory management techniques include garbage collection, which automatically frees up memory that is no longer being used, and virtual memory, which uses a combination of RAM and hard drive space to store data. Other techniques include memory paging, memory segmentation, and memory swapping.

5. What are some challenges with memory management?

One of the main challenges with memory management is memory fragmentation, which occurs when memory is divided into small, scattered sections, making it difficult to allocate large blocks of memory. Another challenge is memory leaks, which occur when a program does not release memory that is no longer needed, leading to a decrease in available memory and potentially causing errors or crashes.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • Computing and Technology
2
Replies
46
Views
4K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top