When given the choice to either grow the text segment or the data segment, what set of criteria can determine the choice?
当选择增长文本段或数据段时,有哪些标准可以决定选择?
I'm not aware of the differences and impact at either compile- or execution-time given different configurations (dynamically/statically linked, PIC, non-PIC, etc.). So what I'm asking is what the trade-offs involved are.
在给定不同配置(动态/静态链接,PIC,非PIC等)的情况下,我不知道编译或执行时的差异和影响。所以我要问的是所涉及的权衡是什么。
More concrete: It grows at compile-time through use of a static char array, basically making it either const or not, and constness apparently decides whether it ends up in text or not. Why is that?
更具体:它在编译时通过使用静态字符数组来增长,基本上使它成为常量或非常量,并且constness显然决定它是否以文本结尾。这是为什么?
1 个解决方案
#1
2
The code-segment is read-only, demand-loaded (it can be paged directly from disk) and can be shared.
代码段是只读的,需求加载的(可以直接从磁盘分页)并且可以共享。
In contrast, the data-segment is writable (backed by paging-file or RAM) and therefore not shared.
相反,数据段是可写的(由分页文件或RAM支持),因此不共享。
Thus, if you have the choice between growing either one, prefer a smaller data-segment, as the same amount of space in the data-segment in general consumes more resources, even if your program does not run multiple times simultaneously.
因此,如果您可以选择增长任何一个,则更喜欢较小的数据段,因为数据段中相同的空间量通常会消耗更多资源,即使您的程序不能同时运行多次。
There's an additional benefit to marking everything you can const
:
A failure to respect the fact that the data should not change is more likely to be caught by the compiler, and if not later by the runtime (memory-protection), instead of silently corrupting your state.
标记您可以构建的所有内容还有一个额外的好处:无法尊重数据不应更改的事实更有可能被编译器捕获,如果不是后来运行时(内存保护),而不是静默破坏您的国家。
#1
2
The code-segment is read-only, demand-loaded (it can be paged directly from disk) and can be shared.
代码段是只读的,需求加载的(可以直接从磁盘分页)并且可以共享。
In contrast, the data-segment is writable (backed by paging-file or RAM) and therefore not shared.
相反,数据段是可写的(由分页文件或RAM支持),因此不共享。
Thus, if you have the choice between growing either one, prefer a smaller data-segment, as the same amount of space in the data-segment in general consumes more resources, even if your program does not run multiple times simultaneously.
因此,如果您可以选择增长任何一个,则更喜欢较小的数据段,因为数据段中相同的空间量通常会消耗更多资源,即使您的程序不能同时运行多次。
There's an additional benefit to marking everything you can const
:
A failure to respect the fact that the data should not change is more likely to be caught by the compiler, and if not later by the runtime (memory-protection), instead of silently corrupting your state.
标记您可以构建的所有内容还有一个额外的好处:无法尊重数据不应更改的事实更有可能被编译器捕获,如果不是后来运行时(内存保护),而不是静默破坏您的国家。