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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:53:55+00:00 2026-06-12T18:53:55+00:00

I would like to create a struct that has a certain alignment. I would

  • 0

I would like to create a struct that has a certain alignment.

I would like to use the same struct definition for both GCC and VisualC++ compilers.

In VisualC++, one typically does this:

__declspec(align(32))
struct MyStruct
{
// ...
};

In GCC, one typically does this:

struct MyStruct
{
// ...
} __attribute__ ((aligned (32)));

I could of course create the appropriate macros to make this work:

BEGIN_ALIGNED_STRUCT(32)
struct
{
// ...
}
END_ALIGNED_STRUCT(32)
;

And thus be able to handle both cases transparently, but here I have to duplicate the alignment constant (32), which I’d like to avoid.

An alternative in GCC is to put the __attribute__ after the struct tag, as mentioned in the docs, like so:

struct __attribute__ ((aligned (32))) MyStruct
{
// ...
};

And thus I could make this type of syntax work:

ALIGNED_STRUCT(32) MyStruct
{
// ...
};

Does anyone have any better versions? Other ideas?
I tried a little code searching, but didn’t find anything too promising.


Update: Based on @John’s comment, here’s another version that could work (I haven’t compiled it, but the docs indicate it’s an OK idea)

struct MyStruct_Unaligned
{
// ...
};

TYPEDEF_ALIGNED(32, MyStruct_Unaligned, MyStruct);

// Would expand to one of:
// 
// typedef __declspec(align(32)) MyStruct_Unaligned MyStruct;
//
// typedef struct __attribute__ ((aligned (32))) MyStruct_Unaligned MyStruct
  • 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-06-12T18:53:57+00:00Added an answer on June 12, 2026 at 6:53 pm

    I know this thread is quite old – however it is yet to be marked as answered and the solutions mentioned are not the easiest to use.
    The best way to solve this is to notice that MSVC allows the declspec to appear after the declarator.
    Here is my own implementation:

    #if defined(_MSC_VER)
    #define ALIGNED_(x) __declspec(align(x))
    #else
    #if defined(__GNUC__)
    #define ALIGNED_(x) __attribute__ ((aligned(x)))
    #endif
    #endif
    
    #define _ALIGNED_TYPE(t,x) typedef t ALIGNED_(x)
    
    /*SOME USAGE SAMPLES*/
    
    ALIGNED_TYPE_(double, 16) aligned_double_t;
    
    ALIGNED_TYPE_(struct, CACHE_LINE) tagALIGNEDSTRUCT
    {
        /*STRUCT MEMBERS GO HERE*/
    }aligned_struct_t;
    
    ALIGNED_TYPE_(union, CACHE_LINE) tagALIGNEDUNION
    {
        /*UNION MEMBERS GO HERE*/
    
    }aligned_union_t;
    

    You can test this with the following code (notice the #pragma pack –> This is for MSVC)

    #if defined(_MSC_VER)
    #define ALIGNED_(x) __declspec(align(x))
    #else
    #if defined(__GNUC__)
    #define ALIGNED_(x) __attribute__ ((aligned(x)))
    #endif
    #endif
    
    #define ALIGNED_TYPE_(t,x) typedef t ALIGNED_(x)
    
    #pragma pack(1)
    typedef struct tagSTRUCTPACKED
    {
        int alignedInt;
        double alignedDouble;
        char alignedChar;
    }struct_packed_t;
    #pragma pack()
    
    typedef struct tagSTRUCTNOALIGN
    {
        int alignedInt;
        double alignedDouble;
        char alignedChar;
    }struct_no_align_t;
    
    typedef struct ALIGNED_(64) tagSTRUCTALIGNED64
    {
        int alignedInt;
        double alignedDouble;
        char alignedChar;
    }struct_aligned_64_t;
    
    
    typedef struct tagSTRUCTWITHALIGNEDMEMBERS
    {
        int ALIGNED_(8) alignedInt;
        double ALIGNED_(16) alignedDouble;
        char ALIGNED_(2) alignedChar;
    }struct_with_aligned_members_t;
    
    int main(int argc, char **argv)
    {
        int i,j;
        struct_packed_t _packed;
        struct_no_align_t _noalign;
        struct_aligned_64_t _aligned64;
        struct_with_aligned_members_t _alignedmembers;
    
        char* names[] = {"_packed","_noalign","_aligned64","_alignedmembers"};
        char* ptrs[] = {(char*)&_packed,(char*)&_noalign,(char*)&_aligned64,(char*)&_alignedmembers};
        size_t sizes[] = {sizeof(_packed),sizeof(_noalign),sizeof(_aligned64),sizeof(_alignedmembers)};
        size_t alignments[] = {2,4,8,16,32,64};
        int alcount = sizeof(alignments)/sizeof(size_t);
    
        for(i = 0; i < 4; i++)
        {
            printf("Addrof %s: %x\n", names[i], ptrs[i]);
            printf("Sizeof %s: %d\n", names[i], sizes[i]);
            for(j = 0; j < alcount; j++)
                printf("Is %s aligned on %d bytes? %s\n", 
                    names[i], 
                    alignments[j], 
                    ((size_t)ptrs[i])%alignments[j] == 0 ? "YES" : "NO");
        }
    
        for(j = 0; j < alcount; j++)
        {
                printf("Is _alignedmember.alignedInt aligned on %d bytes? %s\n", 
                        alignments[j], 
                        ((size_t)&_alignedmembers.alignedInt)%alignments[j] == 0 ? "YES" : "NO");
                printf("Is _alignedmember.alignedDouble aligned on %d bytes? %s\n", 
                        alignments[j], 
                        ((size_t)&_alignedmembers.alignedDouble)%alignments[j] == 0 ? "YES" : "NO");
                printf("Is _alignedmember.alignedChar aligned on %d bytes? %s\n", 
                        alignments[j], 
                        ((size_t)&_alignedmembers.alignedChar)%alignments[j] == 0 ? "YES" : "NO");
        }
    
        return 0;
    }
    

    Hope this helps…

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

Sidebar

Related Questions

I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular
I have program, that has structure defined like this: struct foo { int magic;
I am trying to create a struct that has multiple string arrays inside of
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create a json object to send as a post array,
I would like to create set of strings and below is the only limitation.
I would like to create a virtual host in apache2, but I want it
I would like to create and hold on to an iterator_range. The range is

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.