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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:16:04+00:00 2026-05-20T09:16:04+00:00

This question is specific to the MSVC compiler (specifically 2008), but I’m interested in

  • 0

This question is specific to the MSVC compiler (specifically 2008), but I’m interested in non-compiler specific answers too.

I’m trying to figure out how to align a char buffer on the stack, based on the alignment of some arbitrary type. Ideally the code would read:

__declspec( align( __alignof(MyType) ) ) char buffer[16*sizeof(MyType)];

Unfortunately, this doesn’t work

error C2059: syntax error :
‘__builtin_alignof’

The compiler just doesn’t like the nested statements.

My only other idea is to do this:

char buffer[16*sizeof(MyType)+__alignof(MyType)-1];
char * alignedBuffer = (char*)((((unsigned long)buffer) + __alignof(MyType)-1)&~(__alignof(MyType)-1));

Does anyone know of a nicer way? It seems like the declspec thing should work, do I just have the syntax wrong or something?

Thanks for reading 🙂

  • 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-20T09:16:05+00:00Added an answer on May 20, 2026 at 9:16 am

    Update

    Check Robert Knight’s answer! Uses C++11 but is much cleaner than this…


    Original Answer

    How about this nasty hack:

    namespace priv {
    
    #define PRIVATE_STATICMEM(_A_) \
        template <size_t size> \
        struct StaticMem<size,_A_> { \
          __declspec(align(_A_)) char data[size]; \
          void *operator new(size_t parSize) { \
            return _aligned_malloc(parSize,_A_); \
          } \
          void operator delete(void *ptr) { \
            return _aligned_free(ptr); \
          } \
        };
    
        template <size_t size, size_t align> struct StaticMem {};
        template <size_t size> struct StaticMem<size,1> {char data[size];};
    
        PRIVATE_STATICMEM(2)
        PRIVATE_STATICMEM(4)
        PRIVATE_STATICMEM(8)
        PRIVATE_STATICMEM(16)
        PRIVATE_STATICMEM(32)
        PRIVATE_STATICMEM(64)
        PRIVATE_STATICMEM(128)
        PRIVATE_STATICMEM(256)
        PRIVATE_STATICMEM(512)
        PRIVATE_STATICMEM(1024)
        PRIVATE_STATICMEM(2048)
        PRIVATE_STATICMEM(4096)
        PRIVATE_STATICMEM(8192)
    
    }
    
    template <typename T, size_t size> struct StaticMem : public priv::StaticMem<sizeof(T)*size,__alignof(T)> {
        T *unhack() {return (T*)this;}
        T &unhack(size_t idx) {return *(T*)(data+idx*sizeof(T));}
        const T &unhack() const {return *(const T*)this;}
        const T &unhack(size_t idx) const {return *(const T*)(data+idx*sizeof(T));}
        StaticMem() {}
        StaticMem(const T &init) {unhack()=init;}
    };
    

    Looks scary, but you need all that only once (preferably in some well hidden header file 🙂 ). Then you can use it in the following way:

    StaticMem<T,N> array; //allocate an uninitialized array of size N for type T
    array.data //this is a raw char array
    array.unhack() //this is a reference to first T object in the array
    array.unhack(5) //reference to 5th T object in the array
    

    StaticMem<T,N> array; can appear in the code, but also as a member of some bigger class (that’s how I use this hack) and should also behave correctly when allocated on the heap.

    Bug fix:

    Line 6 of the example: char data[_A_] corrected into char data[size]

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

Sidebar

Related Questions

This question is specific to capistrano, but it's a general gem question too. I
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question is kind of related to another question but I have a specific
This question is a duplicate of 1042830 , but MonoTouch-specific. Is there a way
I noticed This question , but my question is a bit more specific. Is
This question was asked already here , but rather than answering the specific question,
This question is specific to Adobe CQ5, so ExtJS answers by themselves won't be
This question might be a bit long and specific, but I have been attempting
(The context of this question is specific to XNA, but the principle is general.)
I don't know if this question is specific to Foundation command line tools, but

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.