const int的不占用空间? [英] const int does not take up space?

查看:115
本文介绍了const int的不占用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此评论<一个href=\"http://stackoverflow.com/questions/7147008/the-usage-of-anonymous-enum/7147037#7147037\">answer在匿名枚举,的奥利查尔斯沃思指出:

const int的是不可改变的,不得占用任何空间,这取决于
  什么编译器选择这样做。

const int is immutable, and may not take up any space, depending on what the compiler chooses to do.

如果我宣布 const int的I = 10 ,如何存储如果 10 它可能不会占用任何空间?

If I declare const int i = 10, how is that 10 stored if it "may not take up any space"?

假设一个 INT 4个字节,我会presume至少有4个字节被预留存放10作为 const int的

Assuming that an int is 4 bytes, I would presume that at least 4 bytes is reserved to store 10 as a const int.

推荐答案

编译器是免费优化code,因为它认为合适的,只要所产生的code提供相同的可观察到的副作用。

The compiler is free to optimise code as it sees fit, so long as the resulting code offers the same observable side effects.

所以变量可被优化以只存在于寄存器,或者与即时值替代。在伪机器code:

So variables may be optimised to only exist in registers, or replaced with immediate values. In pseudo-machine-code:

SET 10, eax
ST eax, &i    # Initialise i

...

LD &i, eax    # Add i to ebx
ADD eax, ebx, ebx

将变成:

SET 10, eax
ADD eax, ebx, ebx

甚至只是:

ADD 10, ebx, ebx

这篇关于const int的不占用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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