简体   繁体   English

这是什么意思? (int&)a

[英]What does this mean? (int &)a

define a float variable a, convert a to float & and int &, what does this mean? 定义一个浮点变量a,将a转换为float&和int&,这是什么意思? After the converting , a is a reference of itself? 转换后,a是自己的参考? And why the two result is different? 为什么这两个结果不同?

#include <iostream>
using namespace std;
int
main(void)
{
    float a = 1.0;
    cout << (float &)a <<endl;
    cout << (int &)a << endl;

    return 0;
}


thinkpad ~ # ./a.out 
1
1065353216
cout << (float &)a <<endl;
cout << (int &)a << endl;

The first one treats the bits in a like it's a float. 第一个处理像是浮点数的位。 The second one treats the bits in a like it's an int. 第二个处理比特,就像它是一个int。 The bits for float 1.0 just happen to be the bits for integer 1065353216. float 1.0的位恰好是整数1065353216的位。

It's basically the equivalent of: 它基本上相当于:

float a = 1.0;
int* b = (int*) &a;
cout << a << endl;
cout << *b << endl;

(int &) a casts a to a reference to an integer. (int &) a(int &) a转换为对整数的引用。 In other words, an integer reference to a. 换句话说,对a的整数引用。 (Which, as I said, treats the contents of a as an integer.) (正如我所说的那样,将a的内容视为整数。)

Edit: I'm looking around now to see if this is valid. 编辑:我现在环顾四周,看看这是否有效。 I suspect that it's not. 我怀疑它不是。 It's depends on the type being less than or equal to the actual size. 这取决于类型是否小于或等于实际大小。

It means undefined behavior:-). 这意味着未定义的行为:-)。

Seriously, it is a form of type punning. 说真的,这是一种形式的双关语。 a is a float , but a is also a block of memory (typically four bytes) with bits in it. a是一个float ,但a也是一个内存块(通常为4个字节),其中包含位。 (float&)a means to treat that block of memory as if it were a float (in other words, what it actually is); (float&)a处理该内存块的方法,就好像它是一个float (换句话说,它实际上是什么); (int&)a means to treat it as an int . (int&)a将其视为int Formally, accessing an object (such as a ) through an lvalue expression with a type other than the actual type of the object is undefined behavior, unless the type is a character type. 正式地,通过具有除对象的实际类型之外的类型的左值表达式来访问对象(例如a )是未定义的行为,除非该类型是字符类型。 Practically, if the two types have the same size, I would expect the results to be a reinterpretation of the bit pattern. 实际上,如果两种类型具有相同的大小,我希望结果是对位模式的重新解释。

In the case of a float , the bit pattern contains bits for the sign, an exponent and a mantissa. float的情况下,位模式包含符号,指数和尾数的位。 Typically , the exponent will use some excess-n notation, and only 0.0 will have 0 as an exponent. 通常 ,指数将使用一些多余的n表示法,而只有0.0将表示0作为指数。 (Some representations, including the one used on PCs, will not store the high order bit of the mantissa, since in a normalized form in base 2, it must always be 1. In such cases, the stored mantissa for 1.0 will have all bits 0.) Also typically (and I don't know of any exceptions here), the exponent will be stored in the high order bits. (某些表示,包括在PC上使用的表示,不会存储尾数的高位,因为在基数2中的标准化形式中,它必须始终为1.在这种情况下, 1.0的存储尾数将具有所有位0.)通常(我不知道这里有任何例外),指数将存储在高位中。 The result is when you "type pun" a floating point value to a an integer of the same size, the value will be fairly large, regardless of the floating point value. 结果是当您将浮点值“打字”为相同大小的整数时,无论浮点值如何,该值都会相当大。

The values are different because interpreting a float as an int & (reference to int ) throws the doors wide open. 这些值是不同的,因为将float解释为int & (引用int )会使门敞开。 a is not an int , so pretty much anything could actually happen when you do that. a不是int ,所以当你这样做时,几乎任何事情都可能发生。 As it happens, looking at that float like it's an int gives you 1065353216 , but depending on the underlying machine architecture it could be 42 or an elephant in a pink tutu or even crash. 实际上,看着那个float就像它是一个int给你1065353216 ,但根据底层的机器结构,它可能是42或粉红色的芭蕾舞短裙或甚至崩溃的大象。

Note that this is not the same as casting to an int , which understands how to convert from float to int . 请注意,这与转换为int ,后者了解如何将float转换为int Casting to int & just looks at bits in memory without understanding what the original meaning is. 转换为int &只查看内存中的位而不了解原始含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM