Introduction to heap, stack, and queue in computer

Source: Internet
Author: User

1. Differences in heap allocation and stack allocation

It is generally believed that

C

are divided into these storage areas

Stack heap Global Zone:

1

Stack

There is a compiler that automatically assigns the release

2

Heap

Usually released by the programmer, if the programmer does not release, the program may end up

OS

Recovery

3

Global (static), global variables and static variables are stored in a block, initialized global variables and static variables in an area, uninitialized global variables and

An uninitialized static variable is in another area adjacent to it.

Program End Release

4

There is also a special place to put constants.

Program End Release

Difference:

The variables defined in the function body are usually on the stack, with

Malloc

Calloc

ReAlloc

The allocated memory functions are allocated on the heap. Defined outside the body of all functions

is the global amount, plus the

Static

Modifier is stored in the global zone (static zone), regardless of where it is located

,

Defined outside the body of all functions

Static

The variable representation is valid in the file and cannot be

extern

to other files, defined in the function body.

Static

Represents only valid in the body of the function. In addition, the function of the

"ADGFDF"

Such a string is stored in a constant area. Than

Such as:

Code:

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

//

Global (static) initialization zone

P1

=

(Char

*) malloc (10);

P2

=

(Char

*) malloc (20);

//

Assigned to

10

And

20

The area of the byte is in the heap area.

strcpy (P1,

"123456");

123456\0

In a constant area, the compiler may associate it with the

P3

The point of

"123456"

Optimized into a piece.

There is a series of reserved field and pass parameters on the stack when the function is called. The stack has a limited space size,

Vc

The default is

2M

。 Stack

In general, a large number of arrays are allocated in the program and the recursive function hierarchy is too deep. It is important to know that when a function call finishes returning it releases the function in the

Some stack space. Stacks are automatically managed by the compiler, and you don't have to worry about them.

The heap is dynamically allocated memory, and you can allocate it with very large memory. However, a memory leak is caused by bad use. and frequently

Malloc

And

Free

Yes

Generate memory fragmentation (a bit similar to disk fragmentation) because

C

When allocating dynamic memory, it looks for matching memory. The stack does not produce fragmentation.

Accessing data on the stack is faster than accessing the data on the heap via pointers. Generally speaking stack and stack is the same, is the stack

(Stack)

, and it is only when the heap is said

is a heap

Heap

。 Stacks are first-in, usually from high-address to low-address growth.

Heap

(heap)

and stacks

(Stack)

Heap

(heap)

and stacks

(Stack)

Is

C + +

There are two basic concepts that are inevitably encountered in programming. First of all, these two concepts can be found in the Book of data structure, they are the basic structure, although the stack is simpler. In the specific

C + +

In the programming framework, these two concepts are not parallel. The study of the underlying machine code can reveal that

The stack is the data structure provided by the machine system, and the heap is provided by the C/S function library.

Specifically

Modern computer

(

Serial execution mechanism

)

, the data structure of the stack is supported directly at the bottom of the code.

This is reflected in the

There are special registers pointing to the stack

Address, there is a dedicated machine instruction to complete the data into the stack out of the stack operation. This mechanism is characterized by high efficiency, limited data support, generally integers, pointers, floating-point

Data types that are directly supported by a number of systems and do not directly support other data structures. Because of this feature of the stack, the use of stacks is very frequent in the program. Child programs

The call is done directly using the stack. of the Machine

Pager

The instruction implies the operation of pushing the return address into the stack and then jumping to the address of the subroutine, while the sub-program

Ret

Instruction is

Suppresses the action that pops the return address from the stack and jumps to it.

C + +

is an example of using the stack directly, which is why when the function returns, the function's

The reason why automatic variables fail automatically.

Different from the stack,

The data structure of the heap is not by the system

(

Whether it's a machine system or an operating system

)

Supported,

Instead, it is provided by a library of functions.

of basic

Malloc/realloc/free

The function maintains a set of internal heap data structures. When the program uses these functions to obtain new memory space, the function first attempts to find the available memory from the internal heap empty

, if there is no memory space available, try to use the system to dynamically increase the size of the program data segment memory, the newly allocated space is first organized into the internal

Heap, and then return to the caller in the appropriate form. When the program frees the allocated memory space, this memory space is returned to the internal heap structure and may be appropriately

Processing

(

For example, and other free space to merge into a larger free space

)

To better fit the next memory allocation request.

This complex allocation mechanism is actually equivalent to a memory allocation

The buffer pool

(Cache)

, there are several reasons for using this set of mechanisms:

1.

System calls may not support memory allocations of any size.

Some system calls only support a fixed size and its multiple memory requests. This can be wasteful for a large number of small memory classifications.

2. System calls to request memory can be costly. System calls may involve conversion of the user state and kernel mentality.

3. Memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory.

Introduction to heap, stack, and queue in computer

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.