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 635887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:27:17+00:00 2026-05-13T20:27:17+00:00

I’m overloading operator new , but I recently hit a problem with alignment. Basically,

  • 0

I’m overloading operator new, but I recently hit a problem with alignment. Basically, I have a class IBase which provides operator new and delete in all required variants. All classes derive from IBase and hence also use the custom allocators.

The problem I’m facing now is that I have a child Foo which has to be 16-byte aligned, while all others are fine when aligned to 8-byte. My memory allocator however aligns to 8-byte boundaries only by default, so now the code in IBase::operator new returns an unusable piece of memory. How is this supposed to be solved correctly?

I can simply force all allocations to 16 bytes, which will work fine until a 32-byte aligned type pops up. Figuring out the alignment inside operator new doesn’t seem to be trivial (can I do a virtual function call there to obtain the actual alignment?) What’s the recommended way to handle this?

I know malloc is supposed to return a piece of memory which is suitably aligned for everything, unfortunately, this “everything” doesn’t include SSE types and I’d really like to get this working without requiring the user to remember which type has which alignment.

  • 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-13T20:27:18+00:00Added an answer on May 13, 2026 at 8:27 pm

    This is a possible solution. It will always choose the operator with the highest alignment in a given hierarchy:

    #include <exception>
    #include <iostream>
    #include <cstdlib>
    
    // provides operators for any alignment >= 4 bytes
    template<int Alignment>
    struct DeAllocator;
    
    template<int Alignment>
    struct DeAllocator : virtual DeAllocator<Alignment/2> {
      void *operator new(size_t s) throw (std::bad_alloc) {
        std::cerr << "alignment: " << Alignment << "\n";
        return ::operator new(s);
      }
    
      void operator delete(void *p) {
        ::operator delete(p);
      }
    };
    
    template<>
    struct DeAllocator<2> { };
    
    // ........... Test .............
    // different classes needing different alignments
    struct Align8 : virtual DeAllocator<8> { };
    struct Align16 : Align8, virtual DeAllocator<16> { };
    struct DontCare : Align16, virtual DeAllocator<4> { };
    
    int main() {
      delete new Align8;   // alignment: 8
      delete new Align16;  // alignment: 16
      delete new DontCare; // alignment: 16
    }
    

    It’s based on the dominance rule: If there is an ambiguity in lookup, and the ambiguity is between names of a derived and a virtual base class, the name of the derived class is taken instead.


    Questions were risen why DeAllocator<I> inherits DeAllocator<I / 2>. The answer is because in a given hierarchy, there may be different alignment requirements imposed by classes. Imagine that IBase has no alignment requirements, A has 8 byte requirement and B has 16 byte requirement and inherits A:

    class IBAse { };
    class A : IBase, Alignment<8> { };
    class B : A, Alignment<16> { };
    

    Alignment<16> and Alignment<8> both expose an operator new. If you now say new B, the compiler will look for operator new in B and will find two functions:

                // op new
                Alignment<8>      IBase
                     ^            /
                      \         /
                        \     /
     // op new            \ /
     Alignment<16>         A
                \         /
                  \     /
                    \ /
                     B 
    
    B ->      Alignment<16>  -> operator new
    B -> A -> Alignment<8> -> operator new
    

    Thus, this would be ambiguous and we would fail to compile: Neither of these hide the other one. But if you now inherit Alignment<16> virtually from Alignment<8> and make A and B inherit them virtually, the operator new in Alignment<8> will be hidden:

                // op new
                Alignment<8>      IBase
                     ^            /
                    / \         /
                  /     \     /
     // op new  /         \ /
     Alignment<16>         A
                \         /
                  \     /
                    \ /
                     B 
    

    This special hiding rule (also called dominance rule) however only works if all Alignment<8> objects are the same. Thus we always inherit virtually: In that case, there is only one Alignment<8> (or 16, …) object existing in any given class hierarchy.

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

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You've got a couple of options: Overwrite Zend_Db_Table_Row_Abstract::__get() in your… May 14, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer Since you're using a variable for the ID, you need… May 14, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer It stands for Table Data May 14, 2026 at 11:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.