I’ve always assumed:
- that a
charis represented by a byte, - that a byte can always be counted upon to have 8 bits,
- that
sizeof (char)is always1, - and that the maximum theoretical amount of memory I can allocate (counted in
chars) is the number of bytes of RAM (+ swap space).
But now that I’ve read the Wikipedia entry on the byte I’m not so sure anymore.
Which one(s) of my assumptions is wrong? Which one(s) is dangerous?
Yes,
charandbyteare pretty much the same. A byte is the smallest addressable amount of memory, and so is acharin C.charalways has size 1.From the spec, section 3.6 byte:
And section 3.7.1 character:
A
charhasCHAR_BITbits. It could be any number (well, 8 or greater according to the spec), but is definitely most often 8. There are real machines with 16- and 32-bitchartypes, though.CHAR_BITis defined inlimits.h.From the spec, section 5.2.4.2.1 Sizes of integer types
<limits.h>:sizeof(char) == 1. Always.From the spec, section 6.5.3.4 The
sizeofoperator, paragraph 3:You can allocate as much memory as your system will let you allocate – there’s nothing in the standard that defines how much that might be. You could imagine, for example, a computer with a cloud-storage backed memory allocation system – your allocatable memory might be practically infinite.
Here’s the complete spec section 7.20.3.3 The
mallocfunction:That’s the entirety of the specification, so there’s not really any limit you can rely on.