开发者

Memory allocation in C++

开发者 https://www.devze.com 2023-02-03 03:33 出处:网络
I am confused about the memory allocation in C++. Can anyone guide me as to where each of the variable in the below snippet is getting allocated. How ca开发者_开发问答n I determine what is getting all

I am confused about the memory allocation in C++. Can anyone guide me as to where each of the variable in the below snippet is getting allocated. How ca开发者_开发问答n I determine what is getting allocated on stack and what gets allocated on heap. Is there any good web reference for learning this.

   class Sample {
    private:
        int *p;
    public: 
        Sample() {
            p = new int;
        }
    };

    int* function(void) {
        int *p;
        p = new int;
        *p = 1;

        Sample s;

        return p;
    }


If it's created via new, it's in the heap. If it's inside of a function, and it's not static, then it's on the stack. Otherwise, it's in global (non-stack) memory.

class Sample {
    private:
        int *p;
    public: 
        Sample() {
            p = new int;  // p points to memory that's in the heap
        }

       // Any memory allocated in the constructor should be deleted in the destructor; 
       // so I've added a destructor for you:
        ~Sample() { delete p;}

        // And in fact, your constructor would be safer if written like this:
        // Sample() : p(new int) {}
        // because then there'd be no risk of p being uninitialized when
        // the destructor runs.
        //
        // (You should also learn about the use of shared_ptr,
        //  which is just a little beyond what you've asked about here.)
    };

    int* function(void) {
        static int stat; // stat is in global memory (though not in global scope)
        int *p;          // The pointer itself, p, is on the stack...
        p = new int;     // ... but it now points to memory that's in the heap
        *p = 1;

        Sample s;        // s is allocated on the stack

        return p;
    }

}

int foo; // In global memory, and available to other compilation units via extern

int main(int argc, char *argv[]) { 
// Your program here...


Where ever new keyword is there it get allocated on heap.

class Sample {
private:
    int *p; //allocated on stack
public: 
    Sample() {
        p = new int; //integer gets allocated on heap
    }
};

int* function(void) {
    int *p;        //allocated on stack
    p = new int;   //integer gets allocated on heap
    *p = 1;

    Sample s;      //allocated on stack

    return p;
}


Any thing with new is on heap. s of Sample s gets memory allocated on stack.


class Sample {
private:
    int *p; 
public: 
    Sample() {
        p = new int; //<--allocated on the heap. Anything created with operator new is allocated on the heap.
    }
};

int* function(void) {
    int *p; //<-- pointer is allocated on the stack as it is a local variable
    p = new int; //<-- the value pointed to by p is allocated on the heap (new operator)
    *p = 1;

    Sample s; //<--Allocated on the stack (local variable).

    return p;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消