On the understanding of memory allocation in C language __c language

Source: Internet
Author: User

In the process of learning C language, it is necessary to master the memory allocation. The following is my understanding of memory allocation, if there are different views, please advise.

In the C language, objects can allocate memory space in a static and dynamic manner.

static Assignment : The compiler is assigned when the program source code is being processed. General program members in the source code directly defines the size of the memory allocation, the program in the process of running, will not change the allocated space. When the allocation of space is too small, it will cause overflow, and memory allocation is too much, it is easy to cause space waste.

Dynamic Assignment : The program calls the MALLOC library function request assignment at execution time. Programmers can allocate space according to their own needs without causing waste of space.

The main differences between static and dynamic memory allocations are as follows:

(1), the static memory allocation is before the program execution, thus the efficiency is high, but the dynamic memory allocation can be flexible to deal with the unknown purpose.

(2), static object is a variable with name, you can operate it directly; A dynamic object is a variable without a name, and it needs to be manipulated indirectly by a pointer.

(3), the allocation and release of static objects are handled by the compiler automatically; the allocation and release of dynamic objects must be managed explicitly by the programmer, by calling malloc () and free two functions.

stack, heap, static area in memory:

(1), stack area (stack)--by the compiler automatically allocate the release, store the function of the parameter values, local variables, and so on. The operation is similar to the stack in the data structure.

(2) heap area (heap)--typically released by programmers, if the programmer does not release, the program may end up being reclaimed by the OS. Note that it is different from the heap in the data structure, the distribution is similar to the list, hehe.

(3), static area (global area) (static)--the storage of static variables and global variables is placed in one piece, initialized global and static variables in an area, uninitialized global variables and uninitialized static variables are in another contiguous area, and the program ends with system release.

malloc/free function

The Malloc () function is used to request memory space in the heap, and the free () function frees the memory space originally requested. The Malloc () function allocates a contiguous space with a length of size byte in the dynamic storage area of memory. The argument is an unsigned integer that returns a pointer to the starting address of the allocated contiguous storage domain. Returns a null pointer when the function fails to allocate storage successfully, such as low memory.

Because the memory area is always limited, cannot be indefinitely allocated, and the program should try to save resources, so when the allocated memory area is not used, you want to release it for other variables or programs to use.

the use of malloc functions

Call form: (Type descriptor *) malloc (size)

Function: Allocates a contiguous area in the dynamic storage area of memory with a length of "size" byte. The return value of the function is the first address of the range. The type specifier indicates what data type is used for the zone. (type specifier *) indicates that the return value is cast to the type pointer. ' Size ' is an unsigned number.

For example: pc= (char *) malloc (100); Represents a 100-byte allocated memory space and is cast to a character array type, and the return value of the function is a pointer to the character array, which is assigned to the pointer variable PC.

Note: When you call the malloc function to allocate space again, if there is enough space behind the allocated space, the space needed is opened directly behind it. If there is not enough space behind the allocated space, look for enough space, copy the contents of the old space to the new space, and then release the new space.

use of the free function

Call form: Free (PC);
Function: Frees up a piece of memory space that the PC points to, the PC is a pointer variable of any type, it points to the first address of the freed area. The freed zone should be the area allocated by the malloc function. After you use the free function, you should assign the freed space to NULL, and the example PC = null.

When you use the free () function, you need to pay special attention to the following points:

(1) after the free () call is freed, no further access to the freed memory space is possible. After the memory is freed, it is very likely that the pointer still points to the memory unit, but this memory is no longer part of the original application, at which point the pointer is a hanging pointer (which can be assigned to null).

(2) The same pointer cannot be released twice. Because the memory space is freed, the space is given to the memory allocation subroutine, freeing the memory space to cause an error.

(3) Free can not be used to release the pointer space created by the malloc (), Calloc () and realloc () functions.

(4) in the C language program development, Malloc/free is a supporting use, that is, do not need the memory space to release the recovery.

(5) Cannot release part of dynamic memory.

(6) It is not possible to use dynamic memory out of bounds.

the difference between malloc and realloc

The main difference between function malloc () and function calloc () is that the former cannot initialize the allocated memory space, while the latter can.

The memory space allocated by the malloc () function has not been used before, each of which may be 0, whereas if this part of memory has been allocated, it may be left with a wide variety of data. That is, a program that uses the malloc () function starts (the memory space has not been reassigned) to work correctly, but the problem may occur over time (memory space has been reassigned).

The Calloc () function initializes each digit in the allocated memory space to zero. That is, if you are allocating memory for character type or integer type elements, then these elements will be guaranteed to be initialized to 0; If you are allocating memory for elements of the pointer type, then these elements are usually initialized to null pointers If you allocate memory for real data, these elements are initialized to 0 of the floating-point type.

The above is my learning dynamic memory after some personal understanding, if my understanding is wrong, I hope to be able to point out to me, so that I can better understand the memory space allocation.

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.