Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack vs Heap C++

Tags:

I just had a quick question about how stack variables versus heap variables work. As I understand it, stack variables are variables that after functions return will be deleted, and heap variables are persistent. But what I'm really confused about is how to allocate heap variables inside functions:

int MyObject::addObject(const char* a){
    MyObject newObject(a);
    return 0;
}

Say that I have a constructor for MyObject that is newObject(const char * a). Then in this function when it is called, after the return does the newly constructed newObject get deleted? If yes, how can you allocate to the heap within a function then? If not, how do you cleanup your memory later?

Furthermore, what exactly is the role of a destructor and when is it called?

like image 978
KWJ2104 Avatar asked Dec 11 '11 22:12

KWJ2104


People also ask

What is the difference between heap and stack?

The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application. Primitive local variables are only accessed the Stack Memory blocks that contain their methods.

Which is better stack or heap?

Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.

What is stack and heap in C programming?

stack : stores local variables. heap : dynamic memory for programmer to allocate. data : stores global variables, separated into initialized and uninitialized. text : stores the code being executed.

What is heap in C?

In certain programming languages including C and Pascal , a heap is an area of pre-reserved computer main storage ( memory ) that a program process can use to store data in some variable amount that won't be known until the program is running.


1 Answers

A constructor for the class MyObject is MyObject(), not newObject(). In your example, newObject is the name of your variable, not the constructor.

To allocate on the heap inside a function, you need to call the new operator:

int MyObject::addObject(const char* a){
    MyObject* newObject = new MyObject(a);
    //newObject is allocated on the heap

    //... Some more code...

    delete newObject;
    //you have to explicitly delete heap-allocated variables
    //if not, you get memory leaks
    return 0;
}

Your code:

MyObject newObject(a);

creates an automatic storage (stack) MyObject called newObject that lives until the scope it was declared in ends - i.e. closing }.

Furthermore, what exactly is the role of a destructor and when is it called?

To clean up memory the class allocated with new or new[] (or malloc) and it owns. It is called either when the object goes out of scope for automatic objects, or when you explicitly call delete for dynamic objects.

like image 51
Luchian Grigore Avatar answered Sep 20 '22 15:09

Luchian Grigore