Solved

Check Structure Byte Alignment

Posted on 2000-05-02
9
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between String.subString() and String.subSequence() 6 262
FizzBuzz challenge 9 84
Problem to event 3 112
Problem to App source 6 89
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 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