Reputation: 135
I'm wondering if there is a difference between sizeof(char) and sizeof(char *) :
char *s;
s = malloc(sizeof(char*)*len + 1);
char *s;
s = malloc(sizeof(char)*len + 1);
Is this the same ?
Upvotes: 6
Views: 55251
Reputation: 310950
First of all it is a good example that shows that malloc
should be casted in C.
Let consider this code snippet
char *s;
s = malloc(sizeof(char*)*len + 1);
Reading it it is not clear what the author of the code was going to do. Did he want to allocate a character array of type char[sizeof( char * ) *len + 1]
or he wanted to allocate an array of type char *[len + 1]
or char *[len]
but made a mistake?
It is evident that such an expressions in the malloc malloc(sizeof(char*)*len + 1)
confuses readers and is error prone.
However if the author wrote for example
s = ( char ** )malloc(sizeof(char*)*len + 1);
then the compiler would report a diagnostic message. Or if he wrote
s = ( char * )malloc(sizeof(char*)*len + 1);
then it would be more clear aboiut the author intention.
Moreover between the pointer declaration and the statement where the memory is allocated there can be many screens of lines of the code and the worst the declaration can be in one file while the allocation statement in the other file.
char *s;
//....
//....
s = malloc(sizeof(char*)*len + 1);
Without casting the malloc
it is very time consuming to read such a code.
As for the question. sizeof( char )
is always equal to 1 and does not depend on used environment. While sizeof( char * )
is implementation-defined and can be equal for example to 2, 4 or 8 bytes or even something else.
Thus this expression in the malloc malloc(sizeof(char)*len + 1)
is fully equivalent to the expression in this call malloc( len + 1)
Again it is not clear seeing this statement
s = malloc(len + 1);
what type of objects is allocated dynamically. Whether there is allocated an array of characters or for example a two dimensional array of characters like char ( * )[2]
and len + 1
- is some even number and 1 is added to len
because len
is odd or even there is allocated an object of some other type.
So it is much better to write
s = ( char * )malloc(sizeof(char)*len + 1);
^^^^^^^^^
This code is self- documented and makes it easy to read.
Thus the difference between these two statements
s = malloc(sizeof(char)*len + 1);
s = malloc(sizeof(char*)*len + 1);
is that the first statement allocates a memory extent of len + 1
bytes while the second statement allocates an extent of sizeof( char * ) * len + 1
bytes where sizeof( char * )
is except very and very rare cases is greater than 1.
Upvotes: -1
Reputation: 75062
char
is a character and sizeof(char)
is defined to be 1. (N1570 6.5.3.4 The sizeof and _Alignof operators, paragraph 4)
char*
is a pointer to a character and sizeof(char*)
depends on the environment. It is typically 4 in 32-bit environment and 8 in 64-bit environment.
In typical environment where sizeof(char*) > sizeof(char)
, malloc(sizeof(char*)*len + 1)
will (at least try to) allocate more memory than malloc(sizeof(char)*len + 1)
if len
is small enough not to cause integer overflow.
Upvotes: 24
Reputation: 5548
They are not the same. char*
is a pointer to a char
. You are misinterpreting char*
as a "type" by itself.
char
is size 1, and char*
which is a pointer can be 4 on 32 bit systems and 8 on 64bit system (provided it is compiled as per the system).
Upvotes: 0