Brief Introduction to dynamic memory allocation

Source: Internet
Author: User

Dynamic Memory Allocation

Contents

Storage Partition

Heap Memory Allocation Function

Other memory allocation functions

Memory Operation Functions

1. Memory partitioning


Stack Zone

Int A = 3

Heap Area

Malloc (255)

Static storage Zone

Static float H = 1.36f

Constant Area

"Lanou"

Code Area

Int getcount (){...}

The custom function is located at the top of the stack.

The main function is located at the bottom of the stack.

The main function is both the entry of the program and the exit of the program.

Stack Filo (Advanced and backward) (equivalent to one-way cup)

Queue (Queue) FIFO (first-in-first-out) (equivalent to two-way exit for ticket buying at the railway station)

Stack memory allocation principles:

The system automatically allocates storage space based on the number of bytes occupied by the data type. After the storage space is used, the system automatically recycles it.

Heap Area

Memory allocated by the malloc function.

Manual allocation and manual release.


// The string array is allocated in the memory of the system's automatic word stack and is automatically recycled by the system at the end of function call, therefore, the first address of the storage area opened in the stack memory cannot be returned as the return value of the function. The compiler first reports a warning, and other functions cannot access the corresponding content even if they receive the returned address.

When the variables defined in the function are directly used as return values, the variables defined in the main function copy the returned values. When the function call ends, the variable storage space defined in the function will be recycled by the system, and the variables that accept the function return value in the main function can be used normally.

Static storage Zone

Static int A = 5;

Static char string [255];

Only initialize once

The memory value after Initialization is 0.

It is released only when the program exits (always exists ).

The variable is stored in the static storage zone when the variable type is prefixed with static.

Constant Area

'A' character constant

5 Integer constants

"IPhone" String constant

Constants occupy memory, read-only status, and cannot be modified!

Char * string = "iPhone ";

String [0] = 'a ';Run crash!

When the pointer variable is used to receive the address of a constant string, the data accessed through the pointer is read-only and cannot be modified. A crash occurs when the address is modified.

Code Area

If (A> B ){

Printf ("% d", );

}

After all statements are compiled, CPU commands are generated and stored in the code area.

2. Dynamic heap memory allocation

Void * malloc (unsigned int size );

The first address of the memory is returned.

Memory release

Void free (void *)

The free function is used to release the memory, and the memory is released to mark the deletion.

3. Other allocation functions

Calloc

Void * calloc (unsigned N, unsigned size)

Allocate n size spaces and clear all the bytes in the memory.

Void * realloc (void * P, unsigned newsize );

Reallocate the IP address based on the specified address and size.

As a realloc function, if the original heap memory is large enough in the future, the realloc function will append newsize to the original space-the original number of bytes. If it is not large enough, A new newsize memory space will be searched and the original heap memory will be free.

4. Memory Operation Functions

Memory copy

Void * memcpy (void * DEST, const void * Source, size_t N)

Copy data from the memory indicated by sourse to DEST and n Bytes.

Initialize memory

Void * memset (void * s, int C, size_t N)

Slave

Memory comparison

Int memcmp (const void * buf1.const void * buf2, unsign int count)

Compare whether buf1 and buf2 point to the same memory and count bytes


Brief Introduction to dynamic memory allocation

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.