C/C++内存分析

//http://blog.youkuaiyun.com/morewindows/article/details/6851681
#include <stdio.h>
#include <malloc.h>
int g_i = 100;
int g_j = 200;
int g_k, g_h;
int main()
{
    const int MAXN = 100;
    int *p = (int*)malloc(MAXN * sizeof(int));
    static int s_i = 5;
    static int s_j = 10;
    static int s_k;
    static int s_h;
    int i = 5;
    int j = 10;
    int k = 20;
    int f, h;
    char *pstr1 = "MoreWindows123456789";
    char *pstr2 = "MoreWindows123456789";
    char *pstr3 = "Hello";


    printf("堆中数据地址:0x%08x\n", p);

    putchar('\n');
    printf("栈中数据地址(有初值):0x%08x = %d\n", &i, i);
    printf("栈中数据地址(有初值):0x%08x = %d\n", &j, j);
    printf("栈中数据地址(有初值):0x%08x = %d\n", &k, k);
    printf("栈中数据地址(无初值):0x%08x = %d\n", &f, f);
    printf("栈中数据地址(无初值):0x%08x = %d\n", &h, h);

    putchar('\n');
    printf("静态数据地址(有初值):0x%08x = %d\n", &s_i, s_i);
    printf("静态数据地址(有初值):0x%08x = %d\n", &s_j, s_j);
    printf("静态数据地址(无初值):0x%08x = %d\n", &s_k, s_k);
    printf("静态数据地址(无初值):0x%08x = %d\n", &s_h, s_h);

    putchar('\n');
    printf("全局数据地址(有初值):0x%08x = %d\n", &g_i, g_i);
    printf("全局数据地址(有初值):0x%08x = %d\n", &g_j, g_j);
    printf("全局数据地址(无初值):0x%08x = %d\n", &g_k, g_k);
    printf("全局数据地址(无初值):0x%08x = %d\n", &g_h, g_h);

    putchar('\n');
    printf("字符串常量数据地址:0x%08x 指向 0x%08x 内容为-%s\n", &pstr1, pstr1, pstr1);
    printf("字符串常量数据地址:0x%08x 指向 0x%08x 内容为-%s\n", &pstr2, pstr2, pstr2);
    printf("字符串常量数据地址:0x%08x 指向 0x%08x 内容为-%s\n", &pstr3, pstr3, pstr3);
    free(p);
    return 0;
}


#include <stdio.h>
void fun(int i)
{
    int j = i;
    static int s_i = 100;
    static int s_j;

    printf("子函数的参数:        0x%p = %d\n", &i, i);
    printf("子函数 栈中数据地址: 0x%p = %d\n", &j, j);
    printf("子函数 静态数据地址(有初值): 0x%p = %d\n", &s_i, s_i);
    printf("子函数 静态数据地址(无初值): 0x%p = %d\n", &s_j, s_j);
}
int main()
{
    int i = 5;
    static int s_i = 100;
    static int s_j;

    printf("主函数 栈中数据地址: 0x%p = %d\n", &i, i);
    printf("主函数 静态数据地址(有初值): 0x%p = %d\n", &s_i, s_i);
    printf("子函数 静态数据地址(无初值): 0x%p = %d\n", &s_j, s_j);
    putchar('\n');

    fun(i);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值