tags:

views:

136

answers:

5

What is the correct way to work out how many bytes an int is? and how do I write an int to a file descriptor?

Here is a mock code sample which might make clear what I am trying to achieve:

char *message = "test message";
int length = strlen(message);
int fd = open(file, O_CREAT|O_RDWR);
write(fd, length??, ??); // <--- what goes here
write(fd, message, length);

I dont care about platform independence and byte order, just that it can compile on as many platforms as possible.

+8  A: 

sizeof(length) goes in the field.

It is preferable over using sizeof(int) in case you ever change the type of length in the future.

sizeof expresses the, well, size of a data type in multiples of sizeof(char), which is always 1.

birryree
I've heard that sizeof returns the size of a datatype in bytes, and I've heard that sizeof returns the size of a datatype in units of sizeof(char). But char isn't defined to be 1 byte in the C standard. Which is correct?
Swiss
Using sizeof(variable) is better than using sizeof(type). That way, if you change the type of the variable, there's no danger of the call to sizeof falling out of sync.
Sancho
@Swiss: It's the other way around: 1 byte is defined to be the size of a char in C.
schot
@schot 1 byte is defined to be the minimum size of a char in C, but larger sizes are allowed as well.
Swiss
`sizeof` when applied to `unsigned char`, `char` is always 1. It's defined in the C standard. That 1 byte could be 8 bits, it could be 9 bits, it could be more depending on platform. http://c-faq.com/charstring/wchar.html expounds on this. I do not have a copy of the C89/C90 standard, but in C99, this is section 6.5.3.4.
birryree
@ schot, biiryree I see now. In C, 8 bits != 1 byte. 1 byte = number of bits in a char.
Swiss
The C99 definition of byte says it is the smallest addressable unit large enough to hold a char. sizeof returns a size in bytes and sizeof(char) returns 1.
JeremyP
@Swiss: that's pretty much it, although C "bytes" must be *at least* 8 bits wide. Whether a C "byte" maps directly to a native machine byte is an issue for the compiler implementor. Harbison 1 word can hold 5 characters with 1 bit unused, and all other types take up 1 or more words. Since most C programs rely on the ability to map any type onto an array of `char`, that means the implementation must use a 9- or 36-bit "byte" which gets mapped onto the native byte, with a performance penalty.
John Bode
+2  A: 

sizeof is your friend.

write(fd, &length, sizeof(int));
shodanex
A: 

An int should always be 32 bits, doesn't matter if it is signed or unsigned.

However you can use a sizeof(int) to find out.

Rook
Wrong, long becomes 64 bit, int stays 32 bit.
Quandary
That's common for almost all ABIs, but is absolutely not guaranteed by the standard.
wnoise
@wnoise: almost all = everybody except MS. Well, perhaps crApple...
Quandary
Per the C standard, an `int` is *at least* 16 bits; it doesn't have to be 32 bits (although its getting harder to find implementations using 16-bit `int` types).
John Bode
A: 

sizeof(int) = 4 (on Linux, 32 & 64 Bit x86-Architecture)
sizeof(long) is 4 on 32 Bit, 8 on 64 Bit (on Linux, x86-32/64-Architecture)
Dunno about Windows.

Quandary
`sizeof(long)` stays 4 on 64-bit Windows (tested in XP x64, Vista x64, and Windows 7 x64).
birryree
@birryree: Why am I not surprised...
Quandary
@birryree: did you compile _for_ x64, or merely on x64, and ran on x64?
wnoise
@wnoise: I compiled targeting 64-bit. This is also documented at MSDN: http://msdn.microsoft.com/en-us/library/3b2e7499(v=VS.100).aspx
birryree
A: 
write(fd, &length, sizeof(length));

You can use sizeof with variable names or type names. In this case you could have done sizeof(int).

The write function takes the address of the memory zone you want to write, so you use the & (address of) operator. You don't have to do it for the string because you already have a pointer (char*).

src