When using malloc and doing similar memory manipulation can I rely on sizeof( char ) being always 1?
For example I need to allocate memory for N elements of type
Something else to keep in mind is that the compiler statically knows the value of sizeof (char) is 1 and it also knows that multiplying a number by a static 1 implies the multiplication doesn't need to be done; the compiler will optimize it out. Concerns of performance shouldn't enter in to consideration on these grounds.
From "New C standard. An economic and cultural commentary".
char
and 1.5% - from unsigned char
. Page 1033 in 1.2 version of book.The number of bits in the representation of a character type is irrelevant. By definition the number of bytes in byte a character type is one.
Coding Guidelines Developers sometimes associate a byte as always containing eight bits. On hosts where the character type is 16 bits, this can lead to the incorrect assumption that applying sizeof to a character type will return the value 2. These issues are discussed elsewhere.