C Language Memory allocation problem simple understanding

Source: Internet
Author: User

How many memory allocations do you have?
memory allocation for static storage stack heap
1. Allocate memory from a static storage area. When the program compiles, the memory is already allocated and exists during the entire run of the program, such as global variables.
2, created on the stack. When executing a function, the storage unit of the local variable within the function can be created on the stack, which is automatically freed when the function ends.
The processor's specified set has an allocation operation on the stack memory, so it is more efficient, but the allocated memory capacity is limited.
3, allocating memory on the heap, also known as dynamic memory allocation, when the program is running with the malloc function or the new operator to request any size of memory, the programmer
Use the free function or the delete operator to release memory. Dynamic memory usage is very flexible, but there are many problems.

1 //main.c2 intA =0;//Global Initialization Zone3 Char*P1;//Global Uninitialized Zone4   5 Main ()6 {  7     Static intc =0;//Global (Static) initialization zone8     intb//Stack9     CharS[] ="ABC";//StackTen     Char*P2;//Stack One     Char*P3 ="123456";//"123456\0" in the constant area, p3 on the stack.  AP1 = (Char*)malloc(Ten);  -P2 = (Char*)malloc( -);//areas that are allocated 10 and 20 bytes are in the heap area.  -}

C Language Memory allocation problem simple understanding

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.