Int promotion

F

fasf

Hi,
I'm reading about int promotion: char and short shoud be avoided
because are changed in int variables during arithmetic operations...So
i shoud always avoid char and short variables? These variables are
smaller in byte size, but new instructions (like sign extending) are
implemented at compiler time: these instructions occupy memory bytes
so (apparently) there is no advantage to use short data type....or
not?
 
F

Flash Gordon

fasf said:
Hi,
I'm reading about int promotion: char and short shoud be avoided

That is an exaggeration. They should be used when appropriate and not
when they are not appropriate.
because are changed in int variables during arithmetic operations...

They get promoted, yes.
So
i shoud always avoid char and short variables?
No.

These variables are
smaller in byte size, but new instructions (like sign extending) are
implemented at compiler time: these instructions occupy memory bytes
so (apparently) there is no advantage to use short data type....or
not?

On some processors sign extension is a "free" operation, i.e. one that
takes no additional time and can be done on loading the value in to the
register where it is needed.

Also, sometimes you have a large array of data, in which case the saving
in space can make a big difference and even improve performance.

Finally consider this. Why were the types included in the language if
they were of no use?
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I'm reading about int promotion: char and short shoud be avoided
because are changed in int variables during arithmetic operations...So
i shoud always avoid char and short variables? These variables are
smaller in byte size, but new instructions (like sign extending) are
implemented at compiler time: these instructions occupy memory bytes
so (apparently) there is no advantage to use short data type....or
not?

For arithmetic operation, everything less than an `int' is converted to
either `int' or `unsigned int', depending whether the source type is a
subset of `int'. In theory, these operation should be performed on type
`int' but an optimizing compiler can omit these conversions when
appropriate.

Consider the following header:

size_t printf(const char *fmt, ...);

When passing `char's and `short's to the variable arguments part, they will
be promoted to `int's prior to passing. Therefore, when you try to read the
arguments, you need to take an `int' out when a `char' or `short' is
expected. Similar for `float' and `double'. However, This promotion does not
occur when a function prototype is in effect. Instead, the argument is
convert to the type of the function prototype.

Consider the following pair of old-style function declaration and definition
(very old K&R C without the void keyword):

main(argc, argv)
int argc;
char **argv;
{
unsigned char x = 0;
foo(x); /* foo is implicitly declared as `int foo(...)' */
return 0;
}

foo(n)
unsigned char n;
{
// blah
}

Here, n is converted from a `unsigned char' to an `int' (possibly `unsigned
int' if `char' is the same size of `int') in main(), passed into foo, and
converted back to `unsigned char'. To modify the declaration into prototype
form, you should write this (standard C90):

int foo(int);

int main()
int argc;
char **argv;
{
unsigned char x = 0;
foo(x);
return 0;
}

int foo(n)
unsigned char n;
{
// blah
}

Later, to convert the definition to prototype form:

int foo(int);

int main(int argc, char **argv)
{
unsigned char x = 0;
foo(x);
return 0;
}

int foo(int _n)
{
unsigned char n = _n;
// blah
}

In general, although some CPU does provide byte/short operation directly, it
is better not using them unless memory is very tight.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAks3Z0kACgkQG6NzcAXitM+rIQCfSPqKhya1RWQCIppE/+px4+pw
AjwAnAvwS0SyKAjCTP0V3J3QqqMc+CxK
=638p
-----END PGP SIGNATURE-----
 
E

Eric Sosman

fasf said:
Hi,
I'm reading about int promotion: char and short shoud be avoided
[... good response snipped ...]
Finally consider this. Why were the types included in the language if
they were of no use?

To keep gets() company?
 
F

Flash Gordon

Eric said:
fasf said:
Hi,
I'm reading about int promotion: char and short shoud be avoided
[... good response snipped ...]
Finally consider this. Why were the types included in the language if
they were of no use?

To keep gets() company?

If getc is get a char, gets should be get a short ;-)
 
E

Eric Sosman

Flash said:
Eric said:
On 12/27/2009 7:53 AM, Flash Gordon wrote:
fasf wrote:
Hi,
I'm reading about int promotion: char and short shoud be avoided
[... good response snipped ...]
Finally consider this. Why were the types included in the language if
they were of no use?

To keep gets() company?

If getc is get a char, gets should be get a short ;-)


But getc is get an int! So gets should be get a long.

Can't we all just get a long?
 
F

Flash Gordon

Richard said:
Flash said:
Eric said:
On 12/27/2009 7:53 AM, Flash Gordon wrote:
fasf wrote:
Hi,
I'm reading about int promotion: char and short shoud be avoided
[... good response snipped ...]
Finally consider this. Why were the types included in the language if
they were of no use?

To keep gets() company?

If getc is get a char, gets should be get a short ;-)

But getc is get an int! So gets should be get a long.

No, it gets a char (interpreted as an unsigned char), it just returns it
in an int... so gets should get a short interpreted as an unsigned short :)
 
F

Frodo Baggins

Also, if you declare a pair of low ranking type objects
and if you can see how far apart their addresses are,
it is not unusual for them to be sizeof(int) bytes apart,
meaning that the bytes saved in between them, are useless.

On my 64 bit system,
[frodo@fedora c]$ cat shorts.c
#include <stdio.h>

int main(void)
{
char a, b;
printf("\n%p, %p\n", &a, &b);
return 0;
}
[frodo@fedora c]$ ./a.out

0x7fff529dd89f, 0x7fff529dd89e
[frodo@fedora c]$ uname -a
Linux fedora 2.6.31.9-174.fc12.x86_64 #1 SMP Mon Dec 21 05:33:33 UTC
2009 x86_64 x86_64 x86_64 GNU/Linux

Regards,
Frodo B
 
N

Nobody

I'm reading about int promotion: char and short shoud be avoided because
are changed in int variables during arithmetic operations...So i shoud
always avoid char and short variables? These variables are smaller in byte
size, but new instructions (like sign extending) are implemented at
compiler time: these instructions occupy memory bytes so (apparently)
there is no advantage to use short data type....or not?

Using individual char/short variables doesn't save much memory, and may
not even save any; the compiler may just use the unused bytes as padding
so that following variables are aligned.

OTOH, the compiler may have to add extra instructions to separate the
data from the padding (e.g. the ARM can read an 8-bit byte or 32-bit word;
to read a 16-bit short, the compiler must read a 32-bit word then
explicitly mask out the unused bits).

Unless you're programming a microcontroller with miniscule amounts of RAM,
the memory savings are unlikely to be worth it for individual variables.
OTOH, it may be worthwhile for a large array of char/short, or a large
array of structures with char/short fields.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top