Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:55:22+00:00 2026-05-16T20:55:22+00:00

Removed the C tag, seeing as that was causing some confusion (it shouldn’t have

  • 0

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 😛

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:55:22+00:00Added an answer on May 16, 2026 at 8:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: remove attribute of html tag I have some jQuery in my code
Is there any way to list all the files added/removed to repository between two
I've designed a multilingual web site and some values in database have a tag
I have an AIR app initially written in Flex 3 that I had removed
I'm writing some user-JS for Opera. It reacts on a request that doesn't have
(Disclaimer: I have removed the Qt tag in case the problem is in my
With reference to this post: How to implement tag counting I have implemented the
i have my site [here][link removed] when i run it through validator , i
I've created a custom foreach output that helps give me a tag id per
I've got this DIV tag that has a class definition in it. <div class=clear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.