指针取消引用而不是sizeof? [英] Pointer dereference rather than sizeof?

查看:82
本文介绍了指针取消引用而不是sizeof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了一篇文章,其中包含许多关于普通C

做法的抱怨。其中一个人抱怨使用sizeof

运算符作为malloc调用的参数。所谓的正确关于调用malloc的
方法是取消引用指针;显然甚至

如果指针未定义。


例如,

ptr = malloc(sizeof(struct some_struct)); =错误


ptr = malloc(* ptr); =对吧


这个方法适用于我的平台,但并不是真的和我坐在一起。

代码可移植吗?标准?我一直在寻找自己,但是到目前为止还没有来过任何禁止它的东西。

I recently read an article containing numerous gripes about common C
practices. One of them contained a gripe about the use of the sizeof
operator as an argument to malloc calls. The supposed "right" way to go
about calling malloc instead is to dereference a pointer; apparently even
if said pointer is undefined.

E.g.
ptr = malloc(sizeof(struct some_struct)); = wrong

ptr = malloc(*ptr); = right

The method works on my platform, but doesn''t really sit right with me. Is
the code portable? Standard? I''ve been looking myself, but haven''t come
across anything forbidding it as of yet.

推荐答案

Micheal Smith写道:
Micheal Smith wrote:

我最近读过一篇文章,其中包含许多关于普通C

做法的抱怨。其中一个人抱怨使用sizeof

运算符作为malloc调用的参数。所谓的正确关于调用malloc的
方法是取消引用指针;显然甚至

如果指针未定义。


例如,

ptr = malloc(sizeof(struct some_struct)); =错误


ptr = malloc(* ptr); =对
I recently read an article containing numerous gripes about common C
practices. One of them contained a gripe about the use of the sizeof
operator as an argument to malloc calls. The supposed "right" way to go
about calling malloc instead is to dereference a pointer; apparently even
if said pointer is undefined.

E.g.
ptr = malloc(sizeof(struct some_struct)); = wrong

ptr = malloc(*ptr); = right



我肯定你误会了。


如果ptr被宣布为

struct some-struct * ptr;

第一种方法很差,但第二种方法是可憎的。

它应该是

ptr = malloc(sizeof * ptr);


这不是运算符''sizeof''的问题,而是运算符的b / b
的参数。请注意,sizeof * ptr是一个编译时常量,并且是

,与ptr初始化为

正确形式的某个实际存储无关。在您假设的正确形式中,您正在追求严重错误。

I''m sure you misunderstood.

If ptr is declared as
struct some-struct *ptr;
the first method is poor, but the second is an abomination.
It should be
ptr = malloc(sizeof *ptr);

It is not the operator ''sizeof'' that is the problem, but the argument to
the operator. Note that sizeof *ptr is a compile-time constant and is
independent of ptr being initialized to some actual storage in the
correct form. In your suppose ''right'' form, you are courting severe errors.


On Sun,2008年8月24日23:50:56 -0400,Martin Ambuhl写道:
On Sun, 24 Aug 2008 23:50:56 -0400, Martin Ambuhl wrote:

Micheal Smith写道:
Micheal Smith wrote:

>我最近阅读了一篇文章,其中包含许多抱怨常见的C
做法。其中一个人抱怨使用sizeof
运算符作为malloc调用的参数。所谓的正确改为调用malloc的方法是取消引用指针;显然
即使指针未定义。

例如ptr = malloc(sizeof(struct some_struct)); =错误

ptr = malloc(* ptr); =右
>I recently read an article containing numerous gripes about common C
practices. One of them contained a gripe about the use of the sizeof
operator as an argument to malloc calls. The supposed "right" way to
go about calling malloc instead is to dereference a pointer; apparently
even if said pointer is undefined.

E.g.
ptr = malloc(sizeof(struct some_struct)); = wrong

ptr = malloc(*ptr); = right



我肯定你误会了。


如果ptr被宣布为

struct some-struct * ptr;

第一种方法很差,但第二种方法是可憎的。它应该是

ptr = malloc(sizeof * ptr);


这不是运算符''sizeof''的问题,而是参数来自运营商的
。请注意,sizeof * ptr是一个编译时常量,并且是

,与ptr初始化为

正确形式的某个实际存储无关。在您的假设正确的形式中,您正在追求严重的

错误。


I''m sure you misunderstood.

If ptr is declared as
struct some-struct *ptr;
the first method is poor, but the second is an abomination. It should be
ptr = malloc(sizeof *ptr);

It is not the operator ''sizeof'' that is the problem, but the argument to
the operator. Note that sizeof *ptr is a compile-time constant and is
independent of ptr being initialized to some actual storage in the
correct form. In your suppose ''right'' form, you are courting severe
errors.



啊,我的例子中有一个漏洞。谢谢你的纠正。它绝对是


ptr = malloc(sizeof * ptr);


如果有一些误解,我会'我也没有劝说他们是正确的。实际上,根据我自己的估计,我更喜欢错误。例如。

Ah, there was a slip in my example. Thanks for the correction. It
definitely is
ptr = malloc(sizeof *ptr);

In case there''s some misunderstanding I''m not touting either to be
right. Actually in my own estimation I''d prefer the "wrong" example.


8月24日晚上8:59,Micheal Smith< xul ... @ cheapbsd.netwrote:
On Aug 24, 8:59 pm, Micheal Smith <xul...@cheapbsd.netwrote:

[询问`ptr = malloc(sizeof(struct some_struct))''和`ptr = malloc(sizeof(* ptr))'''之间的差异
[asking about difference between `ptr = malloc(sizeof(struct some_struct))'' and `ptr = malloc(sizeof(*ptr))'']



第一种形式的缺点是由程序员

来确保调用malloc()时给出的类型与

一致输入ptr的声明。如果我无意中写了


struct other_struct * ptr;


ptr = malloc(sizeof(struct some_struct));


这个代码几乎肯定不是我想要的,但是编译器将会b / b
不告诉我什么是错的。另一方面,如果我使用第二个

表格,则不存在不匹配的可能性,如果我稍后从''struct some_struct'更改

类型为了'struct third_struct'',我只需要

来改变它。

The disadvantage of the first form is that it is up to the programmer
to ensure that the type given in the call to malloc() agrees with the
type in the declaration of ptr. If I inadvertently write

struct other_struct *ptr;

ptr = malloc(sizeof(struct some_struct));

this code is almost certainly not what I want, but the compiler will
not tell me anything is wrong. On the other hand, if I use the second
form, there is no possibility of a mismatch, and if I later change the
type from ''struct some_struct'' to `struct third_struct'', I only have
to change it in one place.


这篇关于指针取消引用而不是sizeof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆