Solved

Check Structure Byte Alignment

Posted on 2000-05-02
9
218 Views
Last Modified: 2013-11-20
Can i have some sample code to detect the structure byte alignment in the Project's setting during compilation.

If the setting is other then the default 8-byte alignment, prompt a message in the VC's output window and stop compiling.
0
Comment
Question by:leowlf
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 2770215

#include <stddef.h>
#include <stdio.h>

#ifndef CTASSERT_H
#define CTASSERT_H

// Use of ctassert<E>, where E is a constant expression,
// will cause a compile-time error unless E evaulates to
// a nonzero integral value.

template <bool t>
struct ctassert {
  enum { N = 1 - 2 * int(!t) };
    // 1 if t is true, -1 if t is false.
  static char A[N];
};

template <bool t>
char ctassert<t>::A[N];

#endif



void main()
{

#pragma pack(4)
   struct _test_struct_align { char a; double b; };

   printf("%d, %d\n", offsetof(struct _test_struct_align, a),
      offsetof(struct _test_struct_align, b));
   printf("%d\n", sizeof(struct _test_struct_align));

   // the below declaration will fail with error C2466 if the
   // packing size isn't exactly 8.

   ctassert <offsetof(struct _test_struct_align, b) == 8 > x;
}


0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 2770227
The problem is divided into two parts; finding the packing, and asserting that it's what you want.

The compile-time assert I use is a little hacky, so I borrowed one from one of the journals.

The code above will make a compile-time error if you've got a packing of 1, 2, or 4. But it won't work if you've got a packing of 16--I can't find a way to do that, since there are no intrinsic datatypes which have a size of at least 16 bytes.

..B ekiM

0
 

Author Comment

by:leowlf
ID: 2886886
Answer accepted
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2887971
Why only a "B" and not an "A"?  Was there something else that you needed?

..B ekiM
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:mikeblas
ID: 2891878
Why only a "B" and not an "A"?  Was there something else that you needed?

..B ekiM
0
 

Author Comment

by:leowlf
ID: 2903880
"..it won't work if you've got a packing of 16 ..."  :(
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2913421
> "..it won't work if you've got a packing of 16 ..."  

That's because 16-byte packing is broken in current versions of the compiler. That's not a shortcoming of my technique.

..B ekiM
0
 

Author Comment

by:leowlf
ID: 2916585
i see, thanks.  
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2916778
> i see, thanks.  

Why didn't you ask about that before grading? Now, because of that misunderstanding, I'm stuck with a bad grade.

Since it's been so long, I assumed you decided to ignore the question.

..B ekiM
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now