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
Solved

Check Structure Byte Alignment

Posted on 2000-05-02
9
232 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
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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
mixString challenge 36 130
repeateFront java challenge 31 94
Turn a spreadsheet into a vba executable. 2 92
changeXy challenge 13 87
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

789 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