sizeof指针 [英] sizeof pointers

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

问题描述

全部,


关于指针类型的大小我有一个简单的问题:


我相信sizeof(char * )可能不一定与

sizeof(int *)相同?但是同一类型的多个指针级别怎么样?

sizeof(char **)和sizeof(char *)一样吗?如果是的话,两种情况下

的内部表示是否相同?


背景:我正在编写一个简单的IDL编译器产生''C''

代码,我试图让阵列/指针编组在

体系结构中安全。任何关于这个主题的好的文献/参考文献(从C $ / $
的角度来看)都会受到赞赏。


谢谢,

詹姆斯

All,

I have a quick question regarding the size of pointer-types:

I believe that the sizeof(char *) may not necessarily be the same as
sizeof(int *) ? But how about multiple levels of pointers to the same type?
Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the
internal representation be the same in both cases?

background on this: I''m writing a simple IDL compiler that produces ''C''
code, and am trying to get array/pointer marshalling to be ''safe'' across
architectures. Any good literature/references on the subject (from a C
perspective) would be appreciated.

thanks,
James

推荐答案

James Brown写道:
James Brown wrote:

全部,


我对指针类型的大小有一个快速的问题:


我相信sizeof(char *)可能不一定与

sizeof(int *)?但是同一类型的多个指针级别怎么样?

sizeof(char **)和sizeof(char *)一样吗?如果是的话,两种情况下

的内部表示是否相同?
All,

I have a quick question regarding the size of pointer-types:

I believe that the sizeof(char *) may not necessarily be the same as
sizeof(int *) ? But how about multiple levels of pointers to the same type?
Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the
internal representation be the same in both cases?



他们可能是相同的,但这不能保证。即使对于系统大小和表示相同的系统,编译器的优化也可能导致代码无法按照您的需要运行。

They could be the same, but this is not guaranteed. Even for systems
where the size and representation are the same, compilers''
optimisations may cause your code to not function the way you want.


背景:我正在编写一个简单的IDL编译器,它产生''C''

代码,我正在尝试将数组/指针编组到对于
架构来说,安全。任何有关该主题的优秀文献/参考资料(从C $ /
的角度来看)将不胜感激。
background on this: I''m writing a simple IDL compiler that produces ''C''
code, and am trying to get array/pointer marshalling to be ''safe'' across
architectures. Any good literature/references on the subject (from a C
perspective) would be appreciated.



如果你可以更具体地了解你想要做什么,

最好使用一个简短的代码片段,有人可能是能够建议

a以避免这个问题。

If you could be more specific about what you''re trying to do,
preferably using a short code snippet, someone may be able to suggest
a way to avoid the issue.


" James Brown" < em *** @ viahomepage.comwrites:
"James Brown" <em***@viahomepage.comwrites:

我对指针类型的大小有一个快速的问题:


我相信sizeof(char *)可能不一定与

sizeof(int *)相同?但是同一类型的多个指针级别怎么样?

sizeof(char **)和sizeof(char *)一样吗?如果是的话,两种情况下

的内部表示是否相同?
I have a quick question regarding the size of pointer-types:

I believe that the sizeof(char *) may not necessarily be the same as
sizeof(int *) ? But how about multiple levels of pointers to the same type?
Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the
internal representation be the same in both cases?



由于历史原因,标准保证void *,char *,

signed char *和unsigned char *具有相同的表示和

对齐。如果我没记错的话,它还保证所有

指向结构和联合的指针具有相同的表示。所有投注都已关闭。

The standard guarantees, for historical reasons, that void*, char*,
signed char*, and unsigned char* have the same representation and
alignment. It also guarantees, if I recall correctly, that all
pointers to structs and unions have the same representation. Beyond
that, all bets are off.


背景:我正在编写一个简单的IDL编译器来生成''C ''

代码,我试图让阵列/指针编组在

架构中安全。任何有关该主题的优秀文献/参考资料(从C $ /
的角度来看)将不胜感激。
background on this: I''m writing a simple IDL compiler that produces ''C''
code, and am trying to get array/pointer marshalling to be ''safe'' across
architectures. Any good literature/references on the subject (from a C
perspective) would be appreciated.



任何指向对象类型的值都可以再次转换为void *和

,产生相同的值。你可以利用

那个。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< ; *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

Any value of any pointer-to-object type can be converted to void* and
back again, yielding the same value. You can probably make use of
that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




" James Brown" < em *** @ viahomepage.comwrote in

"James Brown" <em***@viahomepage.comwrote in

>

我对指针类型的大小有一个快速的问题:


我相信sizeof(char *)可能不一定与

sizeof(int *)相同?但是同一个

类型的多个指针级别怎么样? sizeof(char **)是否与sizeof(char *)相同?如果是,

内部表示在两种情况下都是相同的吗?


背景:我正在编写一个简单的IDL编译器产生''C''

代码,我试图让阵列/指针编组在

体系结构中安全。任何关于这个主题的好的文献/参考文献(从C $ / $
的角度来看)都会受到赞赏。
>
I have a quick question regarding the size of pointer-types:

I believe that the sizeof(char *) may not necessarily be the same as
sizeof(int *) ? But how about multiple levels of pointers to the same
type? Would sizeof(char **) be the same as sizeof(char *)? And if it is,
would the internal representation be the same in both cases?

background on this: I''m writing a simple IDL compiler that produces ''C''
code, and am trying to get array/pointer marshalling to be ''safe'' across
architectures. Any good literature/references on the subject (from a C
perspective) would be appreciated.



char **不是很相似to char *。


char *指向一个字符列表,一个char **指向一个指针列表。如果

硬件具有64位字节,并且bit b / b
twiddling支持8位字符,则char * s将需要额外的位来表示偏移量。 ints

在这样的系统上自然会是64位,所以int *是一个原始地址。


但是char **可能只是指向内部结构的指针 -

字符指针。虽然不是禁止,但令人惊讶的是,它不会与一个国际航空公司不同。

A char ** is not very similar to to a char *.

A char * points to a list of characters, a char ** to a list of pointers. If
the hardware has 64 bit bytes, and 8 bit chars are supported by bit
twiddling, then char *s will need extra bits to represent the offset. ints
will, naturally, be 64 bits on such a system, so an int * is a raw address.

However a char ** is probably just a pointer to an internal structure - the
character pointers. It would be surprising, though not forbidden, for it to
be different to an int **.


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

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