Solved

Check Structure Byte Alignment

Posted on 2000-05-02
9
205 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

14 Experts available now in Live!

Get 1:1 Help Now