Variable storage location

Source: Internet
Author: User

1. First, tell the difference between heap heap and stack stack:

A program that has been compiled by C + + has divided the memory into a few parts

(1), Stack stack: The compiler automatically assigns the release,

Store local Variables

(2) Heap area heap: The programmer is usually assigned to release, if the programmer does not release, the end of the program may be recycled by the OS,

Note that it is different from the heap in the data structure, and is distributed in a way similar to a linked list.

Storage of malloc and new allocated space

(3), global (Static) static: The storage of global variables and static variables (global and local static variables) is placed in a block.

Initializes the global variables and static variables in an area,

Uninitialized global variables and static variables are placed in another adjacent area.

Released by the system after the program is finished.

(4), literal constant area: the constant string is placed here.

Released by the system after the program is finished.

(5), Program code area: Store the function body of the 2 code.


It was written by a predecessor, very detailed

Main.cpp

int a = 0; Global initialization Zone

Char *p1; Global uninitialized Zone

Main ()

{

int b;//Stack

Char s[] = "ABC"; Stack

Char *p2; Stack

Char *p3 = "123456"; 123456\0 ";//in the constant area, p3 on the stack.

static int c = 0;//global (Static) initialization area

P1 = (char *) malloc (10);

P2 = (char *) malloc (20);

Areas that are allocated 10 and 20 bytes are in the heap area.

strcpy (P1, "123456"); 123456\0 is placed in a constant area, the compiler may optimize it to a place with the "123456" that P3 points to.

}

Variable storage location

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.