栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

菜鸟写c之内存分配

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

菜鸟写c之内存分配

#include 
#include 
#include 
#include 

int or_goal_a;
double or_goal_b;
int stat_goal_c;
double stat_goal_d;
int main(int argc, char *argv[])
{
    int or_a, or_b;
    int *mallo_a = (int*)malloc(sizeof(int)); 
    int *mallo_b = (int*)malloc(sizeof(int));
    static int stat_ins_c, stat_ins_d;
    char *char_only_s = "hello";
    const char *con_char = "li";
    const int const_a;
    const double const_b;

    printf("------------------stack-----------------------------------------------------------------------------n");
    printf("insid_or is or_a is :      %p, or_b :      %pn",                                       &or_a, &or_b);
    printf("malloc is mallo_a:         %p, mallo_b:    %pn",                                       &mallo_a, &mallo_b); 

    printf("------------------heap-----------------------------------------------------------------------------n");
    printf("malloc object is mallo_a : %p, mallo_b:    %pn",                                       mallo_a, mallo_b);

    printf("------------------bss&data-----------------------------------------------------------------------------n");
    printf("goal is or_goal_a :        %p, or_goal_b : %p, stat_goal_c is %p, stat_goal_d is %pn", &or_goal_a, &or_goal_b, &stat_goal_c, &stat_goal_d);
    printf("insid_stat is stat_ins_c : %p, stat_ins_d :%pn",                                       &stat_ins_c, &stat_ins_d);

    printf("------------------text-----------------------------------------------------------------------------n");
    printf("char* is char* :           %pn",                                                       char_only_s);
    printf("const_char* is con_char:   %pn",                                                       con_char); 

    printf("------------------?????-----------------------------------------------------------------------------n");
    printf("const is const_a :         %p, const_b :   %pn",                                       &const_a, &const_b);

    return 0;
}

 

 

 目的

为了了解c语言对各种变量的分配。

 结论
  • 局部变量存放在栈区, 包括使用malloc的对象。
  • malloc出来的内存存放在堆区。
  • 全局变量,全局静态变量,静态变量存放一起。
  • 常量(char*),存放在一块区域。
  • const int存放在栈区。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号