473,387 Members | 1,812 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,387 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 2874
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.