简体   繁体   English

static个常量存储在哪里?

[英]Where are static constants stored?

If a constant is defined globally, it goes to the text segment.如果一个常量是全局定义的,它会转到text段。 Local constants get pushed onto the stack .本地常量被压入stack Static variables get stored into either the data or the bss segment depending on if it is initialised in place and what it is initialised with. Static 变量存储到databss段中,具体取决于它是否就地初始化以及初始化的内容。

What about static const ? static const怎么样? This question links to another one which is only about static variables, not constants. 这个问题链接到另一个只有大约 static 个变量,而不是常量的问题。 I suppose static constants should be stored in the text segment as read-only variables, but I'm not sure.我想 static 常量应该作为只读变量存储在text段中,但我不确定。 Where is it commonly stored?一般存放在哪里?

A static const may be folded at compile time. static const可能会在编译时折叠。 If it is not, then it is stored in the data or bss segment as though it were a static (but other modules can't link to it).如果不是,则将其存储在databss段中,就好像它是static (但其他模块无法链接到它)。 Storing it in the text segment is valid, but very few compilers do so.将它存储在text段中是有效的,但很少有编译器这样做。 I've only seen it in embedded compilers where the RAM/ROM distinction matters.我只在 RAM/ROM 区分很重要的嵌入式编译器中看到过它。 Newer toolchains often have a rodata segment that takes static and global constants.较新的工具链通常有一个rodata段,它采用 static 和全局常量。

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

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