最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Is arr in stack or heap? - Stack Overflow

programmeradmin1浏览0评论

I want to understand if arr here is in stack or heap. Since obj is dynamically allocated is the arr in heap? What if i do not have obj2, if i just allocate obj dynamically in main?


#include <iostream>
#include <memory>
class Obj {
public:
    double arr[10000];
    Obj() {
        std::cout << "Obj Constructor" << std::endl;
    }
    ~Obj() {
        std::cout << "Obj Destructor" << std::endl;
    }
};
class Obj2 {
public:
    Obj2() {
        obj = std::make_unique<Obj>(); 
    }
    ~Obj2() {
        std::cout << "Obj2 Destructor" << std::endl;
    }
private:
    std::unique_ptr<Obj> obj; 
};
int main() {
    Obj2 obj2; 
    return 0;
}

Program runs and complies as should.

I want to understand if arr here is in stack or heap. Since obj is dynamically allocated is the arr in heap? What if i do not have obj2, if i just allocate obj dynamically in main?


#include <iostream>
#include <memory>
class Obj {
public:
    double arr[10000];
    Obj() {
        std::cout << "Obj Constructor" << std::endl;
    }
    ~Obj() {
        std::cout << "Obj Destructor" << std::endl;
    }
};
class Obj2 {
public:
    Obj2() {
        obj = std::make_unique<Obj>(); 
    }
    ~Obj2() {
        std::cout << "Obj2 Destructor" << std::endl;
    }
private:
    std::unique_ptr<Obj> obj; 
};
int main() {
    Obj2 obj2; 
    return 0;
}

Program runs and complies as should.

Share Improve this question edited Jan 18 at 14:06 trincot 351k36 gold badges272 silver badges325 bronze badges asked Jan 18 at 8:26 Rich GgRich Gg 612 bronze badges 5
  • 2 arr is in the heap because arr is part of Obj and obj is pointing at a dynamically allocated Obj. Same is true of your second question, for the same reason. – john Commented Jan 18 at 8:29
  • 3 Note : c++ doesn't specify stack or heap (it is in a way an implementation detail) . But instead C++ specifice automatic or dynamic (or static) storage : see Storage class specifiers. Where indeed automatic storage is often the stack, and dynamic storage is often implemented using a heap – Pepijn Kramer Commented Jan 18 at 9:53
  • @John, if the instance of Obj is a local variable then arr has automatic storage (stack), if the instance of Obj had dynamic storage then arr will have automatic storage too. obj will always have dynamic storage – Pepijn Kramer Commented Jan 18 at 9:55
  • On all platforms I've used, C++ storage duration: automatic storage corresponds to the stack, and dynamic storage corresponds to the heap. (In case you need to make a distinction between the standard and an implementation's details.) – Eljay Commented Jan 18 at 12:43
  • Side note: nothing in this code needs the extra stuff that std::endl does. Use '\n' to end a line unless you have a good reason not to. – Pete Becker Commented Jan 18 at 14:29
Add a comment  | 

1 Answer 1

Reset to default 6

class member arrays are placed in the same storage as their containing class or struct, the array is a subobject.

You allocated Obj on the heap with std::make_unique<Obj>();, therefore the array inside it is placed on the heap.

if you want a dynamic array member that automatically gets placed on the heap regardless of its parent's storage then use std::vector, which is recommended if you have large arrays to avoid a stack overflow.

发布评论

评论列表(0)

  1. 暂无评论