16

当使用 malloc 并进行类似的内存操作时,我可以依赖 sizeof( char ) 始终为 1 吗?

例如,我需要为 N 个类型的元素分配内存char。乘以sizeof( char )必要:

char* buffer = malloc( N * sizeof( char ) );

还是我可以依靠 sizeof( char ) 始终为 1 并跳过乘法

char* buffer = malloc( N );

我完全理解sizeof在编译期间进行评估,然后编译器甚至可能编译出乘法,因此性能损失将是最小的并且很可能为零。

我主要询问代码清晰度和可移植性。类型是否需要这种乘法char

4

8 回答 8

29

根据定义,sizeof(char) 始终等于 1。一个字节是 C 中字符的大小,无论一个字节中的位数是多少(在普通桌面 CPU 上为 8)。

一个字节不是 8 位的典型示例是PDP-10和其他具有 9/36 位字节的旧的类似小型计算机的体系结构。但是我相信不是 2^N 的字节变得非常罕见

另外,我认为这是更好的风格:

char* buf1;
double* buf2;

buf1 = malloc(sizeof(*buf1) * N);
buf2 = malloc(sizeof(*buf2) * N);

because it works whatever the pointer type is.

于 2009-06-18T09:48:26.427 回答
14

sizeof(char)无论您执行何种类型的内存操作,始终为 1。

但是,sizeof(TCHAR)可能会因您的编译器选项而异。

于 2009-06-18T09:48:17.893 回答
12

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.

于 2009-06-18T10:12:58.967 回答
7

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".

于 2009-06-19T08:40:02.640 回答
6

这不是必要的。请参见此处(例如)。

sizeof(char)由 C 标准定义为始终为1(字节)。请注意,因为sizeof返回多个字节,所以每个字节的位数是无关紧要的(实际上无论如何都是 8)。

于 2009-06-18T09:47:26.007 回答
3

Something else to keep in mind is that the compiler statically knows the value of sizeof (char) is 1 and it also knows that multiplying a number by a static 1 implies the multiplication doesn't need to be done; the compiler will optimize it out. Concerns of performance shouldn't enter in to consideration on these grounds.

于 2009-06-18T15:55:20.040 回答
3

From "New C standard. An economic and cultural commentary".

  1. Statistics: 2.0% of sizeof are taken from char and 1.5% - from unsigned char. Page 1033 in 1.2 version of book.
  2. page 1037.

The number of bits in the representation of a character type is irrelevant. By definition the number of bytes in byte a character type is one.

Coding Guidelines Developers sometimes associate a byte as always containing eight bits. On hosts where the character type is 16 bits, this can lead to the incorrect assumption that applying sizeof to a character type will return the value 2. These issues are discussed elsewhere.

于 2010-02-07T01:40:59.687 回答
-4

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.

于 2009-06-18T11:13:15.787 回答