2

Is there any effect of allocating memory using 'new' inside an object that itself was allocated using new. Is there any advantage or disadvantage on the compiler, linker, run-time performance or anything?

An example of what I'm talking about

class IntData
{
public:
    IntData()
    {
        IntVector = new std::vector<int>();
        //...
    }

protected:
    std::vector<int> *IntVector; //Would this be any different to static allocation if...
};

//...I know that all IntData objects will be dynamically allocated
IntData *object = new IntData();
Saif Ur Rehman
  • 338
  • 3
  • 14
  • 1
    `IntVector = new std::vector();` doesn't do any good over simply saying `std::vector IntVector;` – πάντα ῥεῖ May 25 '14 at 10:27
  • using *IntVector just add a level of indirection .... thats it. – sunny1304 May 25 '14 at 10:29
  • 1
    no issue of using this approach. as of performance, run time - this depends on the final design and algorithm, very hard to say – NirMH May 25 '14 at 10:30
  • 1
    Avoid using new in C++, this isn't Java/C#! That said, it makes a difference as it's a waste of time to get it coded correctly (you need assignment op, cctor and dtor) and also at runtime for the extra pointer indirection. – Ulrich Eckhardt May 25 '14 at 10:37
  • What @NirMH says, +1. If your requirements mandate this, then you have to do it. It's not uncommon, eg. in GUI frameworks where the lifetime of windows that contain windows that contain windows... is longer than the functions that create such instances. – Martin James May 25 '14 at 10:48
  • Also, what @UlrichEckhardt says - use the containers available. – Martin James May 25 '14 at 10:49

1 Answers1

1

Using

std::vector<int> IntVector;

you're giving automatic storage to your vector (i.e. if in a function it will be on the stack space, in the case of a member of a class it depends on how the parent object is allocated.. in the above example: on the heap)

using

IntVector = new std::vector<int>();

you're fully allocating the object on the heap, including a few container's internal data (objects will always be copied on the heap).

Is this worth doing it?

For a small bunch of bytes: usually no. Furthermore you'll have to do memory book-keeping by yourself by calling delete. Might not be an issue but in the simple case above I see no reason for doing that.


Also related to a more general case: STL containers on the stack and the heap and Object creation on the stack/heap?

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • By "headers for the STL" you mean the variables inside `vector`? The word "headers" seems misleading there. Also only the objects inside a `vector` will always be on the heap, it is possible and useful to put objects on the stack instead, or where ever you want. Also the manual book-keeping you speak of can be avoided by using a `unique_ptr`. – nwp May 25 '14 at 12:03
  • I agree that the terminology might be misleading, I'll edit it to be more general. Also agreed with the smart ptrs, nevertheless it's something that isn't strictly needed in the simple code posted above. – Marco A. May 25 '14 at 12:22