2

I have a question regarding the memory management in C++. As far as I know, there is no need to deallocate memory in Java, since unused objects will be removed by the JVM garbage collector at some time. My point is, if I forgot to free memory in C++, the used memory addresses will be occupied until I restart the machine and the data in the memory get lost? For example, on the code below I have a simple linked list and you can observe that I do not free memory(which is commented in the destructor):

#include <iostream>
using namespace std;

typedef struct Node{
Node* next;
int id;
} *ListPtr;
class LinkedList{
public:`ListPtr header;

LinkedList(){
    header = NULL;
}
~LinkedList(){
    /*
     ListPtr a = header,b;
    while(a != NULL)
    {
        b = a;
        a = a -> next;
        delete b;
    }
    delete a,b;
    cout << "Memory freed!"<< endl;
    */
}

public: void Insert(){
    ListPtr new_element = new Node;
    new_element -> next = NULL;

    if(header == NULL){
        header = new_element;
        header -> id = 0;
    }
    else{
        new_element -> next = header;
        new_element -> id = header -> id + 1;
        header = new_element;
    }
}

public: void Print(){
    ListPtr curr = header;
    while(curr != NULL){
             cout << "[" << &curr -> id << "]" << "-->";
        curr = curr -> next;
    }
    cout << "[NULL]"<<endl;
}};

int main(){
LinkedList list;
list.Insert();
list.Insert();
list.Insert();
list.Insert();
list.Insert();
list.Print();
return 0;
}

Does it mean that those memory addresses will be occupied until I turn the machine off? What happens with variables such as integers after the execution of a program is done? Can I free them?

The Output for the program is: [0x8622ac]-->[0x86229c]-->[0x86228c]-->[0x86227c]-->[0x8615bc]-->[NULL]

Igor Lopes
  • 56
  • 1
  • 8
  • possible duplicate of [What REALLY happens when you don't free after malloc?](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – Rakholiya Jenish Jul 06 '15 at 11:09
  • If you start by using the Standard containers - e.g. `std::string`, `std::vector`, `std::set` - they'll manage and release memory for you automatically if you store elements "by value". When you must track or otherwise handle elements using pointers, prefer "smart pointers" which can enforce an ownership/lifetime model and avoid leaks: in particular, the C++11 Standard library provide `std::unique_ptr` and `std::shared_ptr` which are very useful. – Tony Delroy Jul 06 '15 at 11:56

3 Answers3

12

It depends on the operating system. Most modern operating systems keeps track of allocated memory per process, so when the process exits all memory allocated by the process should be released.

However, not releasing resources after you're done with them will lead to leaks, in the case of memory you will have memory leaks. And for long-running processes, those leaks can accumulate, until all resources are used up.


Even when the memory and other resources are released automatically on process termination, it's often still considered good style to explicitly release allocated resources before exiting the process.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Unless you are using some kind of stripped down real-time operating system, it is extremely unlikely that the system cannot recover all the memory when the process exits.

That said, it is a good practice to not count on that behavior. For example, you could include counts of allocated objects in your constructors/destructors. If they all go to zero before the program exits, you have a good check against memory leaks. If those counts are non-zero, you have a memory leak somewhere.

user3344003
  • 20,574
  • 3
  • 26
  • 62
1

C++ does not have garbage collector like Java and it is programmers duty to free dynamically allocated memory. Operating system clears the memory that was allocated for a application when the application is terminated but there are specific cases where deallocation must be performed by the programmer otherwise it will cause memory leak.

#include <iostream>
using namespace std;
class test {
    public:
    int cl;
};

int main()
{
  test *p;
  p=new(test);
  p->cl=5;
  return 0;
}

When the above program terminates it will free the pointer p but will not free the integer pointed by p though that integer is no longer accessible.

Farsan Rashid
  • 1,460
  • 4
  • 17
  • 28