Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main difference between the two instances is related to object lifetime. </p> <p><code>T1</code> has dynamic allocation, meaning its lifetime ends when <code>delete</code> is called, <code>T2</code> has automatic allocation, meaning its lifetime ends when execution leaves the allocation's enclosing block.</p> <p>Object lifetime should be the primary decision factor when choosing between dynamic or automatic variables. </p> <p>The second decision factor should be object size. Automatic objects are generally stored on a "stack", which has a limited size. In contrast, dynamically allocated objects can have much larger sizes.</p> <p>A distant third factor could be locality of reference, which might mean, in some scenarios, that the indirection (<code>-&gt;</code>) will impose a minute performance penalty. That's something only a profiler can tell.</p> <blockquote> <p>I design the methods of T1 and T2 accordingly, meaning that in T1 I pretty much have only void functions that will operate on the this object and inside T2 I will have methods that will access the actual members.</p> </blockquote> <p>That doesn't really make much sense. Both classes can have members and non-void functions.</p> <p>Do note that dynamic memory allocations impose a cost and also, usually, a lock must be aquired internally by the memory allocator. You can try different allocators (like <a href="http://goog-perftools.sourceforge.net/doc/tcmalloc.html" rel="nofollow">TCMalloc</a> and others) which offer some performance improvements in multithreaded scenarios.</p> <p>With dynamic storage there is also the real thread of memory leaks, forgetting to call <code>delete</code>. This can be alleviated by using smart pointers but they add their own performance penalties.</p> <p>Overall, the only real issue, in a multithreaded environment or not, is whether you actually need the (lifetime or size) properties offered by dynamic allocation and are willing to pay its performance cost.</p> <p>(A cost that you should measure before making the decision. Perfect is the enemy of good enough.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload