Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are data members allocated in the same memory space as their objects in C++?

Say I've got a class like this:

class Test
{
  int x;
  SomeClass s;
}

And I instantiate it like this:

Test* t = new Test;

Is x on the stack, or the heap? What about s?

like image 730
fluffels Avatar asked Oct 09 '08 15:10

fluffels


People also ask

How is memory allocated to data members?

Note: Memory for member functions and static data members is allocated per class and not per object. The class sample has no data member(except static count), but this does not mean no memory space will be allocated to the objects of Sample. In such cases, minimum memory is set aside for object.

How memory is allocated to an object in C?

The memory is only allocated to the variables of the class when the object is created. The memory is not allocated to the variables when the class is declared. At the same time, single variables can have different values for different objects, so every object has an individual copy of all the variables of the class.

How memory is allocated to data member and member function?

member functions are created and placed in the memory space only once when they are defined as a part of a class specification. Since all the objects belonging to that class use the same member functions, no separated space is allocated for member functions when the objects are created.

Is the memory allocated to the class when the object is created?

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on the heap (See this for more details).


2 Answers

Test a;
Test *t = new Test;

a, and all its members, are on the stack.

The object pointed to by t, and all its members, are on the heap.

The pointer t is on the stack.

like image 196
moonshadow Avatar answered Sep 18 '22 17:09

moonshadow


Each time you "instantiate" an object/symbol using a new (we are speaking C++ here), a new memory zone will be allocated for this object. If not, it will be put on the "local" memory zone.

The problem is that I have no standard definition for "local" memory zone.

An example

This means that, for example:

struct A
{
   A()
   {
      c = new C() ;
   }

   B b ;
   C * c ;
}

void doSomething()
{
   A aa00 ;
   A * aa01 = new A() ;
}

The object aa00 is allocated on the stack.

As aa00::b is allocated on a "local" memory according to aa00, aa00::b is allocated inside the memory range allocated by the new aa00 instruction. Thus, aa00::b is also allocated on stack.

But aa00::c is a pointer, allocated with new, so the object designed by aa00::c is on the heap.

Now, the tricky example: aa01 is allocated via a new, and as such, on the heap.

In that case, as aa01::b is allocated on a "local" memory according to aa01, aa01::b is allocated inside the memory range allocated by the new aa01 instruction. Thus, aa01::b is on the heap, "inside" the memory already allocated for aa01.

As aa01::c is a pointer, allocated with new, the object designed by aa01::c is on the heap, in another memory range than the one allocated for aa01.

Conclusion

So, the point of the game is:
1 - What's the "local" memory of the studied object: Stack of Heap?
2 - if the object is allocated through new, then it is outside this local memory, i.e., it is elsewhere on the heap
3 - if the object is allocated "without new", then it is inside the local memory.
4 - If the "local" memory is on the stack, then the object allocated without new is on the stack, too.
5 - If the "local" memory is on the heap, then the object allocated without new is on the heap, too, but still inside the local memory.

Sorry, I have no better vocabulary to express those concepts.

like image 45
paercebal Avatar answered Sep 18 '22 17:09

paercebal