Chinaunix首页 | 论坛 | 博客
  • 博客访问: 246682
  • 博文数量: 37
  • 博客积分: 837
  • 博客等级: 准尉
  • 技术积分: 566
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-26 17:36
文章分类

全部博文(37)

文章存档

2012年(31)

2011年(6)

我的朋友

分类: C/C++

2012-11-10 13:31:30

In C, as in other compiled languages, the compiled code goes into the text segment, while the variables reside in the remaining segments. Exactly which memory segment a variable will be stored in depends on how the variable is defined. Variables that are defined outside of any functions are considered to be global. The static keyword can also be prepended to any variable declaration to make the variable static. If static or global variables are initialized with data, they are stored in the data memory segment; otherwise, these variables are put in the bss memory segment. Memory on the heap memory segment must first be allocated using a memory allocation function called malloc(). Usually, pointers are used to reference memory on the heap. Finally, the remaining function variables are stored in the stack memory segment. Since the stack can contain many different stack frames, stack variables can maintain uniqueness within different functional contexts. The memory_segments.c program will help explain these concepts in C.

2.7.2.1. memory_segments.c

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. int global_var;

  3. int global_initialized_var = 5;

  4. void function() { // This is just a demo function.
  5.    int stack_var; // Notice this variable has the same name as the one in main().

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


  8. int main() {
  9.    int stack_var; // Same name as the variable in function()
  10.    static int static_initialized_var = 5;
  11.    static int static_var;
  12.    int *heap_var_ptr;

  13.    heap_var_ptr = (int *) malloc(4);

  14.    // These variables are in the data segment.
  15.    printf("global_initialized_var is at address 0x%08x\n", &global_initialized_var);
  16.    printf("static_initialized_var is at address 0x%08x\n\n", &static_initialized_var);

  17.    // These variables are in the bss segment.
  18.    printf("static_var is at address 0x%08x\n", &static_var);
  19.    printf("global_var is at address 0x%08x\n\n", &global_var);

  20.    // This variable is in the heap segment.
  21.    printf("heap_var is at address 0x%08x\n\n", heap_var_ptr);

  22.    // These variables are in the stack segment.
  23.    printf("stack_var is at address 0x%08x\n", &stack_var);
  24.    function();
  25. }

Most of this code is fairly self-explanatory because of the descriptive variable names. The global and static variables are declared as described earlier, and initialized counterparts are also declared. The stack variable is declared both in main() and in function() to showcase the effect of functional contexts. The heap variable is actually declared as an integer pointer, which will point to memory allocated on the heap memory segment. The malloc()function is called to allocate four bytes on the heap. Since the newly allocated memory could be of any data type, the malloc() function returns a void pointer, which needs to be typecast into an integer pointer.


  1. reader@hacking:~/booksrc $ gcc memory_segments.c
  2. reader@hacking:~/booksrc $ ./a.out
  3. global_initialized_var is at address 0x080497ec
  4. static_initialized_var is at address 0x080497f0

  5. static_var is at address 0x080497f8
  6. global_var is at address 0x080497fc

  7. heap_var is at address 0x0804a008

  8. stack_var is at address 0xbffff834
  9. the function's stack_var is at address 0xbffff814
  10. reader@hacking:~/booksrc $

The first two initialized variables have the lowest memory addresses, since they are located in the data memory segment. The next two variables, static_var and global_var, are stored in the bss memory segment, since they aren't initialized. These memory addresses are slightly larger than the previous variables' addresses, since the bss segment is located below the data segment. Since both of these memory segments have a fixed size after compilation, there is little wasted space, and the addresses aren't very far apart.

The heap variable is stored in space allocated on the heap segment, which is located just below the bss segment. Remember that memory in this segment isn't fixed, and more space can be dynamically allocated later. Finally, the last two stack_vars have very large memory addresses, since they are located in the stack segment. Memory in the stack isn't fixed, either; however, this memory starts at the bottom and grows backward toward the heap segment. This allows both memory segments to be dynamic without wasting space in memory. The first stack_var in the main() function's context is stored in the stack segment within a stack frame. The second stack_var in function() has its own unique context, so that variable is stored within a different stack frame in the stack segment. When function() is called near the end of the program, a new stack frame is created to store (among other things) the stack_var for function()'s context. Since the stack grows back up toward the heap segment with each new stack frame, the memory address for the second stack_var(0xbffff814) is smaller than the address for the first stack_var (0xbffff834) found within main()'s context.

阅读(1735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~