Thursday, 3 April 2014

Is Using 'sizeof(char)' When Dynamically Allocating A 'char' Redundant?

When boldly allocating chars, I've always finished it like this:char *pCh
= malloc(NUM_CHARS * sizeof(char));I've recently been told, however, that
controlling sizeof(char) is over-abundance and low because, "by
definition, a distance of a bake is one byte," so we should/could write a
above line like this:char *pCh = malloc(NUM_CHARS);My bargain is a
distance of a bake depends on a internal impression set that is being used
on a aim computer. For example, if a internal impression set is ASCII, a
bake is one byte (8 bits), and if a internal impression set is UNICODE a
bake will indispensably need some-more bytes (> 8 bits).To yield border
portability, wouldn't it be required to use sizeof(char), as malloc simply
allocates 8-bit bytes? Am we disagreement malloc and sizeof(char)?

No comments:

Post a Comment