Data structure-C language memory allocation

Source: Internet
Author: User

Commonly used data stores:

In the C language, the memory space is divided into three zones depending on the time (lifetime) in which the data exists in memory:

1. Program Area: the code used to store the program, that is, the binary code of the program;

2. static storage: for storing global variables and static variables, the space of these variables has been allocated at the time of program compilation;

3. Dynamic Storage: The memory that is allocated when the program executes, and is divided into: Heap area (heap) and stack area (stack) two kinds.

Heap Area: used for dynamic memory allocation, which allocates memory on the heap by the memory allocation function when the program runs. In the C language, memory can be allocated dynamically as long as the pointer is used.

Stack Area: When the function is executed, the local variables inside the function and the memory area of the function parameters are stored, and the memory areas are freed automatically at the end of the function run. In the application of data structure-stack refers to the stack used to implement function calls, the system needs to create a variable store for function calls, where the store is from the stack area of the application, the stack of stacks is used to represent the stack.

Pointer instances:

 1#include <stdio.h>2  3 intMainvoid) 4 { 5     int*num =NULL;6     int*x, y[] = { A, A, +}, z = -; 7  8     //as shown below, pointers can act as variables or as arrays 9x=&z;//the address of the integer variable is assigned to xTenprintf"*x=%d, x[0]=%d\n", *x, x[0]);//100,100
&z indicates that Z's address is paid to X
*x indicates the contents of the current pointer.
One Ax = y;//the address of the array is assigned to x -printf"*x=%d, x[0]=%d, x[1]=%d, x[2]=%d\n", *x, x[0], x[1], x[2]);//12 12 22 32
Assigns the first address of an array to x - thex = y +1;//the second address of the array is assigned to x -printf"*x=%d, x[-1]=%d, x[0]=%d, x[1]=%d\n", *x, x[-1], x[0], x[1]);//22 - -x = y +2;//the third-digit address of the array is assigned to x +printf"*x=%d, x[-2]=%d, x[-1]=%d, x[0]=%d\n", *x, x[-2], x[-1], x[0]);//32 - + return 0; A}

memory leaks are mainly in the following situations:
    1. The memory allocation was unsuccessful, but it was used.
    2. The memory allocation succeeds, but it is not initialized to reference it.
    3. The memory allocation succeeds and has been initialized, but the operation crosses the memory boundary.
    4. Forgot to release memory, causing a memory leak.
    5. Frees up memory but continues to use it.

1#include <stdio.h>2#include <stdlib.h>3 4 intMainvoid)5 {6     int*p, I;7 8p = (int*)malloc(6*sizeof(int)) ;9     if(p = = NULL) {//determines whether the emptyTenprintf"Memory allocation Error! "); OneExit1); A     } -  -      for(i=0; i<6; i++) { thep++; -*p =i; -printf"%2d", *p); -     } +printf"\ n"); -  +      Free(p);//there was an error in running this sentence A  at     return 0; -}

I think that the application of the use of pointers can be replaced by the following:

#include <stdio.h>#include<stdlib.h>intMainvoid){    int*p, I; P= (int*)malloc(6*sizeof(int)) ; if(p = = NULL) {//determines whether the emptyprintf"Memory allocation Error! "); Exit (1); }     for(i=0; i<6; i++) { P = & i; printf ("%2d", *p); P++; } printf ("\ n"); Free (p); Run this sentence is to fight for    return 0;}

Memory release

A pointer used within a function is dynamically allocated memory and is not freed after it is exhausted. The reason for this is that all the variables inside the function die out after the function has finished running. This is wrong. The dynamically allocated memory is defined in the "heap", which is the free storage space used by the system, which is destroyed only after the program is finished, and does not die with the end of the function.

A pointer to a dynamically allocated memory that is set directly to NULL after it is exhausted. The reason: it is already null, and this is released. This is also wrong. The pointer can be arbitrarily assigned, and the memory is not freed, whereas the pointer is not NULL after the memory is freed.

The pointer is set to NULL, which indicates that the pointer is pointing to an empty address, but the pointer still occupies memory and is not released, which can cause a memory leak.

Reference Links:

Http://www.cnblogs.com/jiajinghai/archive/2011/11/09/2243709.html

Http://www.quanxue.cn/jc_clanguage/CLang/Clang13.html

Data structure-C language memory 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.