Class members memory allocation

My question is mostly theoretical. Suppose we have a class

class A{
private:
    int * a;
    int b;
private:
    A(){
      a = new int[100];
      b = 100;
    }
   ~A(){
      delete [] a;
    }
}

As far as I know if we create object of type A dynamically ( A * a = new A() ) the memory for this object will allocate in heap and if I use ( A a ) it will be created on stack (A a) . In the case when object is created on stack memory for variable a will allocated on heap and in case when we allocated object on heap memory for object b will be allocated on stack. First question for me to be sure: am I right?

And second question would it be more efficient to store all members of class in heap memory or stack memory eg?

class A{
    private:
        int * a;
        int * b;
    private:
        A(){
          a = new int[100];
          b = new int(100);
        }
       ~A(){
          delete [] a;
          delete b;
        }
    }

When I said efficient I mean that all data about class member will be storing near each other in memory in heap or stack (actually I'm not sure that is correct that they will be storing near each other).


First off there is no heap or stack in C++. Instead we have automatic storage duration and dynamic storage duration. A object with automatic storage duration is a scoped object. When it goes out of scope it is automatically cleaned up. An object with dynamic storage duration on the other hand is not bound by its scope. Its lifetime only ends when it is explicitly ended by the program (generally this means calling delete ).

Now in A you have one object stored with automatic storage duration , b , and one with dynamic storage duration , a . This means that b will live wherever the A instance lives. a also lives within the A instance but the memory it points to will reside somewhere in memory but we do not know where. When the instance is destroyed b will automatically be cleaned up but a will required special handling in the destructor otherwise the memory will leak. You can visualize it like

    A
+------+   +----------+
|   a->+---| 100 ints |
|   b  |   +----------+
+------+

As far as efficiency goes as Some programmer dude mentioned you should not really worry about that. You should use the types that you fell is right for the job. Once you have it up and running then you can profile it to find where the bottle neck is. If you see too many cache misses because of using pointers then you can look at trying to localize the data to the class itself.

I would also like to mention then if you find yourself writting some_type* name = new/new[] then you should consider using a std:unique_ptr<some_type>/std:unique_ptr<some_type[]> or std::vector<some_type> .


No, you are not exactly correct.

a and b are variables inside your object. They both extends the size of your class - at least by the size of sizeof(int*) .

Depending of the way you constructed your object, the memory for this variables is allocated on stack or heap as you already mentioned:

new A/int/etc allocates memory on heap

A/int/etc. var A/int/etc. var allocates memory on stack

The thing you miss is that your data allocated in constructor

  a = new int[100];

is not a part of your class object. It is some external data. In your class object you have only a int* member (with a size of 4-8 bytes, depending on architecture) that points to this data.


First of all the downside to the member raw pointers (for ex. MyClass ) is it forces to #include the header where MyClass is declared. This can lead to slow compilation. To solve you can use smart pointers with forward declarations.

And second question would it be more efficient to store all members of class in heap memory or stack memory eg?

Typically best to use pointer only when necessary. You should generally declare the members as values in class. It will be local, there will be less chance for errors, fewer allocations, ultimately fewer things that could go wrong, and the compiler can always know it is there at a specified offset so... it helps optimization and binary reduction at a few levels.

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

上一篇: JavaScript如何决定为数值分配什么样的内存大小?

下一篇: 类成员内存分配