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
I consider it kind of an anti-pattern. It signals that the programmer didn't quite know what he/she was doing, which immediately casts the rest of the code in dubious light.
Granted, it's not (quoting Wikipedia) "ineffective", but I do find it "far from optimal". It doesn't cost anything at run-time, but it clutters the code with needless junk, all the while signalling that someone thought it necessary.
Also, please note that the expression doesn't parse as a function-call: sizeof
is not a function. You're not calling a function passing it the magical symbol char
. You're applying the built-in unary prefix operator sizeof
to an expression, and your expression is in this case a cast to the type char
, which in C is written as (char)
.
It's perfectly possible, and highly recommended whenever possible, to use sizeof
on other expressions, it will then yield the size of the expression's value:
char a;
printf("A char's size is %u\n", (unsigned int) sizeof a);
This will print 1
, always, on all conforming C implementations.
I also heavily agree with David Cournapeau and consider repeating the type name in a malloc()
-call to also be kind of an anti-pattern.
Instead of
char *str;
str = malloc(N * sizeof (char));
that many would write to allocate an N-character-capacity string buffer, I'd go with
char *str;
str = malloc(N * sizeof *str);
Or (for strings only) omit the sizeof
as per above, but this of course is more general and works just as well for any type of pointer.
sizeof(char)
is always 1 no matter what type of memory manipulation you do.
However, sizeof(TCHAR)
may vary depending upon your compiler options.
While its not necessary, I consider it good practice to leave in the sizeof( char ) because it makes the code more readable and avoids the use of a magic number. Also, if the code needs to be changed later so that instead of a char it's mallocing the size of something into a pointer for that object, it's easier to change the code than if you have just a "1".
It is not necessary. See here (for example).
sizeof(char)
is defined by the C standard to always be 1 (byte). Note that because sizeof
returns a number of bytes the number of bits per byte is irrelevant (and in practical terms is 8 anyway).
By definition, sizeof(char) is always equal to 1. One byte is the size of character in C, whatever the numbers of bits in a byte there is (8 on common desktop CPU).
The typical example where one byte is not 8 bits is the PDP-10 and other old, mini-computer-like architectures with 9/36 bits bytes. But bytes which are not 2^N are becoming extremely uncommon I believe
Also, I think this is better style:
char* buf1;
double* buf2;
buf1 = malloc(sizeof(*buf1) * N);
buf2 = malloc(sizeof(*buf2) * N);
because it works whatever the pointer type is.
Using the sizeof(char) makes your code more readable and portable.
On x86, we all know that a character is 1 byte. But explicitly writing it down helps make your intentions clearer, which is always a good thing.
Also, what if your code gets put on some other platform where a character isn't 1 byte. What if a character was only 4 bits instead?
Agreed, it's not necessary, but it doesn't slow your run time down and it will pay off in that rare case you need to port your code to a different architecture.