1. executable File structure:
1) Code area: contains opcode and operand, constant data (const declaration), immediate number, code area is shared, and only read-only is provided.
2) global/static data area: Contains the initialized global data and initializes the static data.
3) Uninitialized data area (BBS): Contains uninitialized global data and uninitialized static data.
2. Process structure:
1) Code Area: Loads the executable program code snippet, which is completed by the loader.
2) Global Data area: Load executable data segment, the data life cycle is the whole program running process.
3) Uninitialized data area: Loading uninitialized data, data lifetime is the whole program running process.
4) Stack area: Automatically allocated by the compiler, storing the function's parameter value, return value, local variable, function completion call is released.
5) Heap Area: Dynamic allocation of memory, by the programmer to allocate and release, if the programmer is not released, then finally released by the OS.
3. Heap and Stack differences:
Stack is automatically managed by the system by the allocal () function to complete the application, automatic completion of the release, is a continuous area downward expansion, space is limited, more than will overflow;
A heap is a discontinuous region extending to a high address, which is requested/released by Malloc/free (New/delete), is inefficient, and produces more fragmentation.
4. Memory Management Common functions:
1) malloc ()/free () (c + +: New/delete): Dynamically Allocate/free memory
2) realloc (): Used to change the memory space already configured in the heap
3) calloc (): Initialize allocated memory to 0, usage similar to malloc ()
4) allocal (): Allocates memory for stack, automatically releases
5) Memcopy (): copies n bytes from the starting position of the memory address referred to by the source SRC to the beginning of the memory address referred to by the target dest
6) Memmove (): Used to copy count characters from SRC to dest, if the target region and source area overlap,memmove can ensure that the source string copies the bytes of the overlapping region into the target area before being overwritten.
7) memset (): Fills a block of memory with a given value, which is the quickest way to clear 0 operations on large structures or arrays
8) memcmp (): Compare the first count bytes of the memory area BUF1 and BUF2
9) MEMCHR (): Find characters from the first count byte of the memory area referred to by BUF CH
Implementation of 5.memcopy ():
1 void* Memcopy (void* Dest,Const void*src, size_t num)2 {3 Char* PDEs = (Char*) dest;4 Char* Psrc = (Char*) src;5ASSERT (dest!=null && src!=null && num>0);6 while(num--)7*pdes++ = *psrc++;8 returndest;9}
Implementation of 6.memmove ():
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <assert.h>5 void* Memmove (void* Dest,Const void*src, size_t num)6 {7 Char* PDEs = (Char*) dest;8 Char* Psrc = (Char*) src;9ASSERT (dest!=null && src!=null && num>0);Ten //determine if there is overlap between dest and src One if(Pdes+num <psrc | | psrc+num < PDES)//there is no overlap, normal from front to back copy A while(num--) -*pdes++ = *psrc++; - Else //there is overlap, copying from backward forward the { -PDEs = pdes+num-1; -PSRC = psrc+num-1; - while(num--) +*pdes--= *psrc--; - } + returndest; A } at intMain () - { - int Constn= -; - CharDest[n]; - Char* src="Hello world."; -printf"%s\n", SRC); inmemset (dest,0Nsizeof(Char)); - Char*result= (Char*) memmove (Dest,src,strlen (SRC)); toprintf"%s\n", result); + return 0; -}
7.memmove () differs from memcopy ():
When the memory does not collapse, the function of the two is the same, when the memory collapses, memmove () can ensure that the output is correct, memcopy () is not guaranteed, memcopy () is a subset of Memmove ().
Linux program and process memory structure