哪里恒局部变量数组走在内存中的'C'程序 [英] Where does constant local variable array go in memory for a 'C' program

查看:132
本文介绍了哪里恒局部变量数组走在内存中的'C'程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC 4.8.1,似乎并不存储常量局部变量的数据段为主。下面是code和3这样的程序存储器映射:

I am using GCC 4.8.1 and it doesn't seem to store const variable local to main in DATA segment. Below is code and memory map for 3 such programs:

code 1:

int main(void) 
{ //char a[10]="HELLO"; //1 //const char a[10] = "HELLO"; //2 
return 0; 
} 

MEMORY MAP FOR ABOVE: 
text     data    bss     dec     hex    filename 
7264     1688 1040   9992    2708   a.exe 

code 2:

CODE 2:

int main(void)
{
    char a[10]="HELLO";
    //const char a[10] = "HELLO";
    return 0;
}

MEMORY MAP FOR 2: 
text     data    bss     dec     hex    filename 
7280     1688    1040    10008   2718 a.exe 

code 3:

CODE 3:

int main(void)
{
    //char a[10]="HELLO";
    const char a[10] = "HELLO";
    return 0;
}

MEMORY MAP FOR 3 :
 text    data    bss     dec     hex    filename 
7280     1688    1040    10008   2718   a.exe

我看不出3 codeS之间的数据段任何区别。有人可以解释这个结果给我。

I do not see any difference in data segment between 3 codes. Can someone please explain this result to me.

由于在期待!

推荐答案

这是会发生什么:

code 1:什么也不随地存储

Code 1: nothing is stored anywhere.

code 2: A 被存储在堆栈上。它不保存在。数据

Code 2: a is stored on the stack. It is not stored in .data.

code 3 A 是存储在堆栈或 .RODATA ,取决于它初始化为常量前pression与否。优化器也可能决定将其(与code一起)存储在的.text

Code 3 a is either stored on the stack or in .rodata, depending on whether it is initialized with a constant expression or not. The optimizer might also decide to store it in .text (together with the code).

我看不出3 codeS之间的数据段任何区别。

I do not see any difference in data segment between 3 codes.

这是因为,不应该有差别。 。数据用于非恒定与变量的静态存储持续时间的被初始化为零以外的值。

That's because there should be no difference. .data is used for non-constant variables with static storage duration that are initialized to a value other than zero.

这篇关于哪里恒局部变量数组走在内存中的'C'程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆