1.
Static memory
Compile the pre-application of memory, do not need to manage their own, the function after the completion of the automatic recovery
Defects:
A function similar to the following will make an error:
Char *uppr (char *oldstring) { char newstring[]; ... return newstring;}
Because the character array new has been destroyed since the function was executed, the position pointed to by the pointer is of course not the correct address.
This function can be resolved in the following ways:
void uppr (char *old,Char *new) { ...}
You can pass in a two-character array and then rewrite it back to new.
The reason for this is that the array address represented by new is defined by the main function, and the change is not destroyed with the child function destruction, so the change of the pointer of the main function variable can be implemented in the child function, the classic implementation is the double return exchange
As follows:
void swap (int *a,int *b) { int C; C=*A; *a=*b; *b=C;} Call Swap (&a,&b);
2.
Overflow
Using more than the requested memory, that is, the request array is 100 elements of the case is written to 101 and above elements, or attempt to read to 101 or more locations of the elements
The result of a reference error is not predictable, and usually security is derived from the overflow area, reading a location that could not be read, and launching another program.
Errors written may cause data to be overwritten by other programs, and the program may also have errors.
How to resolve:
Typically, the size of the space is determined at the time of operation, intercepted, and is about to be partially removed, but this can result in data loss
3.
Dynamic memory
The memory that is allocated dynamically during the program's run, usually the application itself
Main functions
#include <stdlib.h>void *malloc (size_t size)void *calloc (size_t nmemb,size_t size)
Freeing Memory void *free (void *ptr)
Adjusting Memory void *realloc (void *ptr,size_t size)
Note that the release of dynamic memory is not determined by the system, but must be freed by using the free function, so you can return a pointer to the requested memory in a child function
For example:
Char*UPPR (Char*Old ) { Char*New; if(New=malloc (strlen (old) +1) {strcpy (New, old); inti; for(i=0; I<strlen (New);++i) { if(New[i]>= the&&New[i]<=122){ New[i]-= +; } } }Else{exit (255); } return New;}
When the child function is executed, new is not destroyed, so the address can be returned, but it is best to get rid of it after the operation is complete.
In addition, the ReAlloc function, when PTR is null, is equivalent to Malloc,size 0 o'clock, which is equivalent to free
Note the free pointer changes to a dangling pointer and cannot be accessed (it is not possible to access the dangling pointer with unpredictable exceptions).
4.
Allocation stack
#include <stdlib.h>void *alloca (size_t size)
The allocated memory is automatically retracted at the end of the function and is seldom used
5.
Memory lock
In general, if the memory is not used for a period of time, the system automatically transfers it to the original location on disk to place new data, and if you want to prevent this zone from being transferred, you can use a memory lock
Common functions are
#include <sys/types.h>
Lock Memory Header address + length int mlock (constvoid *addr,size_t length);
Unlock memory, first address + length int munlock (void *addr,size_t lemgth);
/* Lock memory page, flag option:
*mcl_current All Memory Pages
*mcl_future All address pages added for the process
*/
int mlockall (int flag); int munlockall (void);
(The above function is only for root user)
Memory management-linux C Programming Guide