gpt4 book ai didi

c++ - 静态和全局变量存储说明

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:33 26 4
gpt4 key购买 nike

当我回顾 C/C++ 中的内存组织和存储时,我发现了这个:

“初始化数据段,通常简称为数据段。数据段是程序虚拟地址空间的一部分,其中包含由程序员初始化的全局变量和静态变量。

请注意,数据段不是只读的,因为变量的值可以在运行时更改。"

(在 http://www.geeksforgeeks.org/memory-layout-of-c-program/ 中找到)

我的印象是静态和/或全局变量在整个应用程序中保持不变,我认为这就是它们存在的意义。它们真的可以在运行时更改吗?

最佳答案

Can they really be altered at run time?

是的。当然,除非您将它们声明为 const

I was under the impression that a static and/or global variable remained immutable throughout an application, I thought this was the point of their existence.

不,您是在描述常量。顾名思义,具有所谓的静态存储持续时间 的变量具有不同的生命周期。 [基本.stc.静态]:

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).

想想 cout,一个全局流对象,您可以通过向其中插入数据来修改它。

关于c++ - 静态和全局变量存储说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27189541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com