Program memory allocation

Publisher:TapirLatest update time:2015-09-29 Source: eefocusKeywords:program Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Answer: The memory occupied by a program compiled by C/C++ is divided into the following parts:
1. Stack area (stack) - automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. Its operation is similar to the stack in the data structure.
2. Heap area (heap) - generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS when the program ends. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list, haha.
3. Global area (static area) (static) - global variables and static variables are stored together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. Released by the system after the program ends.
4. Text constant area - constant strings are placed here. Released by the system after the program ends.
5. Program code area - stores the binary code of the function body

 

Example program
This is written by a senior, very detailed
//main.cpp
  int  a=0;     //global initialization area
  char  *p1;    //global uninitialized area
  main()   
  {
   intb; stack
   char  s[]="abc";    //stack
   char  *p2;          //stack
   char  *p3="123456";    //123456 is in the constant area, p3 is on the stack.
   static  int  c=0;    //global (static) initialization area
   p1  (char*)malloc(10);
   p2  (char*)malloc(20);    //the allocated 10 and 20 byte areas are in the heap area.
   strcpy(p1,"123456");    //123456 is placed in the constant area, the compiler may optimize it and p3 to "123456" into one place.
}

Keywords:program Reference address:Program memory allocation

Previous article:MCU simulates serial port transmission and baud rate issues
Next article:struct and typedef struct

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号