C ++文字常量存储在内存中的哪里? [英] Where the C++ literal-constant storage in memory?

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

问题描述

内存中C ++文字常量存储在哪里?堆还是堆?

Where the C++ literal-constant storage in memory? stack or heap?

int *p = &2

是错误的.我想知道为什么吗?谢谢

int *p = &2

is wrong. I want know why? Thanks

--------------------------------------------------- -

-------------------------------------------------

我的问题是内存中的C ++常量常量存储在哪里","int *p = &2

是错误的",不是我的问题.

My question is "Where the C++ literal-constant storage in memory", "int *p = &2

is wrong",not my question.

推荐答案

详细信息取决于机器,但是假设机器和操作系统是最普通的……每个可执行文件都包含多个段"-CODE,BSS, DATA和其他一些.

The details depend on the machine, but assuming a commonest sort of machine and operating system... every executable file contains several "segments" - CODE, BSS, DATA and some others.

CODE包含所有可执行的操作码.实际上,它通常被命名为TEXT,是因为它在几十年前就已经对人们有意义.通常它是只读的.

CODE holds all the executable opcodes. Actually, it's often named TEXT because somehow that made sense to people way back decades ago. Normally it's read-only.

BSS是未初始化的数据-实际上它不需要存在于可执行文件中,而是在程序开始运行时由操作系统的加载程序分配的.

BSS is uninitialized data - it actually doesn't need to exist in the executable file, but is allocated by the operating system's loader when the program is starting to run.

DATA保存文字常量-int8,int16,int32等以及浮点数,字符串文字以及编译器和链接器关心产生的任何奇怪的东西.这就是您要问的问题.但是,它仅包含定义为用作变量的常量,如

DATA holds the literal constants - the int8, int16, int32 etc along with floats, string literals, and whatever weird things the compiler and linker care to produce. This is what you're asking about. However, it holds only constants defined for use as variables, as in

const long x = 2;

,但是不太可能保存源代码中使用的文字常量,但并不与变量紧密相关.编译器直接处理一个单独的"2".例如在C中,

but unlikely to hold literal constants used in your source code but not tightly associated with a variable. Just a lone '2' is dealt with directly by the compiler. For example in C,

print("%d", 2);

将导致编译器生成对print()的子例程调用,编写操作码以将指针推向字符串文字%d"和值2,这两个指针在64位计算机上都是64位整数(您可以不是不是那些仍在使用32位硬件的落后者吗?:)跟着操作码跳到位于("print"子例程的标识符)处的子例程.

would cause the compiler to build a subroutine call to print(), writing opcodes to push a pointer to the string literal "%d" and the value 2, both as 64-bit integers on a 64-bit machine (you're not one of those laggards still using 32-bit hardware, are you? :) followed by the opcode to jump to a subroutine at (identifier for 'print' subroutine).

%d"文字进入DATA. 2个没有;它内置在将整数填充到堆栈中的操作码中.实际上,这可能是立即加载寄存器RAX",后跟值2,然后是推送寄存器RAX",或者可能只有一个操作码就可以完成这项工作.因此,在最终的可执行文件中,将在CODE(也称为TEXT)段中找到2.

The "%d" literal goes into DATA. The 2 doesn't; it's built into the opcode that stuffs integers onto the stack. That might actually be a "load register RAX immediate" followed by the value 2, followed by a "push register RAX", or maybe a single opcode can do the job. So in the final executable file, the 2 will be found in the CODE (aka TEXT) segment.

通常无法创建指向该值或任何操作码的指针.就象C这样的高级语言(在谈论操作码和段时,C是高级")而言,这毫无意义.& 2"只能是一个错误.

It typically isn't possible to make a pointer to that value, or to any opcode. It just doesn't make sense in terms of what high level languages like C do (and C is "high level" when you're talking about opcodes and segments.) "&2" can only be an error.

现在,拥有指向操作码的指针并不是完全不可能的.每当您在C中定义一个函数,或在C ++中定义一个对象方法,构造函数或析构函数时,该函数的名称都可以被视为指向从该函数编译的机器代码的第一个操作码的指针.例如,不带括号的print()是指向函数的指针.也许如果您的示例代码在函数中并且您猜到了正确的偏移量,则可以使用指针算法指向操作码中嵌套的立即"值2,但这对于任何当代的CPU来说都不容易,当然不适合初学者.

Now, it's not entirely impossible to have a pointer to opcodes. Whenever you define a function in C, or an object method, constructor or destructor in C++, the name of the function can be thought of as a pointer to the first opcode of the machine code compiled from that function. For example, print() without the parentheses is a pointer to a function. Maybe if your example code were in a function and you guess the right offset, pointer arithmetic could be used to point to that "immediate" value 2 nestled among the opcodes, but this is not going to be easy for any contemporary CPU, and certainly isn't for beginners.

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

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