C language (memory management)

Initialize memory space

#include <malloc.h> Import header file

  memset: using a constant fill byte memory
  memcpy: copy memory
  memmove: copy memory
  memcmp: Comparative memory
  memchr: search for a character in the memory space
    if the character string processing, the beginning of the 'mem' to 'str', Because this is for processing memory space;


Flexible memory management

void * malloc (size_t size); // Apply for dynamic memory space; (void can be converted to any type of data)
 // Function: apply to the system to allocate size bytes of memory space and return a pointer to this space for example : 
Int * ptr = ( int *) malloc ( sizeof ( int )); // heap memory

void  free ( void * ptr); // Release the dynamic memory space;
 // Because the dynamically applied memory space exists on the heap, if it is not actively released, it will be saved to the program close
 // Function: release the point pointed to by the ptr parameter The memory space, provided that the space must be applied by malloc, calloc, and realloc functions.
 // Note: If the parameter is NULL, no operation is performed. This function does not modify the value of ptr parameter, so after the call is still pointing to the original place
 // really just release the right to use the address, the address data stored in fact not destroyed; it is defined in the C language variables must be initialized Otherwise, the data is the garbage data used by the variable address last time;

void * calloc (size_t nmemb, size_t size); // Apply and initialize a series of memory spaces
 // Function: apply for nmemb continuous memory space of length saze and initialize to 0; namely: 
int * ptr = ( int *) calloc (N, sizeof ( int )); // Equivalent to 
int * ptr = ( int *) malloc (N * sizeof ( int ));
memset( ptr , 0 , N * sizeof(int) );

void * realloc ( void * ptr, size_t size); // Reallocate memory space (generally used for dynamic lengthening)
 // Function: ptr points to a reallocated space, release the old memory block and copy the data in the new space in 
If: ptr parameter is NULL, call corresponding to the malloc (size);
 // size parameter is 0; and ptr parameter is not NULL, equivalent to calling Free (ptr)

Note: If ptr parameter is not NULL; then he must be the space dynamically allocated by malloc, calloc, realloc


Memory leak

  Because the space allocated for the program is dynamically applied, the program is killed

    1. Implicit memory leak (the used memory block is not released in time with the free function);
    2. The memory block address is lost (the allocated address is lost, so that it cannot be released in time after being used up)


Memory layout rules

  From low to higher addresses stored: code segment -> segment -> BSS segment -> Heap -> stack
    code segment : refers generally used to keep the period of the discharge region code execution , and it has been determined before the program is running;
    data Segment : usually used to store initialized global variables and local static variables ;
    bss segment : usually used to store a memory area of uninitialized global variables in the program . (It will be automatically initialized to 0 before running)
    Heap : It is used to store the dynamically allocated memory segment during the operation of the process . The size is not fixed and can be dynamically expanded and contracted. (Dynamic application and release of memory)
    Stack : The memory area where the function is executed , usually Share the same area with the heap ( local variables, function parameters, function return value )

  The difference between heap and stack:
    application: manual application of heap; stack is automatically allocated by the system;
    release: manual application of heap; stack is automatically released by the system;
    life cycle: heap from dynamic application to active release; (local variables between functions can be accessed; That is, return to the calling function, the calling function can continue to be used.) The stack starts from the function call and ends when the function returns; (local variables between functions are not accessible to each other)
    Development direction: the heap and other sections are from low Address to high address development;
    stack is the opposite, from high address to low address development (push the stack);


Memory pool

  When releasing dynamically applied memory; first put it in the memory pool, you can directly use the memory in the memory pool when you apply for memory next time;
  because when you dynamically apply for memory, the program needs to access the memory, making the efficiency less ;

  (Available with a singly linked list)
—————————————————————————————————————————————— ---------
bit field

  Specify the occupied binary number ( one byte occupies eight binary bits ); (Only int type and boolean type support bit field)
    unsigned int a: 2; The statement here cannot be signed, and occupy two binary bits => at most can be expressed
  Note to 3 : The width of the bit field cannot exceed the length of the type it is based on [the int above only occupies at most 32 binary bits]

Guess you like

Origin www.cnblogs.com/TianLiang-2000/p/12712454.html