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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:24:21+00:00 2026-05-24T08:24:21+00:00

I have an array which is used as the underlying memory of an object

  • 0

I have an array which is used as the underlying memory of an object of type T:

char memory[sizeof T];
.
.
.
new(memory) T(whatever);

How can I make sure memory is aligned correctly for T objects? In C++0x I could just say:

alignas(T) char memory[sizeof T];

but Visual Studio 2010 does not support that particular feature yet.

  • 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-24T08:24:22+00:00Added an answer on May 24, 2026 at 8:24 am

    The usual (portable) solution is to put the memory declaration in a union with whatever built-in type in T requires the most alignment.
    The simplest way would be to use a union with all of the likely
    candidates:

    union MaxAlign
    {
        int                 i     ;
        long                l     ;
        long long           ll    ;
        long double         ld    ;
        double              d     ;
        void*               p     ;
        void (*             pf)() ;
        MaxAlign*           ps    ;
    } ;
    
    union
    {
        MaxAlign dummyForAlignment;
        unsigned char memory[sizeof(T)];
    } rawT;
    

    I’ve yet to hear about, much less encounter, a machine where the above
    didn’t suffice. Generally, just double suffices. (It is definitely
    sufficient on Intel and on Sparc.)

    In some extreme cases, this can result in allocating more memory than
    necessary, e.g. if T only contains one or two char. Most of the
    time, this really doesn’t matter, and isn’t worth worrying about, but if
    it is, the following can be used:

    namespace MyPrivate {
    
    template< typename T, bool isSmaller >
    struct AlignTypeDetail ;
    
    template< typename T >
    struct AlignTypeDetail< T, false >
    {
        typedef T type ;
    } ;
    
    template< typename T >
    struct AlignTypeDetail< T, true >
    {
        typedef char type ;
    } ;
    
    template< typename T, typename U >
    struct AlignType
    {
        typedef typename AlignTypeDetail< U, (sizeof( T ) < sizeof( U )) >::type
                            type ;
    } ;
    }
    
    template< typename T >
    union MaxAlignFor
    {
        typename MyPrivate::AlignType< T, char >::type        c ;
        typename MyPrivate::AlignType< T, short >::type       s ;
        typename MyPrivate::AlignType< T, int >::type         i ;
        typename MyPrivate::AlignType< T, long >::type        l ;
        typename MyPrivate::AlignType< T, long long >::type   ll ;
        typename MyPrivate::AlignType< T, float >::type       f ;
        typename MyPrivate::AlignType< T, double >::type      d ;
        typename MyPrivate::AlignType< T, long double >::type ld ;
        typename MyPrivate::AlignType< T, void* >::type       pc ;
        typename MyPrivate::AlignType< T, MaxAlign* >::type   ps ;
        typename MyPrivate::AlignType< T, void (*)() >::type  pf ;
    } ;
    

    In this case, MaxAlignFor<T> will never be bigger than T
    (and to have sufficient alignment, since the required alignment will
    never be larger than the size of T).

    Note that none of this is formally guaranteed by the standard. But it
    will work in practice.

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

Sidebar

Related Questions

I have an array which contains around 50-200 hyperlinks. How can I pass this
I have an array which tells whether a card is in use: int used[52];
I have Javascript array which contains User object. I have created this array from
I have an array which is a list of domains, I want to print
I have an array which contains another array Would I notate it this way?
I have an array which has the contents as the result of an sql
I have an array which i need to sort its elements by occurrence then
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here
Lets say I have an array which has n dimensions. Now in order to
I have a string say string s =C:\\Data , I have an array which

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.