GET THE SIZE of struct in the PREPROCESSOR

R

requinham

Hello,

i have a structure in my program and i want generate a compile error
when the size of structure is not pow of two.

my problem is that i don't found the way to get the size of structure
in preprocessor mode and it's necessary to generate a COMPILE ERROR
when the size of my struct is not pow of 2 :(

thank you
 
M

Marcel Müller

Hi!
i have a structure in my program and i want generate a compile error
when the size of structure is not pow of two.

my problem is that i don't found the way to get the size of structure
in preprocessor mode and it's necessary to generate a COMPILE ERROR
when the size of my struct is not pow of 2 :(

The standard does not allow that. Some compilers support it anyway.
(e.g. Borland as far as I remember)

You might want to have a look at BOOST_STATIC_ASSERT or C++0x
static_assert if your compiler supports the latter.


Marcel
 
J

James Kanze

i have a structure in my program and i want generate a compile
error when the size of structure is not pow of two.
my problem is that i don't found the way to get the size of
structure in preprocessor mode and it's necessary to generate
a COMPILE ERROR when the size of my struct is not pow of 2 :(

It's impossible; the preprocessor was designed so that it could
be implemented as a separate program, without any knowledge of
what the symbols actually mean.

The simplest way to get a compile time error in such cases is to
design some sort of expression which evaluates to true or false,
and define an array with the results of this expression as its
size. Since false becomes 0, and you can't define an array with
a size of 0, you get a compiler error. (I seem to remember some
older compiler where you could define an array [0]. In that
case, you use 2*x-1 as the dimension, where x is your
expression.) For your case, something like:
char must_be_a_power_of_two[ (N & (N-1)) == 0 ];
should do the trick (to test for N).
 
R

requinham

Thx for all your help i found the best solution like this :

#define CHECK_SIZE_POW2(a) ((a) && !((a) & ((a)-1)))
#define SIZE_IS_POW_OF_TWO(x) ( CHECK_SIZE_POW2(sizeof(x)))//
#define STATIC_ASSERT(b) int _static_assert[ b ? 1 : -1 ];

// Usage:

typedef struct Foo
{
unsigned int a;
unsigned char b;
unsigned char c[24];
}foo;

STATIC_ASSERT(SIZE_IS_POW_OF_TWO(Foo));

what is your opinion ?
 
M

Michael Doubez

Thx for all your help i found the best solution like this :

#define CHECK_SIZE_POW2(a) ((a) && !((a) & ((a)-1)))

This won't work for 1:
1 & (1-1) == 0

You don't need the first check, replace it with a!=1.
#define SIZE_IS_POW_OF_TWO(x)  ( CHECK_SIZE_POW2(sizeof(x)))
#define STATIC_ASSERT(b) int _static_assert[ b ? 1 : -1 ];

// Usage:

typedef struct Foo
{
        unsigned int a;
        unsigned char b;
        unsigned char c[24];

}foo;

STATIC_ASSERT(SIZE_IS_POW_OF_TWO(Foo));

what is your opinion ?

I would prefer:

STATIC_ASSERT( IS_POW_OF_TWO(sizeof(x)) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top