Does this type of memory get allocated on the heap or the stack?

Given a slight modification of your example.

Given a slight modification of your example: class Foo{ private: int x100; int *y; public: Foo() { y = new int100; } ~Foo() { delete y; } } Example 1: Foo *bar = new Foo(); x and y are on the heap: sizeof(Foo*) is created on the stack. Sizeof(int) * 100 * 2 + sizeof(int *) is on the heap Example 2: Foo bar; x is on the stack, and y is on the heap sizeof(int) * 100 is on the stack (x) + sizeof(int*) sizeof(int) * 100 is on the heap (y) Actual sizes may differ slightly due to class/struct alignment depending on your compiler and platform.

An object of type Foo takes the size of 100 ints stored in sequence. If you create it on the stack, you will be getting it all on the stack. If you do it with new, it'll be on the heap as part of the object.

This is part of the language specification, I'm not sure what your question is.

Maybe he's asking which part of the language specification says this :-) – PolyThinker Jan 9 '09 at 1:32.

Strictly speaking, according to the standard the object need not exist on a stack or heap. The standard defines 3 types of 'storage duration', but doesn't state exactly how the storage must be implemented: static storage duration automatic storage duration dynamic storage duration Automatic storage duration is typically (nearly always) implemented using the stack. Dynamic storage duration is typically implemented using the heap (ultimately via malloc()), though this can be overridden even by the compiler's user.

Static storage duration is what it typically known as globals (or static storage). The standard has this to say about these things (the following are excerpts form various bits of 3.7 - Storage Duration): Static and automatic storage durations are associated with objects introduced by declarations (3.1) and implicitly created by the implementation (12.2). The dynamic storage duration is associated with objects created with operator new (5.3.4).... All objects which neither have dynamic storage duration nor are local have static storage duration.

The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3). ... Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.... Objects can be created dynamically during program execution (1.9), using new-expressions (5.3.4), and destroyed using delete-expressions (5.3.5).

A C + + implementation provides access to, and management of, dynamic storage via the global allocation functions operator new and operator new and the global deallocation functions operator delete and operator delete.... The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.4.1) And finally (regarding the array in your example class): 3.7.4 Duration of sub-objects basic.stc. Inherit The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).

Yes, the member array x will be created on the heap if you create the Foo object on the heap. When you allocate dynamic memory for Foo you are asking for memory of length sizeof(Foo) (plus possibly some memory overhead, but let's ignore that for the time being), which in your sample code implies the size of 100 ints. This has to be case for the lifespan of objects of type Foo (and their internal data) to cross scopes.

If you don't create the Foo object on the heap, and the internal array of Foo isn't a pointer to which you allocate memory with new in Foo's constructor then that internal array will be created on the stack. Again, this has to be the case in order for the array to automatically be cleaned without any deletes when the scope ends. Specifically, struct Foo { int* y; Foo() : y(new int()) { } ~Foo() { delete y; } }; will create y on the heap regardless of whether a Foo object was created on the stack or on the heap.

You mean Foo* bar = new Foo(); I suppose. That is created in the heap.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions