jeudi 30 juin 2016

object created in function, is it saved on stack or on heap?


I am using c++ specifically: when I create an object in a function, will this object be saved on the stack or on the heap?

reason I am asking is since I need to save a pointer to an object, and the only place the object can be created is within functions, so if I have a pointer to that object and the method finishes, the pointer might be pointing to garbage after. --> if I add a pointer to the object to a list (which is a member of the class) and then the method finishes I might have the element in the list pointing to garbage.

so again - when the object is created in a method, is it saved on the stack (where it will be irrelevant after the function ends) or is it saved on the heap (therefore I can point to it without causing any issues..)?

example:

class blah{ 
private:
    list<*blee> b;
public:
    void addBlee() {
        blee b;
        blee* bp = &b;
        list.push_front(bp);
    }
}

you can ignore syntax issues -- the above is just to understand the concept and dilemma...

Thanks all!


Aucun commentaire:

Enregistrer un commentaire