I was doing some C coding and after reading some C code I\'ve noticed that there are code snippets like
char *foo = (char *)malloc(sizeof(char) * someDynamicAmo
Writing sizeof(char)
is not "future-proofing" your code against possible changes in the standard. It's usually a sign of complete misunderstanding of what sizeof
means and the whole fundamental model of memory objects in C - what's referred to as Representation of Types in the language of the C standard. The only reason a sizeof
operator even exists or makes sense is because C specifies objects to have a "representation" in memory in terms of a smallest possible unit that is unsigned char
. If not for this, storage would be a lot more abstract and there would be no use of sizeof
and the related pointer arithmetic.
sizeof
is defined in units of char
, i.e. sizeof(T)==N
means type T
occupies N
char
s. In light of this, sizeof(char)
is completely silly; it's attempting to measure how many char
s a char
occupies.