-9

This might be a trivial question - everybody knows that objects created with "new" operator are allocated on heap, but what if the object is really complex, have some members, local variables, etc? Where are all of those stored?

Let's say I have object of a class Engine, really complex one. Somewhere in my code, I do this:

Engine* e = new Engine;

So it's simple, pointer goes to stack and the new instance of Engine is created on heap. But in a class Engine, I have a field that is not a pointer - where is this field stored in this case? And what if in Engine class I have a method that uses local variables - are those stored on heap as well?

Bartosz Boczula
  • 349
  • 1
  • 2
  • 9
  • 2
    _"Where are all of those stored?"_ Within the memory allocated for that complex object? – πάντα ῥεῖ Nov 03 '16 at 21:04
  • 3
    "Everybody knows" followed by speculation and lies... – Kerrek SB Nov 03 '16 at 21:06
  • What are you really asking, local vs global variables? – cpatricio Nov 03 '16 at 21:16
  • 1
    new doesn't necessarily allocate an object on the heap. It doesn't even necessarily allocate it at all. – tofro Nov 03 '16 at 21:18
  • Let's say that in some complex class I have a field, a normal field, like "int numOfThings" - if this complex object is created on heap using new operator, where would numOfThings be stored in memory? – Bartosz Boczula Nov 03 '16 at 21:25
  • I seriously doesn't understand why are you dissing this question.... It's a normal, valid and IMHO interesting question, and now I regret I even asked this - is this really stackoverflow attitude? Dissing people who are seeking knowledge? :/ – Bartosz Boczula Nov 03 '16 at 21:51
  • 2
    `is this really stackoverflow attitude?` Yes, sadly. ... Other than that, methods are not copied for each object. Methods are functions, and their local variables are on the stack. And if some object data is on the heap, non-pointer members (and even pointer members) are on the heap too (where else?). – deviantfan Nov 03 '16 at 22:31
  • 1
    @BartoszBoczula SO has a lot of posts around `new/delete` or `heap/stack`. But this one could answer [Object creation on the stack/heap?](http://stackoverflow.com/questions/10157122/object-creation-on-the-stack-heap). – J. Piquard Nov 03 '16 at 22:37

1 Answers1

1

Members of a struct/class are stored in the memory block that is allocated for an object instance of that struct/class (whether that be on the stack or the heap does not matter, but is dependent on how the object is allocated). The size of an object in memory is the total size of its data members, plus any padding the compiler adds between them for alignment purposes.

new type allocates a block of dynamic memory that is at least sizeof(type) bytes in size (it may be more for memory manager overhead) and then calls the type's constructor on that memory block.

So, for example, if you have this class:

class Engine
{
private:
    int numOfThings;
    ...
};

Then any instance of that class takes up sizeof(Engine) bytes of memory, where the first 4 bytes are occupied by the numOfThings member. That could be automatic memory:

Engine engine;

Or it could be dynamic memory:

Engine *engine = new Engine;

Now, lets say that Engine has other members that are either automatic or dynamic, eg:

class Engine
{
private:
    int numOfThings;
    uint32_t values[16];
    std::string name;
    uint8_t *data;

    Engine()
    {
        data = new uint8_t[256];
    }

    ~Engine()
    {
        delete[] data;
    }
};

The value member is (sizeof(uint32_t) * 16) (4*16=64) bytes in size, which is added to the total size of the Engine class.

The name member is sizeof(std::string) bytes in size (which varies depending on STL implementation), which is added to the total size of the Engine class. However, internally std::string has a pointer to dynamic memory it allocates for character data. The size of that memory is not included in the total size of the Engine class.

The data pointer is sizeof(uint8_t*) bytes in size (4 on 32bit systems, 8 on 64bit systems), which is added to the total size of the Engine class. The memory block that data points at is (sizeof(uint8_t) * 256) (1*256=256) bytes in size, and is stored elsewhere in dynamic memory.

Variables that are declared local inside of a class method are not part of the class itself, and thus do not count towards its size. Only data members that are declared in the class definition are counted towards its size.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks! And what if in Engine class I have a method f.e. run(), which declares a local variable, like "int temp = 4" - where this would be stored if Engine was allocated on heap? – Bartosz Boczula Nov 03 '16 at 22:31