1 分钟阅读

最近遇到了类型转换的问题,记录一下

1. 问题:如何将 float 转成 void*

2. 分析

float 4字节

指针 根据操作系统确定大小

  • 64位 8字节
  • 32位 4字节

结论:可以进行转换,不会丢失精度。

如果丢失精度应怎样处理?

顺便回顾一下float类型的内存表示

ruanyifeng 解释的较详细

https://www.ruanyifeng.com/blog/2010/06/ieee_floating-point_representation.html

特殊值

https://blog.csdn.net/eickandy/article/details/48376435

开源中国的在线转换器

https://tool.oschina.net/hexconvert

3. 强制转换

  1. yym

line 5 说明

1
2
3
 * (&p) 取出 void * 指针的地址
 * (float *)(&p) 将 void *的地址 强制转换 为float * 类型
 * *(float *)(&p) 用* 来从地址中取值

https://www.runoob.com/cplusplus/cpp-pointers.html

1
2
3
4
5
6
7
8
9
10
11
12
    #include <iostream>
    int main() {
      void *p = nullptr;
      float f = 0.01;
      *(float *)(&p) = f;
      std::cout << "p = " << p << std::endl;

      float f2 = *(float *)(&p);
      std::cout << "f2 = " << f2 << std::endl;
    }

  1. Memory copy
1
2
3
4
5
6
7
8
9
10
11
12
13
    #include <iostream>
    int main() {
      void *p = nullptr;
      float f = 0.01;
      *(float *)(&p) = f;
      std::memcpy(&p, &f, sizeof f);
      std::cout << "p = " << p << std::endl;

      float f2 = *(float *)(&p);
      std::cout << "f2 = " << f2 << std::endl;
    }

  • output
1
2
3
4
5
    zangchuanqideMacBook-Pro:~ zcq$ ./a.out
    p = 0x3c23d70a
    f2 = 0.01

回顾C++四种强制转换的方法

https://blog.csdn.net/ydar95/article/details/69822540

https://en.cppreference.com/w/cpp/language/reinterpret_cast

https://www.cnblogs.com/ider/archive/2011/08/05/cpp_cast_operator_part6.html

4. 其他栗子

1、指针类型强制转换:

1
2
3
4
5
6
7
    int m;
    int *pm = &m;
    char *cp = (char *)&m;

    // pm指向一个整型,cp指向整型数的第一个字节

2、结构体之间的强制转换

1
2
3
4
5
6
7
8
    struct str1 a;
    struct str2 b;

    a=(struct str1) b;             //this is wrong
    a=*((struct str1*)&b);         //this is correct


https://blog.csdn.net/mhjcumt/article/details/7355127

标签:

分类:

更新时间: