C language dynamic memory

  1. Overview dynamically allocated memory

    • Chapter in the array, the length of the array is introduced predefined, fixed throughout the program, but in the actual programming, often this occurs, i.e., the memory requirements depend on the actual input data , and can not be predetermined. In order to solve the above problem, c language provides some memory management functions, these memory management functions can be dynamically allocated as needed memory space, the space can no longer use recycled again

  2. Static allocation, dynamic allocation

    • Static allocation

      • Compiling the program or during the operation, according to a predetermined pre-allocation of memory space allocated size int a [10]

      • You must know in advance the size of the space required

      • Partitioned stack area or global variable regions, generally in the form of an array

      • Distribution according to plan

    • Dynamic allocation

      • The program is running, allocate the required space needed free size

      • Distribution according to need

      • Heap allocation, typically using a particular distribution function

  3. Dynamic allocation functions

    • Required header files include 'stdlib.h'

    • malloc function

      • Function prototype: void * malloc (unsigned int size)

      • Function Description

        • Dispensing a length of dynamic memory storage area (stack region) is a continuous area size bytes, used to store the type described type specified. Prototype void * function returns a pointer to be cast accordingly use; memory content distribution uncertainty generally used memset initialized.

      • Return Value: Start address space allocation (allocation success) NULL (allocation failure)

      • note:

        • After calling malloc, we must determine what, if successful application memory

        • If multiple malloc memory applications, the first and second memory applications are not necessarily contiguous

      • For example

        • include 'stdlib.h'

        • include 'stdio.h'

        • include 'string.h'

        • int main () {

        • int count,*array,n;

        • printf ( "Please enter the number of array elements that you want to apply \ n");

        • scanf("%d",&n);

        • array = (int*)malloc(n *sizeof(int));

        • if (array = NULL)

        • {

        • printf ( "application memory failed \ n");

        • return 0;

        • }

        • memset(array,0,n*sizeof(int));

        • for(count=0;count<n;count++)

        • {

        • array[count] =count;

        • }

        • for(count = 0;count<n;count++)

        • {

        • printf("%d\n",array[count]);

        • }

        • free (array); // release at the memory array

        • return 0;

        • }

    • free function (to release memory function)

      • Function definition void free (void * ptr)

      • Function Description: free function frees the memory pointed to by ptr

      • Note ptr points to memory must be RAM malloc calloc relloc dynamic applications

      • note

        • After the free, because there is no assignment to p, so p still points to the original dynamic memory applications.

        • Memory can only free a dynamic application once, not many times free

    • calloc function

      • Function definition void * calloc (size_t, nmemb, size_t, size);

      • unsigned int size_t actually, it is in the file header, out with a typedef

      • Function Function: the heap memory, the application nmemb block size of each block is continuous area size bytes

      • Function's return value

        • Returns the application of the first address memory (the application is successful)

        • Returns NULL (application failure)

      • Note: malloc and calloc functions are used to allocate memory

      • the difference:

        • Name of the function is not the same

        • The number of parameters is not the same

        • malloc allocated memory, the memory content is stored in a random, uncertain, and the contents of memory in the application function calloc 0

      • Call the method

        • char*p = (char *)calloc(3,100)

        • In the application of the stack 3, for each block size of 100 bytes, 300 bytes i.e. contiguous regions

    • relloc function (re-application memory)

      • Memory functions malloc and calloc single application we call continuous, two memory is not the same application twice in a row, sometimes there is this demand that I use malloc or calloc first applied for a piece of memory, I still want to memory on the original application, or when I started to use malloc or calloc applied for a piece of memory, I would like to release a portion of the memory behind, in order to solve this problem, the invention of relloc function

      • Function is defined: void * relloc (void * s, unsigned int newsize)

      • Function features:

        • And then reapply the original memory on the basis of s point of memory, a new memory size is new_size if there is enough space behind the original memory, it added, if behind the memory is not enough, then relloc function, find a newsize bytes the size of the memory allocation, and the original contents of memory copied, and then deposited into the new memory address

      • If newsize smaller than the original memory, free up storage space behind the original memory. Leaving only the front newsize Returns: a new application of the first memory address

      • For example

        • char *p;

        • p = (char* )malloc (100);

        • // we want an additional 50 bytes 100 bytes later

        • p = (char *) relloc (p, 150); // p points to the new memory size is 150 bytes

        •  

        • char *p;

        • p = (char *)malloc(100);

        • // we want to re-apply for the memory, the new size is 50 bytes

        • p = (char *) relloc (p, 50); // p points to the new memory size is 50 bytes, 100 bytes after byte 50 is released

    • Note: malloc calloc relloc dynamic application memory, only then free or end of the program when it is released

    • Memory Leak

      • The concept of memory leaks:

        • Application memory, lost his first address, can not find, could not be used, also not released, this memory was leaked.

      • For example

        • int main () {

        • char*p;

        • p = (char*)malloc(100);

        • // Next, you can use the memory pointed to by p

        • p = "hello world"; // p points to somewhere else

        • // From then on, you could not find the application of 100 bytes, the dynamic application of 100 bytes was leaked

        • }

        •  

        • void fun(){

        • char *p;

        • p = (char*) malloc(100);

        • // Next, you can use the memory pointed to by p

        • }

        • int main () {

        • fun();

        • fun();

        • return 0;

        • // add free (p); solved a memory leak

        • // Each call fun leak 100 bytes

        • }

        • Solution 2

        •  

        • char*fun(){

          • char *p;

          • p = (char*) malloc(100);

          • // Next, you can use the memory pointed to by p

          • return p;

          • }

          • int main () {

          • char *q;

          • q = fun (); // q by using the dynamic application memory of 100 bytes

          • // remember release

          • free(p);

          • // Each call fun leak 100 bytes

          • }

    • Summary: memory allocation, must not the first address to the lost, when not in use be sure to free the memory

Guess you like

Origin www.cnblogs.com/fengzi759/p/11618708.html