Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>#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; </code></pre> <p>You can test this with the following code (notice the #pragma pack --> This is for MSVC)</p> <pre><code>#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*)&amp;_packed,(char*)&amp;_noalign,(char*)&amp;_aligned64,(char*)&amp;_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 &lt; 4; i++) { printf("Addrof %s: %x\n", names[i], ptrs[i]); printf("Sizeof %s: %d\n", names[i], sizes[i]); for(j = 0; j &lt; 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 &lt; alcount; j++) { printf("Is _alignedmember.alignedInt aligned on %d bytes? %s\n", alignments[j], ((size_t)&amp;_alignedmembers.alignedInt)%alignments[j] == 0 ? "YES" : "NO"); printf("Is _alignedmember.alignedDouble aligned on %d bytes? %s\n", alignments[j], ((size_t)&amp;_alignedmembers.alignedDouble)%alignments[j] == 0 ? "YES" : "NO"); printf("Is _alignedmember.alignedChar aligned on %d bytes? %s\n", alignments[j], ((size_t)&amp;_alignedmembers.alignedChar)%alignments[j] == 0 ? "YES" : "NO"); } return 0; } </code></pre> <p>Hope this helps...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload