简体   繁体   English

指向浮点的短指针

[英]short pointer to a float

i run this code in c++: 我在c ++中运行此代码:

#include <iostream>
using namespace std;
int main()
{
    float f = 7.0;
    short s = *(short *)&f;
    cout << sizeof(float) << endl
         << sizeof(short) << endl
         << s << endl;
    return 0;
}

i get the following out pot: 我得到以下的锅:

4
2
0

but, in a lecture given in Stanford university, Professor Jerry Cain says he is sure the out pot well not be 0. 但是,在斯坦福大学的一次演讲中,杰里·凯恩教授表示,他确信出局不会是0。

the lecture is can be fond here . 讲座在这里可以很喜欢。 he says that around the 48 minute. 他说在48分钟左右。

is he wrong, or that some standard change since? 他错了,还是有些标准改变了? or is there a difference between platforms? 或者平台之间有区别吗?
I'm using g++ to compile my code. 我正在使用g ++来编译我的代码。

EDIT: in the next lecture he does mention "big endian" and "small endian" and says that they well affect the result. 编辑:在下一个讲座中,他确实提到了“大端”和“小端”,并说它们很好地影响了结果。

static void bitPrint(float f)
{
    assert(sizeof(int) == sizeof(float));
    int *data = reinterpret_cast<int*>(&f);
    for (int i = 0; i < sizeof(int) * 8; ++i)
    {
        int bit = (1 << i) & *data;
        if (bit) bit = 1;
        cout << bit;
    }
    cout << endl;
}

int main()
{
    float f = 7.0;
    bitPrint(f);
    return 0;
}

This program prints 00000000000000000000011100000010 此程序打印00000000000000000000011100000010

Since the sizeof(short) == 2 on your platform you get the first 2 bytes which are both zeros 由于平台上的sizeof(short) == 2 ,因此前两个字节都是零

Note that since size of types and possibly float implementation (not sure about this) are implementation defined different output can be seen on different platforms. 请注意,由于类型的大小和可能的浮动实现(对此不确定)是实现定义的,因此可以在不同平台上看到不同的输出。

Well, let's see. 好的,我们等着瞧。 First you write a float into the memory. 首先,你将浮点数写入内存。 It occupies 4 bytes, and it's value is 7. A float in the memory looks something like "sign bit -> exponent bits -> mantissa bits". 它占用4个字节,它的值为7.内存中的浮点看起来像“符号位 - >指数位 - >尾数位”。 I'm not sure how many bits are there for each part exactly, probably that depends on your platform. 我不确定每个部件到底有多少位,这可能取决于你的平台。

Since the float's value is 7, it only occupies some of the least-significant bits on the right (I assume big-endian). 由于float的值是7,它只占用右边的一些最低有效位(我假设是big-endian)。

Your short pointer points to the beginning of the float, which means to the most significant bit. 你的short指针指向浮动的开头,这意味着最重要的位。 Since the value is greater than 0, the sign bit is zero. 由于该值大于0,因此符号位为零。 Since the float value is far on the right, we can say that those two most significant bytes are filled with zeros. 由于浮点值远在右侧,我们可以说这两个最重要的字节用零填充。

Now, provided that a size of short is 2, which means we will only take two bytes out of float's 4 bytes, we get our 0 . 现在,假设short的大小为2,这意味着我们只从float的4个字节中取出两个字节,我们得到0

I believe though, that this result is rather UB and can differ on different platforms, compilers, etc. 我相信,这个结果相当UB,可以在不同的平台,编译器等上有所不同。

Accessing data through a pointer to a different type than it was stored as gives (except in a few special cases) undefined behavour. 通过指向与存储的不同类型的指针访问数据(除少数特殊情况除外)未定义的行为。

Firstly it's platform dependent how the data it stored so different systems may well give different values, and secondly the compiler might well generate code that doesn't even see the value you'd expect as it's allowed to do anything it likes when you do this (It's undefined behavour due to the strict aliases rules). 首先,它的平台依赖于它所存储的数据,因此不同的系统可能会提供不同的值,其次,编译器可能会生成甚至看不到您期望的值的代码,因为当您执行此操作时,它可以执行任何喜欢的操作(由于严格的别名规则,这是未定义的行为)。

Having said that there are probably reasons why the number you are seeing is valid, but you can't rely on it unless you specifically know your platform will do what you expect, it's not guarenteed by the standard. 话虽如此,可能有理由说明您所看到的数字是有效的,但除非您明确知道您的平台会按照您的预期行事,否则您不能依赖它,这并不是标准的保证。

He's "pretty" sure it's not zero, he says that explicitly. 他“非常”确定它不是零,他明确地说。

However, given that the representation of a short can be big-endian or little-endian, I wouldn't be so certain. 但是,鉴于短线的表示可以是大端或小端,我不会那么肯定。 In any case, this is a throwaway line at the end of a fifty-minute lecture so we can forgive him a little. 无论如何,这是一个五十分钟讲座结束时的一次性线,所以我们可以原谅他一点。 It may be he came back in the next lecture with a clarification. 他可能会在下一个讲座中回过头来澄清一下。

You would need to examine the underlying bits at (at least) a byte-by-byte level to understand what's going on. 您需要在(至少)逐字节级别检查基础位,以了解正在发生的事情。

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

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