473,241 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Structure size check at compile time

Hi

I'm porting some C++ code to new platforms and have some 1-byte aligned
structures which need a specific size. Since datatypes can vary on different
platforms (which I found out the hard way since longs are not the same size
on win64 and linux x64) I would like to do a check at compile time to make
sure things are correct. This will ease future work.

Is it possible to do something like the following at preprocessing stage:

typedef struct _aaa
{
UINT32 a;
UINT16 b;
}aaa;

#if sizeof(aaa) != (4 + 2)
#error "Wrong settings for this platform"
#endif

The compiler seems to fail no matter which platform I use. I know I can do
this at runtime but it's not useful code so I rather not have it done at
runtime. The only alternative is to enable it as a runtime check when
compiling in debug mode.

Thanks in advance.

-- John
Jan 5 '06 #1
4 8822
John Smith wrote:
Hi
Is it possible to do something like the following at preprocessing stage:
Not at preprocessing, but at compile time.
typedef struct _aaa
{
UINT32 a;
UINT16 b;
}aaa;
Isn't this same as
struct aaa {
UNIT32 a;
UINT16 b;
};

Also, UNIT32 and UNIT16 are not std C++ types . So I believe that they
are platform dependent typedefs or new type definitions.

#if sizeof(aaa) != (4 + 2)
This doesnot work because sizeof is a _compile-time_ operator, and it
is being used at preprocessing time
#error "Wrong settings for this platform"
#endif


Something on the lines of CTAssert (Ref. Modern C++ Design)

template<int i> class CorrectSize;
template<> class CorrectSize<6> {
};

int main()
{
CorrectSize<sizeof(aaa)>(); // Gives compile time error if
sizeof(aaa) is anything other than 6
}

Jan 5 '06 #2
John Smith wrote:
Hi

I'm porting some C++ code to new platforms and have some 1-byte aligned
structures which need a specific size. Since datatypes can vary on different
platforms (which I found out the hard way since longs are not the same size
on win64 and linux x64) I would like to do a check at compile time to make
sure things are correct. This will ease future work.

Is it possible to do something like the following at preprocessing stage:

typedef struct _aaa
{
UINT32 a;
UINT16 b;
}aaa;

#if sizeof(aaa) != (4 + 2)
#error "Wrong settings for this platform"
#endif

The compiler seems to fail no matter which platform I use. I know I can do
this at runtime but it's not useful code so I rather not have it done at
runtime. The only alternative is to enable it as a runtime check when
compiling in debug mode.

Thanks in advance.

-- John


Does your *preprocessor* really evaluate the size of objects? I bet
that's your problem right there. At best, the result of a "sizeof"
isn't known until the compiler runs, and at worst, it isn't known until
run time. Assuming that your struct's size can be evaluated at
compile-time and your compiler is suffiently smart, just put something
like this at the begining of your program (warning: not tested):

if (sizeof(aaa) != (4 + 2))
{
cerr << "Wrong settings for this platform" << endl;
abort();
}

On platforms where the size of the struct really is 6, the compiler will
detect that the condition is always false and optimize it out
completely. On platforms where it isn't, the compiler might even
optimize out the rest of the program.

If you must check this at compile time, I'd suggest putting a check into
your build system. GNU autoconf, for instance, will allow you to
compile and run a trivial program that checks the size of your struct,
and trigger an alert if it's not the size you expected before attempting
to compile your program.

Rennie deGraaf
Jan 5 '06 #3
you may want to do something like this:

//////////////////////////////////////////////////
template<int i> class ThrowError;
template<> class ThrowError<0>
{
public:
enum {val = 0};
};

template<class T, int expected_size>
class CheckSize
{
enum { value = (sizeof(T) == expected_size) ? 0 : 1};
enum { errval = ThrowError<value>::val };
};

/////////////////////////////////////////////////////
class Test
{
UINT32 i;
};

int main()
{
CheckSize<Test, 4>(); // compiles ok, sizeof Test is 4 bytes
CheckSize<Test, 8>(); // compilation, error, size is incorrect
return 0;
}

Jan 5 '06 #4
Rennie deGraaf wrote:
Does your *preprocessor* really evaluate the size of objects? I bet
that's your problem right there. At best, the result of a "sizeof"
isn't known until the compiler runs, and at worst, it isn't known until
run time.


sizeof is always evaluated at compile time, but not by the preprocessor.

Jan 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
7
by: Jimakos Bilakis | last post by:
Hi guys! I'm using the C++ Builder 6 Enterprise Edition where I create some tables in Paradox and with the help of a structure i pass my data from the form (Edit boxes) to the Paradox table with...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
6
by: AlabiChin | last post by:
Hello, I'm trying to find out if it is possible to dynamically add or remove fields for a structure. I'm in a situation where I don't know how many items I want to store and, therefore, will not...
56
by: Bruce. | last post by:
I would like to allocate a structure size of 1024 bytes but I want the compiler to do the calculation for me. typedef struct { int var1; int var2; int var3; char ...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.