java数据段 静态区,静态变量在哪里存储(数据段或堆或BSS)?

I obtained conflicting Opinions about static variable storage.

Opinion 1 : "A stack static variable stores its value in the heap"

Opinion 2 : "A stack static variable stores its value in the data segment".

I am confused with these conflicting answers.

Where exactly are static variables stored?

I am expecting an answers with references (text books, authentic tutorials, etc.).

Static variables have two types:

static variables declared inside a function.

global (declared outside function) static variable.

I would also like to know if there is any difference in the storage of these two types of variables?

解决方案

The 'stack variables' are usually stored on 'the stack', which is separate from the text, data, bss and heap sections of your program.

The second half of your question is about 'static' variables, which are different from stack variables - indeed, static variables do not live on the stack at all. Classically, static variables would all be in the data or bss sections of your program. With modern compilers, if the data is const-qualified, then the data may be stored in the text section of your program, which has a variety of benefits (including enforced non-modifiability).

The C standard does not dictate that there is a stack, nor a bss section. It just requires storage space to be available for variables with appropriate durations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值