Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic vs non-dynamic class members

In C++, ff I have a class that needs to hold a member which could be dynamically allocated and used as a pointer, or not, like this:

class A {
    type a;
};

or

class A {
    A();
    ~A();
    type* a;
};

and in the constructor:

A::A {
    a = new type();
}

and destructor:

A::~A {
    delete a;
}

are there any advantages or disadvantages to either one, aside from the dynamic one requiring more code? Do they behave differently (aside from the pointer having to be dereferenced) or is one slower than the other? Which one should I use?

like image 941
Foglio Avatar asked Jul 26 '10 18:07

Foglio


People also ask

What is a dynamic class?

Dynamic Class Loading allows the loading of java code that is not known about before a program starts. The Java model loads classes as needed and need not know the name of all classes in a collection before any one of its classes can be loaded and run.

What is dynamic member in C++?

Dynamic Member is a concept of implementation of user-defined data types which can shrink or grow according to user needs at the runtime of the program. The core concept which allows us to create some data containers of variable length is the dynamic memory allocations in C++ programming.

What is dynamic member function?

A "dynamic" member function is a member function that will change dynamically over time; this could also be implemented by a function pointer, but the function pointer would not be const.

What is the difference between dynamic and object in DART?

There is actually no difference between using Object and dynamic in the example you have given here. There is no practical difference, and you can swap the two and the program will run the same. When I refer to "semantic difference", I mean how the code will be understood by other Dart programmers.


1 Answers

There are several differences:

  1. The size of every member must be known when you're defining a class. This means you must include your type header, and you can't just use a forward-declaration as you would with a pointer member (since the size of all pointers is known). This has implications for #include clutter and compile times for large projects.

  2. The memory for the data member is part of the enclosing class instance, so it will be allocated at the same time, in the same place, as all the other class members (whether on the stack or the heap). This has implications for data locality - having everything in the same place could potentially lead to better cache utilization, etc. Stack allocation will likely be a tad faster than heap allocation. Declaring too many huge object instances could blow your stack quicker.

  3. The pointer type is trickier to manage - since it doesn't automatically get allocated or destroyed along with the class, you need to make sure to do that yourself. This becomes tricky with multiple pointer members - if you're newing all of them in the constructor, and halfway through the process there's an exception, the destructor doesn't get called and you have a memory leak. It's better to assign pointer variables to a "smart pointer" container (like std::auto_ptr) immediately, this way the cleanup gets handled automatically (and you don't need to worry about deleteing them in the destructor, often saving you from writing one at all). Also, any time you're handling resources manually you need to worry about copy constructors and assignment operators.

like image 186
tzaman Avatar answered Sep 23 '22 15:09

tzaman