Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On moving/copying c++ object instances

Tags:

c++

I'm attempting to create a type-agnostic vector in C++ that is distinguished by two things. First, it allocates the memory in the object itself, at least until a certain point, rather than maintaining the actual array of objects on the heap. Secondly, it can't use C++'s copy/assign constructors, which appears to slow down the code and is not necessary.

While looking through the codebase I maintain on my computer, I found a class in LLVM's codebase that pretty much perfectly describes what I'm looking for: SmallVector.h. Being relatively new to C++, I'm not entirely sure why some of the design decisions were made. For instance, why is the array allocated in terms of U rather than T? The comment gives a clue:

If T has a ctor or dtor, we don't want it to be automatically run, so we need to represent the space as something else. An array of char would work great, but might not be aligned sufficiently. Instead we use some number of union instances for the space, which guarantee maximal alignment.

U, of course, refers to the following union:

union U {
    double D;
    long double LD;
    long long L;
    void *P;
} FirstEl;

So, I guess, here are my true questions: Why does allocating an array of T imply that constructors/destructors are called? Is there any way to move c++ object instances around, i.e. in and out of the vector, without calling these constructors/destructors? I guess I can just use LLVM's SmallVector implementation, but I hate using code without understanding it.

Best, Duane

like image 503
duane Avatar asked Oct 10 '22 14:10

duane


2 Answers

You should look at the basic machinery behind the standard library allocators, which deal with a lot of the questions that you probably have!

Here's the basic allocation principle. We separate memory allocation and object construction. The big obstacle is, as you observed, that the memory should be aligned correctly for the object:

// getting memory
void * p = malloc(1000);  // version 1, system's allocator
char q[1000];             // automatic array, this is also memory :-)

// constructing an object
T * m_x1 = ::new (p) T;   // default-initialized
T * m_x2 = ::new (q) T(); // value-initialized
T * m_x3 = ::new (q + sizeof(T)) T(1, 'a'); // some specific constructor

// destroying the objects:
m_x1->~T();
m_x2->~T();
m_x3->~T();

To do what you have in mind, you could take the char array q that I used and make it a member of your class. That is, the class always carries around with it some memory in which to construct objects.

The actual object construction is done with the global placement-new expression. Recall that objects thus constructed must be destroyed manually (which will be your responsibility).

The standard library allocators do pretty much something like that.

Separating memory allocation and object construction is at the heart of any sort of advanced memory managing, responsibility owning class.

Note that once an object is constructed at a specific address, you must not move the memory around. The object may very well depend on its own location in memory! The only valid way to move objects around is to copy/move-construct a new object.

like image 165
Kerrek SB Avatar answered Oct 13 '22 10:10

Kerrek SB


Why does allocating an array of T imply that constructors/destructors are called?

Because that's what the standard mandates. Allocating an array of T means initializing each of its elements as well. Note that in C++11 the alignment restrictions for char have been changed so that "an array of char works great" now.

Is there any way to move c++ object instances around, i.e. in and out of the vector, without calling these constructors/destructors?

Yes, by means of move-constructors/assignment operators, which are also new to C++11. There is an emulator library for C++03 and also move-support containers at Boost.

like image 23
K-ballo Avatar answered Oct 13 '22 11:10

K-ballo