常数存储在哪里和如何? [英] Where and how are constants stored?

查看:176
本文介绍了常数存储在哪里和如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里阅读了这个问题,我也阅读了 c-faq ,但我不明白此背后的确切原因: -

I read this question from here and I also read related question from c-faq but I don't understand the exact reason behind this :-

#include <iostream>

    using namespace std;

    int main()
    {
        //const int *p1 = (int*) &(5);  //error C2101: '&' on constant
        //cout << *p1;

        const int five = 5;
        const int *p2 = &(five);
        cout << *p2 << endl;

        char *chPtr = (char*) &("abcde");
        for (int i=0; i<4; i++) cout << *(chPtr+i);
        cout << endl;
        return 0;
    }



我想知道如何存储整数或字符串常量。我对字符串文字的理解是,它们在程序启动时在全局静态内存中创建,并持续到程序退出。在abcde的情况下,即使我没有给它一个变量名称,我可以取它的地址( chPtr ),我认为我可以在程序终止之前任何时候取消引用 chPtr ,并且字符值仍然存在,即使我将它解除引用它声明的范围之外。是 const int 变量five也放在全局静态和地址 p2

I was wondering how constants, either integer or string literal, get stored. My understanding of string literals is that they are created in global static memory upon start of program and persist until program exit. In the case of "abcde" even though I did not give it a variable name I can take it's address (chPtr) and I assume I could probably dereference chPtr any time before program termination and the character values would still be there, even if I dereferenced it outside the scope where it was declared. Is the const int variable "five" also placed in global static and that address p2 can also be referenced any time?

为什么我可以使用five的地址但我不能要求:&(5)?常量5five存储方式是否不同? 5是否存储在内存中?

Why can I take the address of "five" but I cannot ask for: &(5) ? Are the constants "5" and "five" stored differently? and where "5" is get stored in memory ?

推荐答案

不能接受文字的地址(例如&(5)),因为文字不是存储在任何地方 - 它实际上是在汇编指令中写的。根据平台,你会得到不同的指令,但是一个MIPS64的附加示例如下:

You cannot take the address of a literal (e.g. &(5)) because the literal is not "stored" anywhere - it is actually written in the assembly instruction. Depending on the platform, you'll get different instructions, but a MIPS64 addition example would look like this:

DADDUI R1, R1, #5

尝试使用立即数的地址是无意义的,因为它不驻留在数据)存储器,但实际上是指令的一部分。

Trying to take the address of the immediate is meaningless as it doesn't reside in (data) memory, but is actually part of the instruction.

如果你声明一个 const int i = 5 ,并且不需要它的地址, (可能会)将其转换为文字,并在相应的汇编指令中放置 5 。一旦你尝试获取 i 的地址,编译器就会看到它不再这样做,并将它放在内存中。这不是这种情况,如果你只是试图取一个文字的地址,因为你没有告诉编译器它需要为一个变量分配空间(当你声明一个 const int i ,它在第一遍中分配空间,稍后将确定它不再需要它 - 它不反向运行)。

If you declare a const int i = 5, and do not need the address of it, the compiler can (and likely will) convert it to a literal and place 5 in the appropriate assembly instructions. Once you attempt to take the address of i, the compiler will see that it can no longer do that, and will place it in memory. This is not the case if you just attempt to take the address of a literal because you haven't indicated to the compiler that it needed to allocate space for a variable (when you declare a const int i, it allocates the space in the first pass, and will later determine it no longer needs it - it does not function in the reverse).

String常数存储在数据存储器的静态部分 - 这就是为什么你可以获取它们的地址。

String constants are stored in the static portion of the data memory - which is why you can take the address of them.

这篇关于常数存储在哪里和如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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