(7条消息) c、c++指针和整型的互相转换

c语言:

Noncompliant Code Example(不兼容的代码示例)

The size of a pointer can be greater than the size of an integer, such as in an implementation where pointers are 64 bits and unsigned integers are 32 bits. This code example is noncompliant on such implementations because the result of converting the 64-bit ptr cannot be represented in the 32-bit integer type:

void f(void) {

char *ptr;

/* ... */

unsigned int number = (unsigned int)ptr;

/* ... */

}

Compliant Solution(兼容的代码示例)

Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value (seeINT36-EX2). The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.

#include

void f(void) {

char *ptr;

/* ... */

uintptr_t number = (uintptr_t)ptr;

/* ... */

}

使用intptr_t和uintptr_t才是兼容的,就死一套代码同时兼容32位和64位的操作系统。

intptr_t和uintptr_t

这两个数据类型是ISO C99定义的,具体代码在linux平台的/usr/include/stdint.h头文件中。

该头文件中定义intptr_t和uintptr_t这两个数据类型的代码片段如下:

/* Types for `void *' pointers.  */
    #if __WORDSIZE == 64
    # ifndef __intptr_t_defined
    typedef long int        intptr_t;
    #  define __intptr_t_defined
    # endif
    typedef unsigned long int    uintptr_t;
    #else
    # ifndef __intptr_t_defined
    typedef int            intptr_t;
    #  define __intptr_t_defined
    # endif
    typedef unsigned int        uintptr_t;
    #endif

在64位的机器上,intptr_t和uintptr_t分别是long int、unsigned long int的别名;在32位的机器上,intptr_t和uintptr_t分别是int、unsigned int的别名。

那么为什么要用typedef定义新的别名呢?我想主要是为了提高程序的可移植性(在32位和64位的机器上)。很明显,上述代码会根据宿主机器的位数为intptr_t和uintptr_t适配相应的数据类型。

c++语言:

reinterpret_cast<type-id> (expression)

type-id 必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针先把一个指针转换成一个整数,再把该整数转换成原类型的指针,还可以得到原先的指针值)。

type-id是转换后的类型,expresion是转换前的。

static_cast 与 reinterpret_cast

reinterpret_cast是为了映射到一个完全不同类型的意思,这个关键词在我们需要把类型映射回原有类型时用到它。我们映射到的类型仅仅是为了故弄玄虚和其他目的,这是所有映射中最危险的。(这句话是C++编程思想中的原话)

static_cast和reinterpret_cast的区别主要在于多重继承,比如

1

2

3

4

5

6

7

8

9

10

11

class A {

    public:

    int m_a;

};

class B {

    public:

    int m_b;

};

class C : public A, public B {};

那么对于以下代码:

1

2

C c;

printf("%p, %p, %p", &c, reinterpret_cast<B*>(&c), static_cast <B*>(&c));

前两个的输出值是相同的,最后一个则会在原基础上偏移4个字节,这是因为static_cast计算了父子类指针转换的偏移量,并将之转换到正确的地址(c里面有m_a,m_b,转换为B*指针后指到m_b处),而reinterpret_cast却不会做这一层转换。

总结:reinterpret_cast用于指针和整型之间的转换是没有问题,如果用于子类和父类类型方面的转换,会有问题。如果需要在子类和父类指针之间的转换,要用static_cast。

(0)

相关推荐

  • 《C++ Primer》笔记 第4章 表达式

    C++的表达式要不然是右值(right-value or read-value),要不然就是左值(left-value or location-value). 当一个对象被用作右值的时候,用的是对象的 ...

  • (8条消息) C++ 智能指针 unique

    unique_ptr 是 C++ 11 提供的用于防止内存泄漏的智能指针中的一种实现,独享被管理对象指针所有权的智能指针.unique_ptr对象包装一个原始指针,并负责其生命周期.当该对象被销毁时, ...

  • (7条消息) C++中位运算的使用方法

    一:简介1 位逻辑运算符:& (位   "与")  and^  (位   "异或")|   (位    "或")   or~  (位 ...

  • (35条消息) 中国城域网路由情况介绍

    中国的城域网,大概有三张比较典型的,一个是中国移动的CMnet,一个是中国电信IP城域网,还有一个是中国网通IP城域网.作为接入最后的阵地,城域网的业务是最复杂的.含盖了IPTV,语音,Interne ...

  • (35条消息) 家用宽带网络与服务器使用的网络有什么不同?

    很多人都知道,服务器的网络跟家用网络有很多区别.其中有很多技术大牛,都是使用家里的宽带做很多别人使用公网服务器才能完成的服务. 但是对于普通人来讲,似乎都觉得没什么区别,本文就此简单做一下区分: 固定 ...

  • 怎么设置微信公众号添加关注后自动回复多条消息

    怎么设置微信公众号添加关注后自动回复多条消息

  • (40条消息) 5G网络(接入网+承载网+核心网)

    前一段时间自己一直在做某市的5G试点项目,对5G的无线接入网相关技术有了更深入的认识.因此,希望通过无线接入网为线索(行话叫锚点),帮大家梳理一下无线侧接入网+承载网+核心网的架构,这里以接入网为主, ...

  • (7条消息) 国家信息化体系六要素

    历史的温度:寻找历史背面的故事.热血和真性情作者:张玮出版社:中信出版集团股份有限公司好评:100% 销售量:0 ¥34.3 历史的温度2:细节里的故事.彷徨和信念作者:张玮出版社:中信出版集团股份有 ...

  • (7条消息) QStringLiteral

    QStringLieral是Qt5中新引入的一个用来从"字符串常量"创建QString对象的宏(字符串常量指在源码中由双引号包含的字符串).在这篇博客我讲解释它的的内部实现和工作原 ...

  • (40条消息) Fastly释Lucet原生WebAssembly编译程序和Runtime

    云端服务供货商Fastly释出Lucet开源项目,这是原生的WebAssembly编译程序和Runtime,目的是要将WebAssembly带出浏览器,在开发者的应用程序中,安全地执行不受信任的Web ...