Using explicitly sized variables in functions as auto variables or parameters

A

Adel

Hello.
Is the statement below proved by the ANSI C standard?
"It is undesirable to use explicitly sized variables in functions as
auto variables or parameters. The values will always be stored as the
processor native word size, and extra code will be generated by the
compiler to mask off bits that are not significant in the result."

Adel
 
E

Eric Sosman

Adel said:
Hello.
Is the statement below proved by the ANSI C standard?
"It is undesirable to use explicitly sized variables in functions as
auto variables or parameters. The values will always be stored as the
processor native word size, and extra code will be generated by the
compiler to mask off bits that are not significant in the result."

If by "explicitly sized variable" you mean types like
int8_t or uint_least32_t, then no: The Standard describes
how these types must behave, but does not specify how the
implementation produces the behavior. In particular, it
does not specify that extra code will be generated, nor
that the values will be stored in a "native" word size.

If by "explicitly sized variable" you mean something
else, I need you to explain it to me.

Advice: Use things like int_fast16_t when your program
needs the services they provide, and don't fret about micro-
optimizing UNTIL AND UNLESS you have measurements that show
it to be necessary. "Premature optimization is the root
of all evil."
 
C

CBFalconer

Eric said:
If by "explicitly sized variable" you mean types like
int8_t or uint_least32_t, then no: The Standard describes
how these types must behave, but does not specify how the
implementation produces the behavior. In particular, it
does not specify that extra code will be generated, nor
that the values will be stored in a "native" word size.

If by "explicitly sized variable" you mean something
else, I need you to explain it to me.

Advice: Use things like int_fast16_t when your program
needs the services they provide, and don't fret about micro-
optimizing UNTIL AND UNLESS you have measurements that show
it to be necessary. "Premature optimization is the root
of all evil."

In fact such actions as masking off a long to 32 bits, or a char to
8 bits, are very likely to be automatically optimized away by the
code generator when unnecessary.
 
J

Jack Klein

Hello.
Is the statement below proved by the ANSI C standard?

The C standard does not prove anything. It neither needs to nor
attempts to.
"It is undesirable to use explicitly sized variables in functions as

The word "undesirable" does not appear in the C standard.
auto variables or parameters. The values will always be stored as the

The standard says nothing at all about how objects are laid out in
memory, let alone any "always" about them.
processor native word size, and extra code will be generated by the
compiler to mask off bits that are not significant in the result."

Adel

If you want proof for a non-technical, opinionated statement that, if
it can be answered at all, can only be answered for specific
implementations on specific architectures, I would suggest you ask the
person who made the statement to provide it.

Tell them to put up or shut up, in other words.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top