What is the Difference - Memory Allocation Wise - between Object Mat(args); and Object *Mat = new Mat(args); in C++? [duplicate]

StackOverflow https://stackoverflow.com/questions/12550482

Question

Possible Duplicate:
Proper stack and heap usage in C++?
Heap vs Stack allocation

I'm trying to understand why a Library I ported from Java to C++ (long and arduous editing work after using a converter) doesn't free up memory and just explodes the Virtual Memory till crash. Obviously this has to do with Java having a GC and C++ not - and the algorithms are pretty straight converted to C++.

So here's my question. Where and how do I delete allocated memory (Free it)? When I have:

Matrix *mat = new Matrix(args);

I obviously need to end the scope with a delete mat;. Can I avoid this? Would using Matrix mat(args); be better? in terms of Memory Allocation and freeing? Or using Matrix mat = Matrix(args)?

Was it helpful?

Solution

Where and how do I delete allocated memory? (Free it)

In most cases, if you coded things properly, you don't have to explicitly free or de-allocate any memory. Either use automatic allocation, or smart pointers. Otherwise, the answer to where is rather unsatisfying: wherever you need to, depending on your program. The answero to how is simpler: with delete (or delete[] for dynamically allocated arrays).

... I obviously need to end the scope with a delete mat; Can I avoid this?

Yes, if you use automatic allocation:

Matrix mat(args);

As a general rule, you should only use dynamically allocated objects if you really need to. If you don't know if you really need to, then you probably don't.

... Or using Matrix mat = Matrix(args);

This performs a copy initialization, and in this context provides no advantages over Matrix mat(args);. The RHS of the expression is a temporary Matrix object, which gets used to copy construct the LHS mat.

Here is a recent related post.

OTHER TIPS

Second option is better if you do not want the object to live beyond the scope in which you create it.

It automatically deallocates your object once the scope ends, unlike dynamic memory option where you have to explicitly remember to delete the object.

Good Read:

Why should C++ programmers minimize use of 'new'?

You really need to understand how memory allocation works in C/C++.
I suggest you start off with a book about it, because the question implies you don't have lots of experience with C++.
To the matter at hand:
Matrix *mat = new Matrix(args); Here you create a Matrix object on the heap, meaning it is not bound to scopes (the pointer still does, of course) and it's your responsibility to free it using delete.
Matrix mat(args); - Here you create an object on the stack, and it will be destructed when it's scope ends, this is not available in Java except for primitive types.
As for performance - creating objects on the stack is not only safer (auto destruct) but also much faster - your stack memory is preallocated in addition to the fact there's no need to search for a free block.
When creating memory from the heap - your process ends up asking the virtual memory manager for memory, causing a huge performance hit.
Having said that - there is a time and place for any type of allocation - and you should use whatever is better for your own code - there is no 'right way' of doing this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top