Is this Size Deduction correct

Started by
15 comments, last by Odiee 18 years, 4 months ago
Quote:Original post by Endar
Quote:Original post by JohnBSmall
Unless you're on a very unusual system, 'char' will be one byte....


What kind of unusual systems have 'char' as not a byte? Some of the older ones?


In managed languages as C# a char is 2 bytes. UNICODE forced down your throat.
Advertisement
Quote:Original post by SiCrane
None. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Quote:Original post by JohnBSmall
Quote:Original post by SiCrane
None. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B


Backwards compatibility with C.

Back in C days, people did a lot of bad stuff with regards to text handling (in particular, assuming it's ok to represent each character of a string in one byte, and then put a special byte at the end and set a pointer to the beginning) - 'char' and 'byte' became synonymous. In C++, sizeof(char) == 1, i.e. the char size is defined to be one byte, but a byte is *not* guaranteed to be 8 bits (it is, however, guaranteed to be *at least* that much, and also that memory consists of contiguous bytes with no holes in between) - to unambiguously refer to 8 bits in technical discussion, use 'octet'.
Quote:Original post by Zahlman
Quote:Original post by JohnBSmall
Quote:Original post by SiCrane
None. char is defined to be one byte.

Odd... I wonder why they didn't just call it 'byte' then.

John B

Backwards compatibility with C.

No, I meant why was 'char' chosen when it was actually a choice (presumably when C was designed, although maybe even in an earlier language).

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
It's not defined as a byte. I don't have a copy of the standard, but it's not.
Quote:Original post by Puzzler183
It's not defined as a byte. I don't have a copy of the standard, but it's not.


Quote:The 2003 C++ standard - 5.3.3 sizeof
The sizeof operator yields the number of bytes in the object representation of its operand. The operand
is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not
be applied to an expression that has function or incomplete type, or to an enumeration type before all its
enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates
a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1;


So sizeof gives the number of bytes. And sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1, which means they are 1 byte.
Original post by Dave
This:
	struct var	{		int a;		float pos[12];		float tex[8];		float n[3];		char bytes[2];		bool passable;	};

and then just
size_t size = sizeof(var);printf("%d",(int)size);

And than you have it, MAN! where do you live?

This topic is closed to new replies.

Advertisement