473,287 Members | 1,395 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,287 software developers and data experts.

enum { int_size_checked = 1/(sizeof(int)==4) };

Does this reliably cause a compile-time error
when int is not 4 bytes ?

enum { int_size_checked = 1/(sizeof(int)==4) };
Any better way to check the value of an expression
involving sizeof before runtime ? I also have:
{
void check_foo_size(void);
if (sizeof(foo)!=120) check_foo_size();
}

but I can't be sure this one will NOT generate a link-time
error, and a name collision is hard to rule out.
François Grieu
Dec 20 '06 #1
5 2868
On Wed, 20 Dec 2006 15:26:42 +0100, Francois Grieu wrote:
>Any better way to check the value of an expression
involving sizeof before runtime ?
http://www.embedded.com/showArticle....leID=164900888

Dec 20 '06 #2
Francois Grieu wrote:
Does this reliably cause a compile-time error
when int is not 4 bytes ?

enum { int_size_checked = 1/(sizeof(int)==4) };
Any better way to check the value of an expression
involving sizeof before runtime ? I also have:
{
void check_foo_size(void);
if (sizeof(foo)!=120) check_foo_size();
}

but I can't be sure this one will NOT generate a link-time
error, and a name collision is hard to rule out.
Difficult. It is one thing what the C Standard says, and another thing
what your compiler does.

In C99, your enum would be an error if sizeof (int) != 4 (exactly as
you want); on C90 it might be undefined behavior; I am not hundred
percent sure. I used to use a macro that evaluates to

sizeof ("" == (sizeof (int) != 4))

If sizeof (int) == 4, we get sizeof ("" == 0). 0 is a null pointer
constant, comparing char* and a null pointer constant gives a result of
type int, and sizeof (...) compiles. If sizeof (int) != 4, we get
sizeof ("" == 1) which compares a pointer and an int, which is not
legal, so you get an error. Not, however, when you use gcc. It is a
warning...

Another approach:

switch (0) { case 0: case sizeof (int) == 4: ; }

If sizeof (int) != 4, you have to labels case 0: which is not allowed.
Just check that it doesn't produce any code.

Dec 20 '06 #3
christian.bau wrote:
Francois Grieu wrote:
>Does this reliably cause a compile-time error
when int is not 4 bytes ?

enum { int_size_checked = 1/(sizeof(int)==4) };
Any better way to check the value of an expression
involving sizeof before runtime ? I also have:
{
void check_foo_size(void);
if (sizeof(foo)!=120) check_foo_size();
}

but I can't be sure this one will NOT generate a link-time
error, and a name collision is hard to rule out.

Difficult. It is one thing what the C Standard says, and another thing
what your compiler does.

In C99, your enum would be an error if sizeof (int) != 4 (exactly as
you want); on C90 it might be undefined behavior; I am not hundred
percent sure. I used to use a macro that evaluates to

sizeof ("" == (sizeof (int) != 4))

If sizeof (int) == 4, we get sizeof ("" == 0). 0 is a null pointer
constant, comparing char* and a null pointer constant gives a result of
type int, and sizeof (...) compiles. If sizeof (int) != 4, we get
sizeof ("" == 1) which compares a pointer and an int, which is not
legal, so you get an error. Not, however, when you use gcc. It is a
warning...

Another approach:

switch (0) { case 0: case sizeof (int) == 4: ; }

If sizeof (int) != 4, you have to labels case 0: which is not allowed.
Just check that it doesn't produce any code.
We have four possible solutions:

1. enum { int_size_checked = 1/(sizeof(int)==SIZE) };

2. int foo;
{
void check_foo_size(void);
if (sizeof(foo)!=SIZE) check_foo_size();
}

3. sizeof ("" == (sizeof (int) != SIZE));

4. switch (0) { case 0: case sizeof (int) == SIZE: ; }

Where we can vary the value SIZE in each to test behaviour when correct
and not correct on any given compiler.

We should look at each solution with the following metrics:
1. Does it prevent compilation when size is wrong? (good)
2. Does it prevent compilation when size is right? (bad)
3. Does it only issue warnings when size wrong? (bad)
4. Does it issue warnings when size is right? (bad)

Solution 1:

lc: issues warning and error when size is wrong;
no warning or error when size is right
bcc32: issues warning only when size is wrong;
no warning or error when size is right
gcc: issues warning and error when size is wrong;
no warning or error when size is right
cl: issues error when size is wrong;
no warning or error when size is right
tcc: issues error when size is wrong;
no warning or error when size is right

So solution 1 works nicely on all 5 compilers tested.

Solution 2:

lc: issues error when size is wrong;
issues error when size is right
bcc32: issues warning and error when size is wrong;
issues warning and error when size is right
gcc: issues error when size is wrong;
no warning or error when size is right
cl: issues error when size is wrong;
issues error when size is right
tcc: issues error when size is wrong;
issues error when size is right

So solution 2 is unusable on all but one compiler. The reference to
check_foo_size remains and the linker trips up even when the size is
right. The only compiler which removes the reference to check_foo_size
is gcc.

Solution 3:

lc: issues error when size is wrong;
no warning or error when size is right
bcc32: issues 2 warnings when size is wrong;
issues 1 warning when size is right
gcc: issues warning when size is wrong;
no warning or error when size is right
cl: issues warning when size is wrong;
no warning or error when size is right
tcc: issues 2 warnings when size is wrong;
issues 1 warning when size is right

So solution 3 is unusable on all but one compiler. Only lc considers the
comparison of pointer and integer as an error worth stopping compilation
over. And two of the compilers, bcc32 and tcc issue warnings no matter
what the situation.

Solution 4:

lc: issues error when size is wrong;
no warning or error when size is right
bcc32: issues 1 warning and 3 errors when size is wrong;
no warning or error when size is right
gcc: issues 2 errors when size is wrong;
no warning or error when size is right
cl: issues error when size is wrong
no warning or error when size is right
tcc: issues error when size is wrong
no warning or error when size is right

So solution 4 works nicely on all 5 compilers tested.

All compilers were tested with no options specified. Additional options
that enable or disable warnings or turn warnings into errors will of
course change the results.

These are the full names and versions of the 5 compilers used:

Logiciels/Informatique lcc-win32 version 3.8. Compilation date: Sep 15
2006 13:56:00

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86

Turbo C Version 2.01 Copyright (c) 1987, 1988 Borland International

--
Simon.
Dec 21 '06 #4
Francois Grieu wrote:
>
Does this reliably cause a compile-time error
when int is not 4 bytes ?

enum { int_size_checked = 1/(sizeof(int)==4) };

Any better way to check the value of an expression
involving sizeof before runtime ?
You might be able to achieve what you want with:

#include <limits.h>
#include <assert.h>

assert(CHAR_BIT == 8 && UINT_MAX == 0xffffffffu);

.... depending of course, on just exactly what it is that you want.

If the assertion is true, then sizeof(int) equals 4.
If the assertion is false,
then sizeof(int) may or may not equal 4.
I also have:
{
void check_foo_size(void);
if (sizeof(foo)!=120) check_foo_size();
}

but I can't be sure this one will NOT generate a link-time
error, and a name collision is hard to rule out.
--
pete
Dec 21 '06 #5
Francois Grieu <fg****@gmail.comwrites:
Any better way to check the value of an expression
involving sizeof before runtime ?
switch (0) {
case 1:
case sizeof(int) != 4:
;
}

If sizeof(int) is not 4, then you'll get an error from a
duplicate case value.

Another possibility:
int dummy[sizeof(int) == 4 ? 1 : -1];
If sizeof(int) is not 4, the array ends up with an invalid size.

(Neither idea is original with me.)
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Dec 21 '06 #6

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.