jonjohnson
jonjohnson

Reputation: 423

Objects on heap

I have a theoretical question more than practical as I would only like to know what happens in a certain situation rather than anything practical. So for example, if I create a smart pointer object std::uniqe_ptr<City> smallville(new City); and then inside that object I create objects like Building b1; Building b2; will those objects be still created on heap even though the syntax is for creating them on stack? Or is it better to create further objects using smart pointers?

Upvotes: 0

Views: 143

Answers (5)

MSalters
MSalters

Reputation: 179779

What syntax is for "creating them (the buildings) on the stack?" Building b1 has insufficient context. That will create the objects in the surrounding context. If the surrounding context is a function, the building will have function scope ("on the stack"). If the surrounding context is a class, then the objects will be class members. And if there's no surrounding context (other than the program itself), the object will be a global and exist for the lifetime of that program.

Upvotes: 0

David Haim
David Haim

Reputation: 26476

let's look at simple case:

class B{
   long b;
};

class A{
 B b;
 int a;
};

if you declare on the stack, like this:

A a;

the object will be declared fully on stack with contiguous bytes.

if we declare on the heap , like this:

A* a = new A();

the object will be declared fully on heap with contiguous bytes.

the fact that you use smart pointer does not change this behaviour, everything will be declared on the heap. smart pointer only wrap the raw pointer with de-allocation mechanism.

if we take this one step forward, I'd say that the smart pointer does not alter the allocation mechanism. let's look at the following example:

class C{
   int* c;
public:
   C(void* buffer) : c(new (buffer) int(5)){}
};

int main (){
  char stackBuffer[100];
  unique_ptr<C> c = std::make_unique<C>(stackBuffer);
}

here, the constructor of C gets some buffer to allocate an integer from. std::unique_ptr did not changed the allocating mechanism , the integer still was allocated from the buffer allocated from the stack, although C was allocated from the heap.

or even simpler:

class E{};

int main (void){
      char buffer [100];
      unique_ptr<E> c(new(buffer) E());
};

unique_ptr only wrapped the pointer returned by new operator. the new allocated the object from a buffer from the stack. you may be confused from all of these examples, but the answer is still simple- smart pointers keep the allocating mechanism you provided. without any placement new as I did, the object will be allocated entierly from the heap, regardless if it contains inner-objects. all of them will be declared on the heap. if you provide different allocating mechanism - the smart pointer will keep it. again, its entire mission is to wrap the raw pointer with relevant destructor that will de-allocate the memory.

Upvotes: 4

Alexander Balabin
Alexander Balabin

Reputation: 2075

If b1 and b2 are non-static members of the City class then they are placed inside the block of memory that is allocated for City - there will be no separate allocation of any kind.

If b1 and b2 are locals in one of the City's methods then that case is no different from any function and they will be placed on stack.

Of course if Building makes any allocation via new in its ctor then those objects allocated are separate blocks on the heap.

Upvotes: 1

SangHeon Lee
SangHeon Lee

Reputation: 31

smallville is on stack, but new Object which smallville points to is on Heap.

Upvotes: 0

Quentin
Quentin

Reputation: 63124

Objects declared with what you call the "on the stack" syntax (automatic lifetime, as opposed to the dynamic lifetime of heap-allocated objects) are physically part of their enclosing object or scope, and their lifetimes are tied together.

So, if you allocate an object on the stack (Foo foo; in a function body), this object and all of its automatic subobjects will live on the stack.

Objects declared in the global scope act like automatic objects of maximal lifetime (the same as the program itself), but are physically stored in static memory.

If you allocate an object on the heap, all of its automatic subobjects will also live on the heap with it.

Note: this is not part of the C++ standard (only lifetimes and scopes are defined there), but that's whan happens in practice in "usual" implementations.

Upvotes: 0

Related Questions