知道数据类型的大小 [英] Knowing size of a data type

查看:67
本文介绍了知道数据类型的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果不使用sizeof,有没有办法知道该操作系统上任何数据的大小

类型...? ??


我能想到的一种方法是取两个变量地址的差值

连续定义。

任意其他方式..... ????


谢谢,

nitin

解决方案

On Tue,02 Sep 2003 15:08:24 +0530,Nitin写道:



不使用sizeof,是否有一种了解该操作系统上任何数据类型大小的方法... ???

我能想到的一种方法是取两个变量的地址差异<连续定义。
任何其他方式..... ????




不要认为它们是连续的内存和

编译器无论如何都可以在它们之间使用填充。


问候

NPV


"尼丁&曲OT; < NI ***** @ bell-labs.com>写在

< bj ******** @ netnews.proxy.lucent.com>:



使用sizeof,有没有办法知道该操作系统上任何数据类型的大小... ???



我很好奇:我可以请问为什么你不想使用

''sizeof''?


-

空气是水其中有孔。


" Nitin" < NI ***** @ bell-labs.com>写道:



如果不使用sizeof,有没有办法知道该操作系统上任何数据类型的大小。 .. ???

我能想到的一种方法是连续定义两个变量地址的差异。




如果是连续的你的意思是保证连续,就像在数组中一样,

然后是。如果`T''是一个类型,'t''是'T''类型的变量,那么

表达式`(size_t)((char *)(& t + 1) ) - (char *)& t)''产生相同的值

为'sizeof(T)''。


Martin


Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????

thanks,
nitin

解决方案

On Tue, 02 Sep 2003 15:08:24 +0530, Nitin wrote:

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.
Any other way .....????



Don''t think they are guaranteed to be consecutive in memory, and the
compiler may use padding between them anyway.

regards
NPV


"Nitin" <ni*****@bell-labs.com> wrote in
<bj********@netnews.proxy.lucent.com>:

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???


I''m curious: may I kindly ask why you would like to stay away from using
''sizeof''?

--
Air is water with holes in it.


"Nitin" <ni*****@bell-labs.com> writes:

Hi,

Without using sizeof, is there a way to get to know the size of any data
type on that OS ... ???

one way I could think of is to take diff of the addresses of two variables
defined consecutively.



If by "consecutive" you mean "guaranteed to be consecutive", as in an array,
then yes. If `T'' is a type, and `t'' is a variable of type `T'', then the
expression `(size_t)((char *)(&t + 1) - (char *)&t)'' yields the same value
as `sizeof (T)''.

Martin


这篇关于知道数据类型的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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