Velocity Reviews > Are these behaviors defined?

# Are these behaviors defined?

William L. Bahn
Guest
Posts: n/a

 08-07-2004
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?

----------------------------------------------------------------
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?

----------------------------------------------------------------
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?

----------------------------------------------------------------
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.

Joona I Palaste
Guest
Posts: n/a

 08-07-2004
William L. Bahn <(E-Mail Removed)> scribbled the following:
> 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?

Unsigned integer types will wrap around, this is defined behaviour. The
behaviour for signed integer types is implementation-defined IIRC.

> ----------------------------------------------------------------
> 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(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.

> ----------------------------------------------------------------
> 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.

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

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon

CBFalconer
Guest
Posts: n/a

 08-07-2004
"William L. Bahn" wrote:
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Jack Klein
Guest
Posts: n/a

 08-07-2004
On Sat, 7 Aug 2004 05:16:04 -0600, "William L. Bahn"
<(E-Mail Removed)> wrote in comp.lang.c:

> 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
(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.

> ----------------------------------------------------------------
> 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?

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.

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

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

William L. Bahn
Guest
Posts: n/a

 08-07-2004
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.

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.

Christian Bau
Guest
Posts: n/a

 08-07-2004
In article <(E-Mail Removed)>,
"William L. Bahn" <(E-Mail Removed)> wrote:

> 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.

William L. Bahn
Guest
Posts: n/a

 08-08-2004

"Christian Bau" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> "William L. Bahn" <(E-Mail Removed)> wrote:
>
> > 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.

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.

pete
Guest
Posts: n/a

 08-08-2004
William L. Bahn wrote:

> ----------------------------------------------------------------
> Logical Operators on floating point values
> ----------------------------------------------------------------
>
> Got mixed feedback on this one. One said no, because the bit
> pattern is not defined.

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)