C/C++ dynamic memory deallocation mechanism

Hi,
In c/c++
we allocate dynamic memory using malloc()/new() and deallocate using free()/delete().
If we manually create it we must manually call the deallocation function.

So, my question is why we have to do it manually ,
if dynamic memory has to be deallocated to avoid memory leak shouldn't it be automatic, I mean the compiler does so much for the programmers and the language has many "syntactical sugar" for common tasks, why isn't this automatic(i.e. the compiler adds necessary calls and etcetera) at the end of each function.

One reason it isn't automatic and built into the language could be if someone intentionaly wants to "leak" memory.So does this really has any application ?
Last edited on
So, my question is why we have to do it manually ,


We don't.


So, my question is why we have to do it manually ,
if dynamic memory has to be deallocated to avoid memory leak shouldn't it be automatic, I mean the compiler does so much for the programmers and the language has many "syntactical sugar" for common tasks, why isn't this automatic(i.e. the compiler adds necessary calls and etcetera) at the end of each function.


If you want to use a language that has a garbage collector then go and use one. C++ is for programmers who want control over things like that. Anyway, even if the language does have a GC it's not good practice to rely on it, as that may result in a sluggish application.
Common guys, you are misinterpreting what i mean i am not against c++ in any way nor am i asking for GC.


If we instantiate an object of a class then when it goes out of scope the object's destructor is called, so why not there could be a mechanism to delete a pointer as well as *the pointed memory which was automaticaly allocated* when it goes out of scope.
Last edited on
So, my question is why we have to do it manually

The point is that C++ gives developers the power to manage the memory as they like. Yes, you need to free up any memory you allocate, but C++ gives you the ability to choose exactly when, and where in the code, that happens. For example, you ask:

why isn't this automatic [...] at the end of each function.

What if I don't want my memory being freed at the end of my function? What if I want my function to allocate something for me, then pass it back to the calling code to use? C++ gives me that ability.

As ajh32 says, there are plenty of languages that do automatic garbage collection for you, but it can be very limiting to developers not to have control over it. Most developers of serious, large applications will tell you that automatic garbage collection can be a nightmare to work with.

Obviously, as a wise man once said, with great power comes great responsibility ;) In this case, that means the responsibility to free the memory yourself. However...

the language has many "syntactical sugar" for common tasks

... the language does indeed give you the syntactic sugar, in the form of smart pointers, though they're part of libraries rather than the actual language syntax itself. If you're using an implementation that conforms to the C++11 standard, they're right there in the standard library. If you're using an older standard, you're better off using the ones provided by the Boost libraries.

Go look them up up, because they're incredibly useful.
Last edited on
so why not there could be a mechanism to delete a pointer as well as *the pointed memory* when it goes out of scope.


There is. They're called "smart pointers" and all C++ programmers should be using them.

1
2
3
4
5
6
7
8
9
10
11
12
13
{
   std::unique_ptr<int> foo( new int );

   *foo = 5;

}  // foo automatically deleted here

{
   std::shared_ptr<int> bar( new int );

   *bar = 10;

}  // bar automatically deleted here 



The difference between unique_ptr and shared_ptr is that unique_ptr can only have on smart pointer object owning the data at once. Whereas shared_ptr has shared ownership (the pointed-to object remains alive until ALL shared_ptrs referring to it are destructed).


Regular pointers do not do this because there is no way to know whether or not the pointer is pointing to dynamically allocated memory or not... or whether that memory is still owned.
Ok , i got my answer , thanks all for wasting their time in this stupid question, i just didn't thought it with different perspectives but now you all made me realise that.
:)
Glad we could help :) This stuff isn't always obvious, and it's not often covered in depth by tutorials either, so don't feel bad!
Topic archived. No new replies allowed.