Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating dynamically sized objects

Tags:

c++

Removed the C tag, seeing as that was causing some confusion (it shouldn't have been there to begin with; sorry for any inconvenience there. C answer as still welcome though :)

In a few things I've done, I've found the need to create objects that have a dynamic size and a static size, where the static part is your basic object members, the dynamic part is however an array/buffer appended directly onto the class, keeping the memory contiguous, thus decreasing the amount of needed allocations (these are non-reallocatable objects), and decreasing fragmentation (though as a down side, it may be harder to find a block of a big enough size, however that is a lot more rare - if it should even occur at all - than heap fragmenting. This is also helpful on embedded devices where memory is at a premium(however I don't do anything for embedded devices currently), and things like std::string need to be avoided, or can't be used like in the case of trivial unions.

Generally the way I'd go about this would be to (ab)use malloc(std::string is not used on purpose, and for various reasons):

struct TextCache
{
    uint_32 fFlags;
    uint_16 nXpos;
    uint_16 nYpos;
     TextCache* pNext;
    char pBuffer[0];
};

TextCache* pCache = (TextCache*)malloc(sizeof(TextCache) + (sizeof(char) * nLength));

This however doesn't sit too well with me, as firstly I would like to do this using new, and thus in a C++ environment, and secondly, it looks horrible :P

So next step was a templated C++ varient:

template <const size_t nSize> struct TextCache
{
    uint_32 fFlags;
    uint_16 nXpos;
    uint_16 nYpos;
     TextCache<nSize>* pNext;
    char pBuffer[nSize];
};

This however has the problem that storing a pointer to a variable sized object becomes 'impossible', so then the next work around:

class DynamicObject {};
template <const size_t nSize> struct TextCache : DynamicObject {...};

This however still requires casting, and having pointers to DynamicObject all over the place becomes ambiguous when more that one dynamically sized object derives from it (it also looks horrible and can suffer from a bug that forces empty classes to still have a size, although that's probably an archaic, extinct bug...).

Then there was this:

class DynamicObject
{
    void* operator new(size_t nSize, size_t nLength)
    {
        return malloc(nSize + nLength);
    }
};

struct TextCache : DynamicObject {...};

which looks a lot better, but would interfere with objects that already have overloads of new(it could even affect placement new...).

Finally I came up with placement new abusing:

inline TextCache* CreateTextCache(size_t nLength)
{
    char* pNew = new char[sizeof(TextCache) + nLength];
    return new(pNew) TextCache;
}

This however is probably the worst idea so far, for quite a few reasons.

So are there any better ways to do this? or would one of the above versions be better, or at least improvable? Is doing even considered safe and/or bad programming practice?


As I said above, I'm trying to avoid double allocations, because this shouldn't need 2 allocations, and cause this makes writing(serializing) these things to files a lot easier. The only exception to the double allocation requirement I have is when its basically zero overhead. the only cause where I have encountered that is where I sequentially allocate memory from a fixed buffer(using this system, that I came up with), however its also a special exception to prevent superflous copying.

like image 733
Necrolis Avatar asked Sep 07 '10 15:09

Necrolis


People also ask

How do you create a dynamic object?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.

How do I create a dynamic object in C++?

In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically.

How do you create a dynamic array of classes?

To make a dynamic array that stores any values, we can turn the class into a template: template <class T> class Dynarray { private: T *pa; int length; int nextIndex; public: Dynarray(); ~Dynarray(); T& operator[](int index); void add(int val); int size(); };


2 Answers

I'd go for a compromise with the DynamicObject concept. Everything that doesn't depend on the size goes into the base class.

struct TextBase 
{ 
    uint_32 fFlags; 
    uint_16 nXpos; 
    uint_16 nYpos; 
     TextBase* pNext; 
}; 

template <const size_t nSize> struct TextCache : public TextBase
{ 
    char pBuffer[nSize]; 
}; 

This should cut down on the casting required.

like image 135
Mark Ransom Avatar answered Oct 08 '22 22:10

Mark Ransom


C99 blesses the 'struct hack' - aka flexible array member.

§6.7.2.1 Structure and union specifiers

¶16 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

¶17 EXAMPLE Assuming that all array members are aligned the same, after the declarations:

struct s { int n; double d[]; };
struct ss { int n; double d[1]; };

the three expressions:

 sizeof (struct s)
 offsetof(struct s, d)
 offsetof(struct ss, d)

have the same value. The structure struct s has a flexible array member d.

106) The length is unspecified to allow for the fact that implementations may give array members different alignments according to their lengths.

Otherwise, use two separate allocations - one for the core data in the structure and the second for the appended data.

like image 26
Jonathan Leffler Avatar answered Oct 08 '22 22:10

Jonathan Leffler