C/C ++ memory (continued)

Source: Internet
Author: User

Stack and stack differences:

1. Application Method

STACK: automatically allocated by the system. For example, if a local variable int B is declared in a function, the system automatically opens up space for B in the stack.

Heap: the programmer must apply for it and specify the size. Use the malloc function in C.

For example, p1 = (char *) malloc (10 );

In C ++, use the new operator, for example, p2 = (char *) malloc (10 );

The allocated 10-byte area is in the heap area. But note that p1 and p2 are in the stack.

2. system response after application

3. Application size limit

STACK: refers to the data structure extended to the low address, a continuous memory area.

Heap: it refers to the data structure extended to the high address and is a non-consecutive area.

4. Comparison of Application Efficiency

STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.

Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use.

5. Storage content in heap and stack

STACK: When a function is called, the first entry to the stack is the address of the next instruction of the main function, and then the parameters of the function. Static variables are not included in the stack. When this function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage.

Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

6. Comparison of storage efficiency:

The array on the stack is faster than the string pointed to by the pointer, such as the heap.

In short:

Heap is a stack, and stack is a stack.

Stack space is automatically allocated and released by the system, and heap space needs to be manually allocated and released.

Stack space is limited, and heap is made up of a large free storage zone.

Stack starts from the highest address, and heap starts from the lowest address.

The memory space allocated by the malloc function in C is on the heap, and C ++ corresponds to the new operator.

During the compilation period, the program allocates both the variable and Function Memory on the stack, and the parameter transfer is also carried out on the stack when the function is called during the program running.

First, let's take an example:

Void f (){

Int * p = new int [5];}

This short sentence contains the heap and stack. When we see new, we should first think that we allocated a heap memory. What about the pointer p? It allocates a stack memory, so this sentence means that the stack memory stores a pointer p pointing to a heap memory.


This article is from the "IT migrant workers a" blog, please be sure to keep this source http://7633061.blog.51cto.com/7623061/1266378

Related Article

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.