Memory consumed by programs compiled by C/

Source: Internet
Author: User

/*********************************************************** *                                                         * * C Program Memory Layout * * * *********************************************************** * (high address space) *-----------------------------           ----*-command line parameter area-(command line arguments and environment variables) *---------------------------------*-heap area (stack areas) -Local Variables *-Stack area-malloc dynamic allocation data *---------------------------------*-Data area ) is divided into:-*-Uninitialized static variable area (. BSS)-no initialized global variable and static variable *-initialized static variable area (. data)-initialized global variable and static variable *-literal constant area (. Rod ATA)-Constant String *---------------------------------*-code area-binary code *---------- -----------------------* (Low address space) *code,.rodata,.data generated after the link *.BSS in the program initialization *stack,heap Open and release in the program operation * Stack area: Operates like a stack in a data structure, extending to the address space of the ground */#include<stdio.h>#include<stdlib.h>#include<string.h>intG_VAR1;//BSSintG_VAR2 =Ten;//DATAintMainintargcint*argv[]) {//argc:stack argv:stack *argv:commamd    CharArr[] ="Hello";//STACK    Char*PTR1 ="Hello,world";//Ptr1:stack, Ptr1->ro_data, "Hello,world": Ro_data    Char*PTR2 = NULL;//Ptr2:stack    inti =0; Static intS_VAR3;//BSS    Static intS_VAR4 = -;//DATA    Const int var=Ten;//Var:stackprintf ("command:*argv=% #lx \ n", *argv); printf ("stack:argv=% #lx, &argc=% #lx, &argv=% #lx \ n",argv,&argc,&argv); printf ("stack:&ptr1=% #lx, &ptr2=% #lx \ n", &ptr1,&ptr2); PTR2= (Char*) malloc (Ten);//Ptr2->heapprintf"heap:**argv=% #lx, ptr2=% #lx \ n", ptr2,**argv); printf ("argv[0]=%s, argv[0]=% #lx \ n", argv[0],argv[0]); printf ("BSS: &g_var1=% #lx, &s_var3=% #lx \ n", &g_var1,&s_var3); printf ("data:&g_var2=% #lx, &s_var4% #lx \ n", &g_var2,&S_VAR4); strcpy (PTR2,"haha"); printf ("ro_data:ptr1=% #lx, ptr2=% #lx \ n", PTR1,PTR2); return 0;}/*------------End of main---------------*/

Operation Result:

command:*argv=0x7fff2a4b8822
Stack:argv=0x7fff2a4b7a18,&argc=0x7fff2a4b78fc,&argv=0x7fff2a4b78f0
stack:&ptr1=0x7fff2a4b7908, &ptr2=0x7fff2a4b7900
heap:**argv=0x164b010,ptr2=0x2e612f2e
Argv[0]=./a.out, argv[0]=0x7fff2a4b8822
BSS: &g_var1=0x600b94, &s_var3=0x600b90
DATA:&G_VAR2=0X600B74, &s_var40x600b78
RO_DATA:PTR1=0X4007B8, ptr2=0x164b010

Reference: http://blog.csdn.net/xluren/article/details/8150723

Memory consumed by programs compiled by C/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.