这似乎是一个非常基本的问题,但它一直在我脑海中:
当我们分配一个局部变量时,它会进入堆栈.类似地,动态分配会导致变量进入堆.现在,我的问题是,这个变量实际上是在堆栈还是堆上,或者我们只是堆栈和堆中的引用.
例如,
假设我声明了一个变量int i.现在这i是在堆栈上分配的.那么,当我打印地址时i,这将是堆栈中的一个位置?堆的问题也一样.
c memory-management heap-memory stack-memory dynamic-allocation
以下是示例程序的objdump输出,
080483b4 <display>:
80483b4: 55 push %ebp
80483b5: 89 e5 mov %esp,%ebp
80483b7: 83 ec 18 sub $0x18,%esp
80483ba: 8b 45 0c mov 0xc(%ebp),%eax
80483bd: 89 44 24 04 mov %eax,0x4(%esp)
80483c1: 8d 45 fe lea 0xfffffffe(%ebp),%eax
80483c4: 89 04 24 mov %eax,(%esp)
80483c7: e8 ec fe ff ff call 80482b8 <strcpy@plt>
80483cc: 8b 45 08 mov 0x8(%ebp),%eax
80483cf: 89 44 24 04 mov %eax,0x4(%esp)
80483d3: c7 04 24 f0 84 04 08 movl $0x80484f0,(%esp)
80483da: e8 e9 fe ff …Run Code Online (Sandbox Code Playgroud) 我们说全局变量和静态变量总是初始化为0.然后我的问题是,为什么我们在二进制文件中有单独的部分用于初始化和未初始化的数据.
我写了以下代码 -
int i;
int j=0;
static int k;
static int l=0;
int main()
{
static int m=0;
static int n;
printf("%d, %d\n",i,j);
printf("%d, %d\n",k,l);
printf("%d, %d\n",m,n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 -
0, 0
0, 0
0, 0
Run Code Online (Sandbox Code Playgroud)
我检查了bss部分的objdump输出,只有这部分包含变量.但根据链接 -
http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html
通常,在每个进程中,该进程可用的虚拟内存称为其地址空间.每个进程的地址空间通常分为6个部分,如下图所示:环境部分 - 用于存储环境变量和命令行参数; 堆栈,用于存储函数参数的内存,返回值和自动变量; 用于动态分配的堆(免费存储),两个数据部分(用于初始化和未初始化的静态和全局变量)和保存实际代码的文本部分.
所以,我只是困惑.如果我们有两个数据部分,为什么所有数据都放在.bss部分.而且我也想知道.data包含什么.
有人可以帮我这个吗?
我想编写一个读取二进制字符串表的程序.二进制在ELF上运行在REDHAT linux 32上.我做了以下 -
以下是我的程序的输出.
Entry Address of Binary - 0x8048340
Start of Program Header - 52
Start of section header - 3272
Size of header - 52
Number of section headers - 36
Size of each section headers - 40
Number of section headers - 36
Section header Offset - 3272
string tbl index for section[0] is 0
string tbl index for section[1] is 27
string tbl index for section[7] is 35
string tbl index for section[1879048182] …Run Code Online (Sandbox Code Playgroud) c ×2
assembly ×1
elf ×1
heap-memory ×1
linux ×1
machine-code ×1
process ×1
ptrace ×1
stack-memory ×1
x86 ×1