search

Home  >  Q&A  >  body text

c++类的成员变量的存储位置?

ringa_leeringa_lee2807 days ago521

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:13:15

    Test * test_ptr = new Test(); allocated on the heap
    Test test; on the stack

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:13:15

    Test *ptr = new Test;
    Test::array is on the heap, ptr itself is on the stack...

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:13:15

    It depends on where the class is instantiated

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:13:15

    It’s been 15 years and it’s time to use c++11
    Smart pointers are safer

    //分配在堆上,多了不会爆栈,当然也不能大的太离谱了
    auto test_ptr = shared_ptr<Test>(new Test()); 
    
    //在栈上,多了会爆栈的
    Test test; 

    reply
    0
  • Cancelreply