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 char
. Is multiplying by sizeof( char )
necessary:
char* buffer = malloc( N * sizeof( char ) );
or can I rely on sizeof( char ) always being 1 and just skip the multiplication
char* buffer = malloc( N );
I understand completely that sizeof
is evaluated during compilation and then the compiler might even compile out the multiplication and so the performance penalty will be minimal and most likely zero.
I'm asking mainly about code clarity and portability. Is this multiplication ever necessary for char
type?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…