Why should I use void * instead of explicit pointer type in sizeof()?

huangapple go评论71阅读模式
英文:

Why should I use void * instead of explicit pointer type in sizeof()?

问题

我阅读了这个在StackOverflow答案(https://stackoverflow.com/a/3331268/17342809)下的评论:

内存分配请求应尽可能与类型无关:在内存分配函数的结果上不要强制转换,也不要在sizeof下使用类型名称。在malloc请求(等等)中使用类型名称的奇怪习惯早就被列入了C编程的垃圾桶。类型名称属于声明。如果不是声明,尽量不允许使用类型名称。

我能理解为什么要避免强制转换malloc()的结果(通过阅读另一个问题的答案:https://stackoverflow.com/a/605858/17342809)。然而,对我来说,在内存分配中使用sizeof()中的类型名称(例如sizeof(int *)而不是sizeof(void *))应该可以使您的代码更容易阅读,因为读者可以立即看到您打算如何使用这段内存。有人能解释一下为什么在malloc(sizeof(type *))中使用显式指针类型是一个不好的习惯吗?

英文:

I read this comment under a StackOverflow answer (https://stackoverflow.com/a/3331268/17342809):

> Memory allocation requests should be made as type-independent as possible: no cast on the result of memory allocation function and no type names under the sizeof. The strange habit of using type names under sizeof in malloc requests (and such) has long ago earned its place in the garbage bin of C programming. Type names belong in declarations. If it is not a declaration, type names are not allowed (as much as possible).
>

I can understand why it is bad to cast the result of malloc() (by reading this answer to another question: https://stackoverflow.com/a/605858/17342809). However, it looks to me that using type names in sizeof() (e.g. sizeof(int *) instead of sizeof(void *)) in memory allocation should make your code easier to read, as the reader could immediately see what you intend to use this memory for.
Can someone explain me why it is a bad habit to use explicit pointer types in malloc(sizeof(type *))?

答案1

得分: 7

请注意以下翻译内容:

"Instead of calling sizeof with the type prefer to use the variable name. For example instead of:

int *foo = malloc(sizeof(int));

prefer the idiom:

int *foo = malloc(sizeof *foo);

This means you are not repeating (DRY) the type of the declaration which may change independently. If the type changes in the best case it doesn't matter or you allocate too much memory, worst case you allocate too little memory which can cause corruption due to a change that is possible far away (say, in a separate header file).

The other thing I find convenient is that it's a purely mechanical transformation (as in I don't have to think about it): sizeof followed by * and the name of the variable. This eliminates the class of errors where the wrong type is used.

It's usually shorter to write.

Parentheses are required when you pass a type of sizeof but not if you pass a variable. sizeof binds the way you expect so sizeof *foo * 42 would allocate space for 42 ints as expected."

英文:

Instead of calling sizeof with the type prefer to use the variable name. For example instead of:

int *foo = malloc(sizeof(int));

prefer the idiom:

int *foo = malloc(sizeof *foo);

This means you are not repeating (DRY) the type of the declaration which may change independently. If the type changes in best case it doesn't matter or you allocate too much memory, worst case you allocate too little memory which cause corrupting due to a change that is possible far away (say, in a separate header file).

The other thing I find convenient is that it's a purely mechanical transformation (as in I don't have to think about it): sizeof followed by * and the name of the variable. This eliminates the class of errors where the wrong type is used.

It's usually shorter to write.

Parenthesis are required when you pass a type of sizeof but not if you pass a variable. sizeof binds the way you expect so sizeof *foo * 42 would allocate space for 42 ints as expected.

huangapple
  • 本文由 发表于 2023年5月13日 15:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定