Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check Structure Byte Alignment

Posted on 2000-05-02
9
Medium Priority
?
256 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 150 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

916 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