3

I just got a question regarding the storage of c++ class members.

For example, I have a class like this:

class MyClass1{
    int a;
    int b[4];
    int c;
}

In the main function:

int main(){
    MyClass1 class1;
    MyClass1* class_ptr1= new MyClass1();
}

Then how are the members a,b,c stored? For class1, I think all members are allocated with a space in stack, even the array b.

Then what about class_ptr1? Apparently it's dynamically allocated, are the members dynamically allocated as well, even thought they are not pointers.

Thanks very much.

Thanks very much for the replies. Now I understand that once the class is dynamically allocated, all its members are also dynamically allocated. In that case, do I need to do anything to deallocate them, once I finished using the class? I mean, is it fine just to:

delete(class_ptr1);

or I need to free its members first.

penpen926
  • 115
  • 1
  • 6
  • "Now I understand that once the class is dynamically allocated, all its members are also dynamically allocated." you understand incorrectly. There is no "also". See my answer. – Slava Jun 24 '14 at 15:20
  • @Slava Are you implying that's not the case? – Luchian Grigore Jun 24 '14 at 15:21
  • @LuchianGrigore I am implying that statement "all its members also dynamically allocated" is incorrect. That does not mean that they allocated separately though. – Slava Jun 24 '14 at 15:25
  • @Slava can you give an example of allocating an object dynamically and its members aren't in dynamic memory? – Luchian Grigore Jun 24 '14 at 15:30
  • 1
    @LuchianGrigore The way the OP made his statement highly suggest that he thinks that every member of his class is seperatly allocated dynamicly as if you had called new on each of them turn by turn, that's the statement which Slava denied, of course every members of the instance are in dynamic memory if the instance itself is because the instance is nothing more than its members :) – Drax Jun 24 '14 at 15:41
  • @LuchianGrigore I cannot. Dough. You need to reread my statement and understand it. If somebody says "when buying a car he _also_ is buying car parts, like wheels, glass, roof etc" he is incorrect, because car is that parts together. There is no also there. Understand? – Slava Jun 24 '14 at 15:42
  • @Drax thanks, you explained my point exactly. – Slava Jun 24 '14 at 15:46

4 Answers4

6

Your question comes from the fact you do not quite understand what the class/struct in C++. When you declare a class:

class MyClass1{
    int a;
    int b[4];
    int c;
};

any instance of this class will be one continuous block of memory size equal to sizeof(MyClass1) where each field will have some offset of the beginning of that block. If you think about it and understand it, answer to your question will be obvious.

Slava
  • 43,454
  • 1
  • 47
  • 90
1

In the first case, the object and its members are in automatic storage.

In the second case, the object and its members are in dynamic storage.

You can trivially refer to these as stack and heap respectively, yes.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • What exactly do you mean with 'trivially' in this case? – dreamer Jun 24 '14 at 15:27
  • @dreamer stack and heap are slang :) C++ is unaware of such implementation details. I say trivially because it's something we all use, but it's not 100% correct. – Luchian Grigore Jun 24 '14 at 15:35
  • Are you implying that internally C++ stores both in the same way but that the only difference is whether the memory is allocated automatically? – dreamer Jun 24 '14 at 15:42
  • @dreamer no, I'm implying "stack" and "heap" aren't part of the standard. automatic/dynamic storage are. see http://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and – Luchian Grigore Jun 24 '14 at 16:28
1

Memory layout of the two instances, class1 and *class_ptr1, is the same, they just reside in different parts of the memory - class1 on the stack, *class_ptr1 on the heap.

Dalibor Frivaldsky
  • 810
  • 1
  • 6
  • 14
1
  • When you created class1, The compiler reserved around 24 bytes on the stack to place that object.
  • When you created class_ptr1, The call to new() allocated around 24 bytes from the heap to place that object.
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173