Store a data member on heap memory

This question already has an answer here:

  • When should I use the new keyword in C++? 11 answers

  • Yes, when you initialize a local variable it gets memory from stack.
    But if you want to initialize some local variable and use heap memory then use concept of Dynamic Memory Allocation like this:

    int *foo;
    foo = new int [5];
    

    This will initialize a int pointer in stack but it will point to some memory location in heap which you can use to store your foo array.

    Check this out for more understanding.

    链接地址: http://www.djcxy.com/p/96532.html

    上一篇: 管理对象变量的生命周期的最佳方法是什么?

    下一篇: 将数据成员存储在堆内存上