Are these behaviors defined?

  • Thread starter William L. Bahn
  • Start date
W

William L. Bahn

I'm working on some lessons and want to be sure I get some stuff correct. If
any of this is covered in the FAQ, please feel free to point me to the
section - I couldn't find it.

----------------------------------------------------------------
Overflow/Wraparound behavior of integer values
----------------------------------------------------------------

If an arithmetic operation such as (+, -, *) yields a result that exceeds
the bounds, is the behavior in any way defined? I know that, for instance,
integers will generally wrap around - but is this the defined behavior?

----------------------------------------------------------------
Logical Operators on floating point values
----------------------------------------------------------------

I know that the bitwise operators are not defined for floating point
representations (though I have no idea why unless it might be to avoid
imposing behavior that might be more difficult to implement if the values
involved are in a floating point unit - any thoughts?). My question is the
logical operators (!, &&, and ||). Are the behaviors of these defined at
all?
 
J

Joona I Palaste

William L. Bahn said:
I'm working on some lessons and want to be sure I get some stuff correct. If
any of this is covered in the FAQ, please feel free to point me to the
section - I couldn't find it.
If an arithmetic operation such as (+, -, *) yields a result that exceeds
the bounds, is the behavior in any way defined? I know that, for instance,
integers will generally wrap around - but is this the defined behavior?

Unsigned integer types will wrap around, this is defined behaviour. The
behaviour for signed integer types is implementation-defined IIRC.
Are the behaviors of functions like free(), fprintf(), fclose(), getc() and
others defined if they are passed a NULL stream pointer?

free(NULL) is a safe no-op (however passing a stream pointer to free
causes undefined behaviour). fflush(NULL) flushes all currently open
output streams. I don't think it is defined for any other function.
By this, I mean is a float guaranteed to be a four-byte IEEE-754 single
precision representation and a double an eight-byte double precision
representation, or are these only minimum requirements.

I don't think it's required to be IEEE-754 at all, but I could be
mistaken.
 
C

CBFalconer

William L. Bahn said:
I'm working on some lessons and want to be sure I get some stuff
correct. If any of this is covered in the FAQ, please feel free
to point me to the section - I couldn't find it.

----------------------------------------------------------------
Overflow/Wraparound behavior of integer values
----------------------------------------------------------------

If an arithmetic operation such as (+, -, *) yields a result that
exceeds the bounds, is the behavior in any way defined? I know
that, for instance, integers will generally wrap around - but is
this the defined behavior?

no, the behavior is specifically undefined. Don't do that.

Also, please try to keep your line length well below 80, so that
quotes do not wrap. 65 is a good value to aim for. I have
reformatted the quotes here.
----------------------------------------------------------------
Logical Operators on floating point values
----------------------------------------------------------------

I know that the bitwise operators are not defined for floating
point representations (though I have no idea why unless it might
be to avoid imposing behavior that might be more difficult to
implement if the values involved are in a floating point unit
- any thoughts?). My question is the logical operators (!, &&,
and ||). Are the behaviors of these defined at all?

No. Because the bit format of floats is not specified.
----------------------------------------------------------------
Standard I/O Functions when given NULL stream pointers
----------------------------------------------------------------

Are the behaviors of functions like free(), fprintf(), fclose(),
getc() and others defined if they are passed a NULL stream
pointer?

free() doesn't receive a stream pointer, and free(NULL) is
allowable and a no op. The others mentioned, no. In general,
read the functions specification in the standard. A free version
is N869 (google for it). Get the text version, which is easily
searched with grep or an editor to find those definitions.
----------------------------------------------------------------
Value of bits shifted in by >> and <<
----------------------------------------------------------------

Are the values that are shifted into the msb's of an integer when
it is shifted to the left (and the lsb's when shifted to the
right) defined in any way?

for left shift, 0. For right shift, implementation defined.
----------------------------------------------------------------
Are float and double representations fixed?
----------------------------------------------------------------

By this, I mean is a float guaranteed to be a four-byte IEEE-754
single precision representation and a double an eight-byte double
precision representation, or are these only minimum requirements.

Absolutely not. See the standard.
 
J

Jack Klein

I'm working on some lessons and want to be sure I get some stuff correct. If
any of this is covered in the FAQ, please feel free to point me to the
section - I couldn't find it.

----------------------------------------------------------------
Overflow/Wraparound behavior of integer values
----------------------------------------------------------------

If an arithmetic operation such as (+, -, *) yields a result that exceeds
the bounds, is the behavior in any way defined? I know that, for instance,
integers will generally wrap around - but is this the defined behavior?

The unsigned integer types cannot overflow. If a calculation produces
an out-of-range result, it is adjusted by adding or subtracting
(maximum value of the unsigned type + 1) until the result is within
range. This is often called 'modulo arithmetic'.

Overflow of signed integer types produces undefined behavior. Nothing
at all is guaranteed.
----------------------------------------------------------------
Logical Operators on floating point values
----------------------------------------------------------------

I know that the bitwise operators are not defined for floating point
representations (though I have no idea why unless it might be to avoid
imposing behavior that might be more difficult to implement if the values
involved are in a floating point unit - any thoughts?). My question is the
logical operators (!, &&, and ||). Are the behaviors of these defined at
all?

The logical operators are defined for all legal expressions, other
than those of type void.

if(float_var) yields the value 0 if float_var is exactly 0.0 and 1
otherwise.

As others have said, free() is not a FILE * function. The others you
have mentioned produce undefined behavior if passed a null pointer.
In general, the C standard states that all functions receiving pointer
arguments have undefined behavior if passed an invalid or null
pointer, unless specifically documented otherwise.
----------------------------------------------------------------
Value of bits shifted in by >> and <<
----------------------------------------------------------------

Are the values that are shifted into the msb's of an integer when it is
shifted to the left (and the lsb's when shifted to the right) defined in any
way?

For left shifts and right shifts of unsigned integer types and signed
integer types with positive values, zero bits are shifted in. For
right shifts of signed integer types with negative values, it is
implementation-defined whether one or zero bits are shifted in. The
compiler must document which behavior it uses.


No, they are completely unspecified by the language. It is entirely
up to the compiler and the underlying hardware platform. IEEE
representation is common on newer platforms, but not required in any
way.
 
W

William L. Bahn

Thanks to all who have responded. To summarize (please correct any errors):


----------------------------------------------------------------
Overflow/Wraparound behavior of integer values
----------------------------------------------------------------

Defined for integer types with modulo (or 'wraparound') behavior.
Undefined for signed types.

----------------------------------------------------------------
Logical Operators on floating point values
----------------------------------------------------------------

Got mixed feedback on this one. One said no, because the bit
pattern is not defined. One said yes, following the same rules as
other expressions. Since a NULL pointer is regarded as false in
an expression regardless of its interal representation, either answer
seems reasonable to me.

I've downloaded N869 and, by ommision more than anything,
it appears to support the second claim - that a floating point
value would evaluate as true just as in the expression (!(0 = = x)).

Similarly, it looks like the bitwise operators are not undefined for
floating point values - again, by ommision as it doesn't say that
they aren't defined.

But are promotions done on bitwise operands? In other words,
if I were to create a bitmask designed to maskoff all but the exponent
of a floating point value (based on knowledge of the implementation
of a float, of course) and did a bitwise & between it and the float
in question, would it promote my mask to a floating point representation?

I can't determine the answer to any of this based on searching N869
so would really appreciate someone pointing out the sections that
spell it out.

----------------------------------------------------------------
Standard I/O Functions when given NULL stream pointers
----------------------------------------------------------------

Not defined except for specific functions.

----------------------------------------------------------------
Are float and double representations fixed?
----------------------------------------------------------------

No. Any they are not required to be IEEE-754 at all.

This answers another question that I had with regards to why the
FAQ says that setting a block of memory to all zeros is not a safe
operation if the data in the block is supposed to be either pointers
or floating point values. The pointers part I understood, but I didn't
understand the floating point part since all zeros is zero in an
IEEE 754 representation.
 
C

Christian Bau

"William L. Bahn said:
Similarly, it looks like the bitwise operators are not undefined for
floating point values - again, by ommision as it doesn't say that
they aren't defined.

May I quote: "Some operators (... collectively called the bitwise
operators) are required to have operands that have integer types.

Anyway, anything that is ommited is undefined. If it is not defined,
then it is undefined.
 
W

William L. Bahn

Christian Bau said:
May I quote: "Some operators (... collectively called the bitwise
operators) are required to have operands that have integer types.

Okay - I found that. Thank you.

I also see now where each of the operators states a constraint that it
must be of integer type. Don't know how I missed that before.
Anyway, anything that is ommited is undefined. If it is not defined,
then it is undefined.

That's a nice platitude, but it is frequently not very helpful when delving
through a several hundred page document where so much is defined
indirectly through several layers of arcane syntax.
 
P

pete

His reason suggests that he thought that he was replying to
"Bitwise Operators on floating point values"

(!x) is semantically identical to (x == 0).
If x is a floating type, then those expressions
are also equivalent to (x == 0.0),
and (x || 0) is equivalent to (x != 0.0)
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top