为什么/何时使用 `intptr_t` 在 C 中进行类型转换? [英] Why / when to use `intptr_t` for type-casting in C?

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

问题描述

我有一个关于使用 intptr_tlong int 的问题.我观察到递增内存地址(例如,通过手动指针算术)因数据类型而异.例如,增加一个 char 指针会将内存地址加 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
", ( int )pChar );
printf( "pFloat: %d
", ( int )pFloat );

pChar++;
pFloat++;

printf( "and then after incrementing,:

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

它编译和执行得很好,但 XCode 给了我关于类型转换的警告:从指针转换为不同大小的整数."

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

经过一些谷歌搜索和binging(后者是一个词吗?),我看到有人推荐使用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
", ( intptr_t )pChar );
printf( "pFloat: %ld
", ( intptr_t )pFloat );

这确实解决了错误.所以,我想,从现在开始,我应该使用 intptr_t 来对指针进行类型转换……但是经过一番折腾,我发现只要替换 int with long 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
", ( long int )pChar );
printf( "pFloat: %ld
", ( long int )pFloat );

所以我的问题是,为什么 intptr_t 有用,什么时候应该使用?在这种情况下,这似乎是多余的.显然,myCharmyFloat 的内存地址太大了,无法放入 int ......所以将它们类型转换为 longints 解决了这个问题.

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.

是否有时内存地址对于 long int 来说也太大了?现在我考虑了一下,我想如果你有 > 4GB 的 RAM,这是可能的,在这种情况下,内存地址可能超过 2^32 - 1(无符号长整数的最大值......)但是 C 早在此之前就被创建了可以想象,对吧?还是他们有先见之明?

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 是合适的大小,但在其他平台上,long是正确的尺寸.你怎么知道你应该使用哪一个?你没有.一个可能是对的,但标准不保证它会是哪一个(如果是的话).因此该标准提供了一种定义为正确大小的类型,无论您在哪个平台上.之前你必须写的地方:

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>

它涵盖了更多的情况.想象一下,将上面的代码片段专门用于运行代码的每个平台.

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天全站免登陆