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!


Keep in mind following thing: the object is NEVER created in the heap (more correctly called 'dynamic storage' in C++) unless explicitly allocated on the heap using new operator or malloc variant.

Everything else is either stack/register (which in C++ should be called 'automatic storage') or aa statically allocated variable. An example of statically allocated variables are global variables in your program, variables local to the function which are declared static or static data members of classess.

You also need to very clear disntguish between a pointer and the object itself. In the following single line:

void foo() {
    int* i = new int(42);
}

int is allocated dynamically (on the heap), while pointer to that allocated int has an automatic storage (stack or register). As a result, once foo() exits, the pointer is obliterated, but the dynamically allocated object remains without any means to access it. This is called classic memory leak.


Heap is the segment where dynamic memory allocation usually takes place so when ever you explicitly allocate memory to anything in a program you have given it memory from the heap segment.

Obj yo = new Obj; //something like this.

Stack is the another segment where automatic variables are stored, along with information that is saved each time a function is called.

Like when we declare:

*int* variable;

It will be on the stack and its validity is only till a particular function exits, whereas variables, objects etc on the heap remain there till main() finishes.


void addBlee() {
    blee b;              // this is created on the stack
    blee* bp = &b;
    list.push_front(bp); // this is a pointer to that object
}  // after this brace b no longer exists, hence you have a dangling pointer

改为:

list.push_front(new blee);  // this is created on the heap and
                            // therefore it's lifetime is until you call
                            // delete on it`
链接地址: http://www.djcxy.com/p/13872.html

上一篇: .NET中堆栈内存的分配

下一篇: 在函数中创建的对象,是保存在堆栈还是堆上?