Will heap allocated objects have their members allocated on the stack?

后端 未结 4 433
醉梦人生
醉梦人生 2021-01-22 17:15

Let\'s consider this example.

class StaticlyManagedObject
{
   //some class members....
}
class DynamiclyManagedObject
{
   StaticlyManagedObject _staticlyManage         


        
相关标签:
4条回答
  • 2021-01-22 17:59

    Whether a member of your class is another class, or an elementary data type:

    class DynamiclyManagedObject
    {
       StaticlyManagedObject _staticlyManagedObject;
       int some_integer;
    };
    

    It doesn't matter whether the class member is another class, or an elementary data type, such as an int. Both "_staticlyManagedObject" and "some_integer" are completely identical to each other, except for their type (of course, that's not exactly a trivial attribute). One is an int, the other one is some other class. The type of a class member does not affect its scope. Either the entire class is dynamically allocated, or it's not. Or it's allocated in the automatic scope (on the stack, as you say).

    The only exception to the rule is a static class member:

    class DynamiclyManagedObject
    {
       StaticlyManagedObject _staticlyManagedObject;
       int some_integer;
    
       static std::string some_string;
    };
    

    The rules are different for some_string. Note that this is a real static class member, and not a class member whose type happens to be StaticallyManagedObject.

    0 讨论(0)
  • 2021-01-22 18:04

    A subobject has the same storage duration as the complete object it is a part of. If an instance of DynamiclyManagedObject is dynamically allocated, then the StaticlyManagedObject member will be destroyed when the DynamiclyManagedObject is destroyed.

    Informally, you might say that the subobject will be on the heap if and only if the complete object is on the heap. However, storage duration is the technically correct way to talk about it; heap and stack are implementation details.

    0 讨论(0)
  • 2021-01-22 18:07

    Irrespective of the type (be it a struct, class or primitive), when using the new operator in C++ or dynamic allocation in C with malloc (which gets the size of your struct as parameter, to know how many bytes to allocate) - all of the space required (containing members) will be placed on the heap.

    These calls return a pointer to the heap memory zone which was allocated.

    Local function variables and parameters are always placed on the stack.

    If you declare a pointer local variable its place will still be on the stack, but you will have to use the dynamic allocation methods mentioned above to allocate heap memory to which your stack-allocated pointer will actually point to.

    0 讨论(0)
  • 2021-01-22 18:08

    StaticlyManagedObject is a misnomer. It's allocated dynamically, same as the parent object.

    The nested class members use the same allocation method as the parent object, unless they're specifically marked static, in which case they aren't allocated at the time of object creation, or they're specifically allocated dynamically in constructors.

    0 讨论(0)
提交回复
热议问题