Q. is sizeof(char) always 1

grahams@rcp.co.uk grahams@rcp.co.uk
Fri Sep 25 12:33:00 GMT 1998


Hi,

The reason I'm asking this question is a lot of code
in egcs assumes that sizeof(char) is 1 when allocating
memory.  For example allocating room for N chars
a typicall call to xmalloc would be xmalloc(N) rather
than xmalloc(N * sizeof(char)).

Are bugs waiting to bite should a machine exist where
sizeof(char) != 1

On a related point there mostly benign typos in a lot of
the memory allocation calls to (alloca, xmalloc, etc) which
wrt the sizeof parameter having one to many '*'.
   

For example there are calls such as
 p = (int *)xmalloc(N * sizeof(int *))
which should be
 p = (int *)xmalloc(N * sizeof(int))

I will be submitting patches for the typos I've spotted
but I probably won't catch all of them, they are very easy
to miss!

Graham




More information about the Gcc-bugs mailing list