内部如何工作int const iVal = 5; (int&)iVal = 10; [英] How internally this works int const iVal = 5; (int&)iVal = 10;

查看:173
本文介绍了内部如何工作int const iVal = 5; (int&)iVal = 10;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在编译器级别上工作。

I wanted to know how the following works @ compiler level.

int const iVal = 5; 
(int&)iVal = 10;

有点m / c或编译器级别的答案会很大。

A bit of m/c or compiler level answer would be great full.

提前感谢。

推荐答案

这是未定义的行为

在第一行定义一个常量整数。因此,在你的程序中,编译器被允许用值5替换iVal。它可以从内存加载它,但可能不会,因为这将没有好处。

In the first line you define a constant integer. Henceforth, in your program, the compiler is permitted to just substitute iVal with the value 5. It may load it from memory instead, but probably won't, because that would bring no benefit.

第二行写入你的编译器告诉你包含数字5的内存位置。但是,这不能保证有任何效果,因为你已经告诉编译器该值不会改变。

The second line writes to the memory location that your compiler tells you contains the number 5. However, this is not guaranteed to have any effect, as you've already told the compiler that the value won't change.

例如,下面将定义一个 5 元素的数组,并打印一个未定义的值undefined)

For example, the following will define an array of 5 elements, and print an undefined value (or it can do anything it wants! it's undefined)

int const iVal = 5;
(int&)iVal = 10;
char arr[iVal];
cout << iVal;

生成的程序集可能类似于:

The generated assembly might look something like:

sub ESP, 9      ; allocate mem for arr and iVal. hardcoded 5+sizeof(int) bytes
                ; (iVal isn't _required_ to have space allocated to it)
mov $iVal, 10   ; the compiler might do this, assuming that you know what
                ; you're doing. But then again, it might not.
push $cout
push 5
call $operator_ltlt__ostream_int
add ESP, 9

这篇关于内部如何工作int const iVal = 5; (int&amp;)iVal = 10;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆