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

  • SEARCH
  • Home
  • 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 3210692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:36:17+00:00 2026-05-17T14:36:17+00:00

I have a (C++) system that has many classes that have variable storage (memory)

  • 0

I have a (C++) system that has many classes that have variable storage (memory) requirements. In most of these cases, the size of the required storage is known at the creation of the object, and is fixed for the lifetime of the object.

I use this, for instance, to create a “String” object that has a count field, followed directly by the actual characters, inline with the object.

class String {
public:
    size_t count;
    String(size_t count, const char* text) : count(count) {/*...*/}
    inline char& at(size_t index) {
        return *(reinterpret_cast<char*>(this + 1) + index);
    }
    void* operator new (size_t size, size_t count) {
        return new char[size + count * sizeof(char)];
    }
};

class Node {
public:
    size_t count;
    Node(size_t count, ...) : count(count) {/*...*/}
    inline Node*& at(size_t index) {
        return *(reinterpret_cast<Node**>(this + 1) + index);
    }
    void* operator new (size_t size, size_t count) {
        return new char[size + count * sizeof(Node*)];
    }
};

// ... and several more like this

I am trying to reduce this code duplication by factoring out this “inline array” behavior into a common base class:

template<class T>
class Expando {
    size_t count;
    Expando(size_t count) : count(count) {}
    inline T& at(size_t index) {
        return *(reinterpret_cast<T*>(this + 1) + index);
    }
    void* operator new (size_t size, size_t count) {
        return new char[size + count * sizeof(T)];
    }
};

However, when I inherit from this class:

class String : public Expando<char> {/*...*/}

And go to create a new String:

String* str = new (4) String(4, "test");

GCC tries to use my globally overloaded new operator, instead of the one in Expando:

inline void* operator new (size_t size, void* mem) { return mem; }

Now, I could just duplicate the new operator for each of the classes (String, Node, …), but that would half-defeat the purpose of the refactoring.

Is there a simple solution to this? I’d like to maintain the data inline with the rest of the class (to avoid extra dereferencing and heap allocation), as well as avoid non-standard extensions (such as the zero-size arrays at the end of classes). At the same time, I’d like to reduce duplication.

  • 1 1 Answer
  • 0 Views
  • 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-17T14:36:18+00:00Added an answer on May 17, 2026 at 2:36 pm

    have you used

    using Expando::new
    

    in your derived class new operator? For example:

    void* operator new (....)
    {
        using Expando::new;
        ....
    }
    

    Otherwise, if you don’t mind my opinion, I think your implementation of your String class is way off base. You have a count member, but no actual pointer data member to point to an array of whatever will comprise your string. Talk about a weird implementation. That is just asking for trouble from a maintanence developer a few years down the road as they scratch their head and go: “Whaaat?”

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

Sidebar

Related Questions

We have a system that has a Mysql database with about 2Gigs of data.
We have a system where customers, mainly European enter texts (in UTF-8) that has
If I have an instance of a System.Timers.Timer that has a long interval -
I have an old system that uses XML for it's data storage. I'm going
I have a list of 'Interests' that each user in my system has the
I have a system that combines the best and worst of Java and PHP.
I have a system that uses a meta refresh to a logout page, which
I have a system that creates an order and that order can be billed
Here's an interesting question. I have a system that attempts to run some initialization
I have a shop system that integrates PayPal in the usual way, i.e. the

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.