Four or Two Bytes?

J

Joachim Schmitz

pete said:
Joachim said:
pete said:
Joachim Schmitz wrote:
pete wrote:
Joachim Schmitz wrote:
On top of that: here it is about 2 or 4, which even on the DS9K
would easily fit into INT_MAX.
Not on the DS9K.
unsigned short can have INT_MAX padding bytes on the DS9K. Chapter
Chapterand verse please
N869
6.2.6.2 Integer types

[#1] For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into
two groups: value bits and padding bits (there need not be
any of the latter). If there are N value bits, each bit
shall represent a different power of 2 between 1 and 2N-1,
so that objects of that type shall be capable of
representing values from 0 to 2N-1 using a pure binary
representation; this shall be known as the value
representation. The values of any padding bits are
unspecified.
N1256
5.2.4.2.1 Sizes of integer types <limits.h>

...

Their implementation-defined values shall be equal or greater in
magnitude (absolute value) to those shown, with the same sign.

...

- maximum value for an object of type int

INT_MAX +32767 // 2^15 - 1





So even on a DS9K printf's %d has to be able to display numbers up
to 32767.

printf's %d being able to display numbers up to 32767,
is irrelevant as to how many padding bytes
(or bytes composed of padding bits),
an object of type unsigned short is allowed to have.
INT_MAX is for int, not short, or unsignd short
(%d prints an int, not an unsigned short)

Ah, so you're saying that an int, in addition to it's minimum of 15 value
bits can have as many pading bits (whos's _values_ are unspecified) as it
likes, making the entire sizeof(int) larger than 32727 (as long as it still
fits a size_t)? And not that 'padding _bits_' implies that they are only
allowed to fill up to the next byte size? (the values are explicitly
unspecivied, what about the number?)

Bye, Jojo
 
K

Keith Thompson

Joachim Schmitz said:
pete wrote: [...]
printf's %d being able to display numbers up to 32767,
is irrelevant as to how many padding bytes
(or bytes composed of padding bits),
an object of type unsigned short is allowed to have.
INT_MAX is for int, not short, or unsignd short
(%d prints an int, not an unsigned short)

Right, but we're talking about printing the *size* of an unsigned
short, not its value. The line that led to this long digression was
my own (very slightly sloppy):

printf("Size of s: %d bytes\n", (int)sizeof s);

where s was an object of type unsigned short.
Ah, so you're saying that an int, in addition to it's minimum of 15 value
bits can have as many pading bits (whos's _values_ are unspecified) as it
likes, making the entire sizeof(int) larger than 32727 (as long as it still
fits a size_t)? And not that 'padding _bits_' implies that they are only
allowed to fill up to the next byte size? (the values are explicitly
unspecivied, what about the number?)

The standard says (C99 6.2.6.2p2):

For unsigned integer types other than unsigned char, the bits of
the object representation shall be divided into two groups: value
bits and padding bits (there need not be any of the latter).

For unsigned short, there must be at least 16 value bits (this follows
from the requirement that the range must be at least 0 .. 65535, and
the fact that mathematically you need at least 16 bits to represent
all the values in that range).

The standard places no upper limit on the number of value bits
or on the number of padding bits. sizeof(unsigned short) is
(VB + PB) / CHAR_BIT; VB + PB must be a whole multiple of CHAR_BIT.

This entire discussion is about as close to having no practical
meaning as I can imagine. An implementation that had as many padding
bits for unsigned short as we're talking about, or even as many value
bits, would be beyond exotic; it would be clinically insane.

In practice, I do prefer this:
printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);
to this:
printf("Size of s: %d bytes\n", (int)sizeof s);
but only because it's a good habit, not because I'm the least bit
concerned about the possibility that sizeof s could exceed INT_MAX.
Or the code might be changed so that s is of some larger type (likely
if the code purpose is just to print the sizes of several types).

Note that for ``(int)sizeof(unsigned short)'' to overflow, almost all
the bits of unsigned short would have to be padding bits. If it had a
huge number N of value bits, then int would have to have at least N-1
value bits (-1 for the sign bit), so it would have more than enough
range to represent the value of sizeof(unsigned short).
 
B

Barry Schwarz

snip


Sure. But since the specific question was why he got four bytes rather than
two in the target file I emphasized that 2 * 2 is 4, and alluded your point
by commenting "More or less..." and mentioning the crash.

But once the OP has invoked undefined behavior, there is no guarantee
that 2*2 will still equal 4. The only reason fwrite produced 4 bytes
is that this particular manifestation of undefined behavior produced 4
bytes. The fact that he inadvertently asked for 4 bytes is only
unfortunate coincidence. The next time it executes it could do
something completely different. If the OP were to recompile with
different options, who knows what the code could do.


Remove del for email
 

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

Similar Threads

Adding adressing of IPv6 to program 1
URGENT 1
error 28
Windows LLDP Driver Responds With No Data 0
Problem Reading Bmp's .. 14
Writing "ls" under windows 39
fread/fwrite 2 18
writing into file using structures 5

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top