关于sizeof(char) [英] about sizeof(char)

查看:115
本文介绍了关于sizeof(char)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论

实现是什么,sizeof(char)总是得到1?我问,因为我看到一些批评者反对


char * ptr = malloc(sizeof(char)* NUM);

赞成简单

char * ptr = malloc(NUM);


-

Quidquid latine dictum sit altum viditur

Is it a given that sizeof(char) always yields 1, no matter the
implementation? I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);

--
Quidquid latine dictum sit altum viditur

推荐答案



"JosédePaula" <乔*********** @ ig.com.br>在留言中写道

news:pa **************************** @ ig.com.br .. 。

"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
无论
实现如何,sizeof(char)总是产生1?我问,因为我看到一些批评者反对

char * ptr = malloc(sizeof(char)* NUM);
赞成简单地说
char * ptr = malloc(NUM) ;
Is it a given that sizeof(char) always yields 1, no matter the
implementation? I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);




它应该是


char * ptr = malloc(sizeof(ptr [0])* NUM);


Tom



It should be

char *ptr = malloc(sizeof(ptr[0]) * NUM);

Tom


"JosédePaula" <乔*********** @ ig.com.br>在留言中写道

news:pa **************************** @ ig.com.br .. 。
"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
无论
实现如何,sizeof(char)总是产生1?



是。

ISO / IEC 9899:1999(E)


6.5.3.4 sizeof运算符


3当应用于具有char类型,unsigned char,

或signed char(或其合格版本)的操作数时,结果

是1.当应用于具有数组类型的操作数时,结果

是数组中的总字节数。当应用于具有结构或联合类型的

操作数时,结果是这样一个对象中的总字节数,包括内部和尾部

填充。

我问,因为我看到一些批评者反对

char * ptr = malloc(sizeof(char)* NUM);
赞成只需
char * ptr = malloc(NUM);
Is it a given that sizeof(char) always yields 1, no matter the
implementation?

Yes.
ISO/IEC 9899:1999(E)

6.5.3.4 The sizeof operator

3 When applied to an operand that has type char, unsigned char,
or signed char, (or a qualified version thereof) the result
is 1. When applied to an operand that has array type, the result
is the total number of bytes in the array. When applied to an
operand that has structure or union type, the result is the total
number of bytes in such an object, including internal and trailing
padding.
I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);




是的,''sizeof(char)''简直就是不必要的混乱。


但我不喜欢其中任何一个。我推荐这种方式:


char * ptr = malloc(NUM * sizeof * ptr);


这使得语句仍然可用指针类型是后来改变了
。减少维护总是很好的事情(tm):-)


-Mike



Yes, imo the ''sizeof(char)'' is simply unnecessary clutter.

But I don''t like either one of those. I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)

-Mike


"JosédePaula" <乔*********** @ ig.com.br>在留言中写道

news:pa **************************** @ ig.com.br .. 。
"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
无论
实现如何,sizeof(char)总是产生1?


只要实施声称符合一致,是的。

我问,因为我看到一些批评者反对

char * ptr = malloc(sizeof(char)* NUM);
赞成简单
char * ptr = malloc(NUM);
Is it a given that sizeof(char) always yields 1, no matter the
implementation?
So long as the implementation claims conformance, yes.
I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);




char * ptr = malloc(NUM * sizeof * ptr);


这是一个风格问题。


sizeof(T)可能存在的问题多于sizeof * P,其中P

是指向T的指针。考虑指针类型

可能会改变的情况。类似地,原始malloc(NUM)也有同样的问题,

考虑是否可以将ptr更改为键入wchar_t *。


-

Peter



char *ptr = malloc(NUM * sizeof *ptr);

It''s a style issue.

sizeof(T) has potentially more problems than sizeof *P, where P
is a pointer to T. Consider the case where the pointer type
might change. Similarly, a raw malloc(NUM) has the same problem,
consider if you might ever change ptr to type wchar_t *.

--
Peter


这篇关于关于sizeof(char)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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