简体   繁体   English

当我在运行时在 c 中为全局变量赋值时,memory 会发生什么?

[英]what happens in the memory when i give a global variable a value at runtime in c?

in c Uninitialized static and global variables are on the uninitialized data segement.在 c 中未初始化的 static 和全局变量在未初始化的数据段上。

so in this code所以在这段代码中

int var; //on the uninitialized  data segement
int main(){
   var = 5; //does this initialized global variable stay on the uninitialized data segement?
return 0;}

var is on the uninitialized data segment. var 在未初始化的数据段上。 what happens if later at runtime i give it a value and define it?如果稍后在运行时我给它一个值并定义它会发生什么?

if the variable later travel somehow to the initliazed data segment then what is the use of bss?如果变量稍后以某种方式传播到初始化数据段,那么 bss 有什么用?

in c Uninitialized static and global variables are on the uninitialized data segement.在 c 中未初始化的 static 和全局变量在未初始化的数据段上。

That may be a correct description of one or more particular C implementations, but "segments" in general and an "uninitialized data segment" in particular are not C-language concepts.这可能是对一个或多个特定 C 实现的正确描述,但一般的“段”和特别是“未初始化的数据段”不是 C 语言概念。

The C semantics are pretty simple: programs behave as if storage is allocated for all file-scope and all static variables before the program begins execution, and remains allocated until the program terminates. C 语义非常简单:程序的行为就像在程序开始执行之前为所有文件范围和所有 static 变量分配了存储空间,并且在程序终止之前一直保持分配状态。 That is, the objects they identify have "static storage duration".也就是说,它们识别的对象具有“静态存储持续时间”。 The language does not specify where the storage for these (or any other) objects is, but there is no difference between how objects with static storage duration may be accessed during their lifetimes and how other objects may be accessed during their (shorter) lifetimes.该语言没有指定这些(或任何其他)对象的存储位置,但是在具有 static 存储持续时间的对象的生命周期内如何访问它们与在其(较短)生命周期内如何访问其他对象之间没有区别。

Whether objects are declared with explicit initializers does not factor in to how objects with static storage duration may be used.是否使用显式初始化程序声明对象不会影响如何使用具有 static 存储持续时间的对象。 It affects only their initial values.它只影响它们的初始值。

var is on the uninitialized data segment. var 在未初始化的数据段上。 what happens if later at runtime i give it a value and define it?如果稍后在运行时我给它一个值并定义它会发生什么?

Your confusion seems to arise from mixing multiple levels.您的困惑似乎来自混合多个级别。 An "uninitialized data segment", in the sense you seem to mean it, is a feature of an executable file format, such as ELF.就您的意思而言,“未初始化的数据段”是可执行文件格式(例如 ELF)的一个特征。 It is largely unrelated to the layout of objects in the program's memory at runtime.它在很大程度上与运行时程序的 memory 中的对象布局无关。 Also, "defining" an identifier is a source-code concept, not a runtime concept.此外,“定义”标识符是源代码概念,而不是运行时概念。 A program can read and write objects' values at run time, but there is nothing it can do to affect whether an identifier is defined.程序可以在运行时读取和写入对象的值,但它无法影响标识符是否已定义。

So the answer is that if you assign a value to an object with static storage duration than that object thereafter holds the assigned value until and unless a different one is assigned to it, regardless of whether it was declared with an explicit initializer.因此,答案是,如果您为 object 分配一个值,其存储持续时间为 static,那么 object 之后将保持分配的值,直到并且除非它被声明为另一个初始化器。

if the variable later travel somehow to the initliazed data segment then what is the use of bss?如果变量稍后以某种方式传播到初始化数据段,那么 bss 有什么用?

Again, BSS and other "segments" are aspects of file formats, not programs' runtime memory.同样,BSS 和其他“段”是文件格式的方面,而不是程序的运行时 memory。 Nothing special has to happen for non- const variables assigned to an ELF BSS segment to receive new values at runtime.对于分配给 ELF BSS 段的非const变量在运行时接收新值,没有什么特别需要发生的。 C language semantics provide no way to distinguish such objects or their runtime storage from other objects and their storage. C 语言语义无法将此类对象或其运行时存储与其他对象及其存储区分开来。 In particular, assigning a value to an object never causes that object's address to change.特别是,为 object 赋值永远不会导致该对象的地址发生变化。

The use of BSS and similar mechanisms is to reduce the on-disk size of executable files. BSS 和类似机制的使用是为了减少可执行文件的磁盘大小。 Assigning a variable to BSS establishes its initial value without recording that initial value explicitly in the file.将变量分配给 BSS 会建立其初始值,而无需在文件中明确记录该初始值。 Generally, a BSS segment will occupy zero space in the executable, so you might describe it as "virtual", but real storage is allocated for BSS contents when the program is loaded into memory for execution.一般来说,一个BSS段在可执行文件中会占用零空间,所以你可以将其描述为“虚拟”,但是当程序加载到memory执行时,会为BSS内容分配真正的存储空间。

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

相关问题 将空字符串赋给C标准函数会发生什么? - What happens when you give a null string to a C standard function? 当我给strftime无效的说明符时会发生什么? - What happens when I give strftime invalid specifier? 在 C 中,更改指针值后 memory 值会发生什么变化? - In C, what happens to memory values after a pointer's value is changed? 当我们将值重新分配给 char 指针时,内存会发生什么? - What happens with memory when we reassign value to char pointer? 如果我们为变量分配一个新值,旧值会发生什么? (在 C 中) - If we assign a new value to a variable, what happens to the old value ? (in C) 在C中,当我们将int转换为struct *时,内存中会发生什么? - In C, what happens in memory when we do cast int to struct*? 当你尝试释放()已经释放c中的内存时会发生什么? - What happens when you try to free() already freed memory in c? 如果C中没有足够的内存用于静态分配,会发生什么情况? - What happens when there is not enough memory for a static allocation in C? 如果我在用calloc分配的内存之外设置值,会发生什么? - What happens if I set a value outside of the memory allocated with calloc? 如果我在 scanf 中传递变量值而不是变量引用会发生什么? - What happens if I pass a variable value instead of a variable reference in scanf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM