在数据/ BSS /堆和栈内存分配 [英] memory allocation in data/bss/heap and stack

查看:106
本文介绍了在数据/ BSS /堆和栈内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段code的:

I have the following piece of code:

#include <stdio.h>

int global_var;

int global_initialized_var=5;

void function(){
int stack_var;

printf("The function's stack_var is at address 0x%08x\n", &stack_var);
}


int main(){
int stack_var;
static int static_initialized_var = 5;
static int static_var;
int *heap_var_ptr;

heap_var_ptr = (int *) malloc(4);

// Next variables will be at data segment 

printf("global_initialized_var is at address 0x%08x\n", &global_initialized_var);
printf("static_initialized_var is at address 0x%08x\n\n", &static_initialized_var);

// These will be in the bss segment

printf("static_var is at address 0x%08x\n", &static_var);
printf("global_var is at address 0x%08x\n", &global_var);

// This will be in heap segment

printf("heap_var is at address 0x%08x\n\n", heap_var_ptr);

// These will be in stack segment 

printf("stack_var is at address 0x%08x\n", &stack_var);
function();
}

我找回了以下内容:

I am getting back the following:

# ./memory_segments 
global_initialized_var is at address 0x0804a018
static_initialized_var is at address 0x0804a01c

static_var is at address 0x0804a028
global_var is at address 0x0804a02c
heap_var is at address 0x09285008

stack_var is at address 0xbf809fbc
The function's stack_var is at address 0xbf809f8c

据推测,在第一2个变量,因为它们被初始化静态和全局应该在.data段其中其他2 static_var和global_var应的.bss段。 ,我越来越觉得我的地址意味着他们两人都是在同一个内存区域。我会做一个盲目的猜测,我会说,这是.bss中的段。

It is supposed that the first 2 variables because they are initialized static and global should be in the .data segment where the other 2 static_var and global_var should be in .bss segment. The addresses that I am getting I think imply that both of them are in the same memory region. I would do a blind guess and I would say that this is the .bss segment.

但无论如何,问题是下面,我是不是正确的?如果我怎么可能找出是从他们开始的地方等这些地区(BSS,数据等)或限制。

Anyway the question is the following, am I right ?? And if I am how is it possible to find out where are the "limits" of these regions (bss, data, etc) or from where they are starting etc.

推荐答案

假设你的东西像编译 GCC memaddr.c -g -o memaddr ,您可以使用 objdump的-h 来显示大小和部分的地址:

Assuming you are compiling with something like gcc memaddr.c -g -o memaddr, you can use objdump -h to display size and address of your sections:


$ objdump -h memaddr | grep -e 'Size' -e '\.data' -e '\.bss'
Idx Name          Size      VMA               LMA               File off  Algn
 23 .data         00000018  0000000000601018  0000000000601018  00001018  2**3
 24 .bss          00000018  0000000000601030  0000000000601030  00001030  2**3
$

您也可以使用 objdump的-t 来显示地址和段的符号都属于:

Also you can use objdump -t to display addresses and sections your symbols belong in:


$ objdump -t memaddr | grep "_var"
000000000060102c l     O .data  0000000000000004              static_initialized_var.2049
0000000000601040 l     O .bss   0000000000000004              static_var.2050
0000000000601044 g     O .bss   0000000000000004              global_var
0000000000601028 g     O .data  0000000000000004              global_initialized_var
$

所以,我们可以看到。数据和.bss部分是相当小,正好都彼此相邻,所以这并不奇怪。数据的和.bss地址是如此接近。

So we can see that the .data and .bss sections are fairly small and happen to lie next to each other, so it is not surprising the .data and .bss addresses are so close.

这篇关于在数据/ BSS /堆和栈内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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