为什么/时,使用类型强制转换`intptr_t`用C? [英] Why / when to use `intptr_t` for type-casting in C?

查看:123
本文介绍了为什么/时,使用类型强制转换`intptr_t`用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用使用intptr_t 长整型有关问题。我观察到递增的内存地址(例如通过手动指针运算),按数据类型不同。例如递增字符指针加1的内存地址,而递增一个int指针增加4个,8双,16的长双,等...

I have a question regarding using intptr_t vs. long int. I've observed that incrementing memory addresses (e.g. via manual pointer arithmetic) differs by data type. For instance incrementing a char pointer adds 1 to the memory address, whereas incrementing an int pointer adds 4, 8 for a double, 16 for a long double, etc...

起初我是这样的:

char myChar, *pChar;
float myFloat, *pFloat;

pChar = &myChar;
pFloat = &myFloat;

printf( "pChar:  %d\n", ( int )pChar );
printf( "pFloat: %d\n", ( int )pFloat );

pChar++;
pFloat++;

printf( "and then after incrementing,:\n\n" );
printf( "pChar:  %d\n", (int)pChar );
printf( "pFloat:    %d\n", (int)pFloat );

这编译和执行得很好,但X $ C $ç给了我为我的类型转换的警告:从指针不同大小的整铸

which compiled and executed just fine, but XCode gave me warnings for my typecasting: "Cast from pointer to integer of different size."

在一些谷歌上搜索和暴食(是后者的一个词了吗?),我看到一些人建议使用使用intptr_t

After some googling and binging (is the latter a word yet?), I saw some people recommend using intptr_t:

#include <stdint.h>

...

printf( "pChar:  %ld\n", ( intptr_t )pChar );
printf( "pFloat: %ld\n", ( intptr_t )pFloat );

其中确实解决了错误。所以,我认为,从现在开始,我应该使用使用intptr_t 的类型转换指针......但后来有些坐立不安后,我发现我可以只更换解决问题 INT 长整型

which indeed resolves the errors. So, I thought, from now on, I should use intptr_t for typecasting pointers... But then after some fidgeting, I found that I could solve the problem by just replacing int with long int:

printf( "pChar:  %ld\n", ( long int )pChar );
printf( "pFloat: %ld\n", ( long int )pFloat );

所以我的问题是,为什么使用intptr_t 有用的,什么时候应该使用它?似乎在此实例是多余的。显然,对于内存的地址 myChar myFloat 只是太大,不适合在 INT ...所以他们值映射为长整型取值解决了这个问题。

So my question is, why is intptr_t useful, and when should it used? It seems superfluous in this instance. Clearly, the memory addresses for myChar and myFloat were just too big to fit in an int... so typecasting them to long ints solved the problem.

是不是有时候内存地址是太大长整型呢?现在回想起来,我想这是可能的,如果你有> 4GB的内存,在这种情况下,内存地址可能会超过2 ^ 32 - 1(最大无符号整型长值...),但下长期之前创建的,这是可以想象,对不对?还是他们的prescient?

Is it that sometimes memory addresses are too big for long int as well? Now that I think about it, I guess that's possible if you have > 4GB of RAM, in which case memory addresses could exceed 2^32 - 1 (max value for unsigned long ints...) but C was created long before that was imaginable, right? Or were they that prescient?

谢谢!

推荐答案

下面的东西:在某些平台上, INT 是大小合适,但对他人,是正确的大小。你怎么知道哪一个是你应该使用一个?你不知道。其中一个可能是正确的,但标准不保证有关哪一个将是(如果是其一)。这样的标准提供了被定义​​为正确的尺寸,不管你什么平台上一个类型。在那里你收到这样写:

Here's the thing: on some platforms, int is the right size, but on others, long is the right size. How do you know which one is the one you should use? You don't. One might be right, but the standard makes no guarantees about which one it would be (if it is either). So the standard provides a type that is defined to be the correct size, regardless of what platform you're on. Where before you had to write:

#ifdef PLATFORM_A
  typedef long intptr;
#else
  typedef int intptr;
#endif

现在你只写:

#include <stdint.h>

和它涵盖了那么多的情况下。想象一下,专业上面的代码片段的每一个平台的您code上运行。

And it covers so many more cases. Imagine specializing the snippet above for every single platform your code runs on.

这篇关于为什么/时,使用类型强制转换`intptr_t`用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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