Fiddling Bits
Fiddling Bits

Reputation: 8861

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

When dynamically allocating chars, I've always done it like this:

char *pCh = malloc(NUM_CHARS * sizeof(char));

I've recently been told, however, that using sizeof(char) is redundant and unnecessary because, "by definition, the size of a char is one byte," so I should/could write the above line like this:

char *pCh = malloc(NUM_CHARS);

My understanding is the size of a char depends on the native character set that is being used on the target computer. For example, if the native character set is ASCII, a char is one byte (8 bits), and if the native character set is UNICODE a char will necessarily require more bytes (> 8 bits).

To provide maximum portability, wouldn't it be necessary to use sizeof(char), as malloc simply allocates 8-bit bytes? Am I misunderstanding malloc and sizeof(char)?

Upvotes: 10

Views: 1307

Answers (6)

unwind
unwind

Reputation: 399823

Yes, it is redundant since the language standard specifies that sizeof (char) is 1. This is because that is the unit in which things are measured, so of course the size of the unit itself must be 1.

Life becomes strange with units defined in terms of themselves, that simply doesn't make any sense. Many people seem to "want" to assume that "there are 8-bit bytes, and sizeof tells me how many such there are in a particular value". That is wrong, that's simply not how it works. It's true that there can be platforms with larger characters than 8 bits, that's why we have CHAR_BIT.

Typically you always "know" when you're allocating characters anyway, but if you really want to include sizeof, you should really consider making it use the pointer, instead:

char *pCh = malloc(NUM_CHARS * sizeof *pCh);

This "locks" the unit size of the thing being allocated the pointer that is used to store the result of the allocation. These two types should match, if you ever see code like this:

int *numbers = malloc(42 * sizeof (float));

that is a huge warning signal; by using the pointer from the left-hand side in the sizeof you make that type of error impossible which I consider a big win:

int *numbers = malloc(42 * sizeof *numbers);

Also, it's likely that if you change the name of the pointer, the malloc() won't compile which it would if you had the name of the (wrong) basic type in there. There is a slight risk that if you forget the asterisk (and write sizeof numbers instead of sizeof *numbers) you'll not get what you want. In practice (for me) this seems to never happen, since the asterisk is pretty well established as part of this pattern, to me.

Also, this usage relies on (and emphasizes) the fact that sizeof is not a function, since no ()s are needed around the pointer de-referencing expression. This is a nice bonus, since many people seem to want to deny this. :)

I find this pattern highly satisfying and recommend it to everyone.

Upvotes: 14

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

The C99 draft standard section 6.5.3.4 The sizeof operator paragraph 3 states:

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]

In the C11 draft standard it is paragraph 4 but the wording is the same. So NUM_CHARS * sizeof(char) should be equivalent to NUM_CHARS.

We can see from the definition of byte in 3.6 that it is a:

addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

and Note 2 says:

A byte is composed of a contiguous sequence of bits, the number of which is implementation defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit.

Upvotes: 5

Matteo Italia
Matteo Italia

Reputation: 126787

Allocation sizes are always measured in units of char, which has size 1 by definition. If you are on a 9-bit machine, malloc understands its argument as a number of 9-bit bytes.

Upvotes: 3

Devolus
Devolus

Reputation: 22084

sizeof(char) will always return 1 so it doesn't matter if you use it or nit, it will not change. You may be confusing this with UNICODE wide characters, which have two bytes, but they have a different type wchar_t so you should use sizeof in that case.

If you are working on a system where a byte is defined to have 16 bits, then sizeof(char) would still return 1 as this is what the underlying architecture would allocate. 1 Byte with 16 bits.

Upvotes: 3

Simon Richter
Simon Richter

Reputation: 29578

sizeof(char) is always 1, but not because char is always one byte (it needn't be), but rather because the sizeof operator returns the object/type size in units of char.

Upvotes: 2

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

The C specification states that sizeof(char) is 1, so as long as you are dealing with conforming implementations of C it is redundant.

The size unit used by mallocis the same. malloc(120) allocates space for 120 char.

A char must be at least 8 bits, but may be larger.

Upvotes: 4

Related Questions