标签:style blog http color os io 2014 ar
基础知识:
一个程序的运行起来后,其在内存中有5个区域
1. 程序代码区
这个很简单,代码要执行,肯定要加载进内存, 我们不必关心。
2. 文字常量区
一般我们这样定义一个字符串时,其是在文字常量区的:
char* s1 = "hello, world";
char* s2 = "hello, world";
if(s1 == s2)
printf("s1和s2指向同一个在文字常量区的字符串");
这里, s1和s2指向的是同一个字符串
3. 静态存储区
全局变量,静态变量会放在这个区域,事实上,全局变量也是静态的。
以上1,2,3三个区域的内存在程序起来的时候就开辟好了的。
4. 栈
局部变量就是在栈里的。另外,函数调用时的参数也是在栈里的,这个现在不必关心
5. 堆
malloc或new出来的内存就是在堆里的,需要程序员自己管理清除。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
压栈顺序
从右到左,从上到下压
*/
int fun(int i, char * s)
{
/*
编译器中等同于
int a;
int b;
*/
int a,d;
printf("&s = 0x%08x\n", &s);
printf("&i = 0x%08x\n", &i);
printf("&f = 0x%08x\n", &fun);
printf("&a = 0x%08x\n", &a);
printf("&d = 0x%08x\n", &d);
}
/*
[root@localhost test_class]# ./a.out
&s = 0xbff4b284
&i = 0xbff4b280
&f = 0x08048404
&a = 0xbff4b26c
&d = 0xbff4b268
*/
int
main()
{
int i = 0x22222222;
char * s = "aaaa";
fun(i, s);
}/* 栈 -- 从高地址到低地址 堆 -- 从低地址到高地址 如下图 */
标签:style blog http color os io 2014 ar
原文地址:http://blog.csdn.net/cy_cai/article/details/38401635