1, C + + memory classification (reference C + + Primer) (object in C language can be understood as a variable)
1.1. Static memory: Static memory is used to hold local static objects, class static data members, and variables defined outside of any function
1.2. Stack memory: Stack memory is used to store non-static objects defined within the function. (Of course, it contains the memory that the function parameter opens up)
1.3, Dynamic Memory: Each program also has a memory pool, this part of memory is called the Free Space (freedom Store) or heap. The program uses heaps to store dynamically allocated memory (dynamically allocate) objects, which are those that are allocated when the program is run. The lifetime of a dynamic object is controlled by the program, so when dynamic objects are no longer used, our code must display the destruction of them.
Note: Objects allocated in static or stack memory are automatically created and destroyed by the compiler, and for stack objects only exist when their defined blocks run, and static objects are allocated before they are used and destroyed at the end of the program.
2. C + + memory allocation related functions and operators
2.1, C allocates memory functions as follows: (under Linux query learned, using the command: Man 3 malloc)
<span style= "FONT-SIZE:18PX;" ></span>
<span style= "FONT-SIZE:18PX;" >name       malloc, free, calloc, realloc-allocate and free dynamic memorysynopsis       #include <stdlib.h>       void *malloc (size_t size);          void free  (void *ptr);       void *calloc (size_t nmemb, size_t size);       void *realloc (void *ptr, size_t size);</span>
size_t type: size_t is the unsigned integer type of the result of sizeof
size_t can store the maximum size of a theoretically possible object of any type (including array).
size_t is commonly used for array indexing and loop counting. Programs that and other types
It is a machine-related unsigned type that is large enough to keep the size of the objects in memory (source Baidu Encyclopedia)
The malloc function allocates a size byte, and the allocation succeeds in returning a pointer to that chunk of memory. Allocation failure will return null;
Calloc is similar to malloc, where the difference is that Calloc allocates nmemb*size bytes.
ReAlloc: Reallocate memory, typically used by programs to append memory. The memory block byte that the PTR points to is changed to size, and if PTR is null at the beginning,
Then this function implements malloc-like, otherwise ptr points to the content in the range (the PTR start and end points (min (old size,new size)) will not change, as
The new size is greater than the old size (the bytes allocated before PTR), so the extra part is not initialized, so I'm getting used to initializing this part
Save. Other parts are similar to malloc.
Free: This function will release the memory pointer PTR, which is returned by malloc, Calloc, realloc, otherwise it will be undefined behavior. If
PTR is empty, then free (PTR), will do nothing.   This tells us that after free (PTR), be sure to ptr=null; If not, PTR becomes a dangling finger.
PIN, if the memory is not allocated to PTR and then free (PTR), an unknown error is generated.
Note: malloc is usually used with free because the other two functions, in general, are actually malloc can replace them.
As an example, the program is as follows (approximate flow):
<span style= "FONT-SIZE:18PX;" >//Code ... char *ptr = NULL; PTR = (char *) malloc (* sizeof (char)); if (NULL = = Ptr) {          print strerror (errno);  Error message in Linux print} gets (PTR); Code ... free (PTR); PTR = NULL;   Good habit//code ... </span>
2.2c++ Allocating memory
In C + +, dynamic memory management is done through a bunch of operators.
NEW: Allocates space for the object in dynamic memory and returns a pointer to the object.
Delete: Takes a pointer to a dynamic object, destroys an object, and frees the memory associated with it. (array: delete [] array name),
Note:c with C + + allocated memory do not mix, such as using new allocated memory, use free () release, to know that delete needs to complete the series of destructors, and free is simply released. Sometimes a fluke makes no guarantee that other compilers will pass.
3. Where memory is allocated for attention (approximate)
3.1. After you have applied for memory space, you must check whether the assignment was successful.
3.2. when you do not need to use the requested memory, remember to release; After release, you should point the pointer to this memory to NULL, preventing the program from accidentally using it.
3.2. The two functions (malloc, free) should be paired. If it is not released after the application is a memory leak, if it is released for no reason, nothing is done. Release can only once, if released two times and more than two times an error (releasing null pointer exception, releasing the null pointer is actually equal to nothing, so release the null pointer released how many times no problem).
3.4. Although the type of the malloc () function is (void *), any type of pointer can be converted to (void *), but it is better to make a forced type conversion before, as this will avoid some compiler checks. 
3.5, c use a custom function to allocate memory generally use a level two pointer.
To see an example, a custom function allocates memory.
#include <stdio.h> #include <stdlib.h>char *allocate_p (char * str) {str= (char *) malloc (sizeof (char));} int main (void) {char *p;//in order to test for non-initialization of NULL    allocate_p (p);    Free (p);    printf ("OK"); return 0;} /* * Use GCC compiler to run error * * * * * * * * * * * * * * * * * * * * * glibc detected/test1:free (): Invalid POINTER:0XB771BFF4 ****** description p is a dangling pointer, then allocate_p () no space allocated. This is very similar to value passing, the P uninitialized memory is passed to STR, and the function allocates memory to STR, while P is the original uninitialized memory. When the allocation function is finished, STR's dynamic allocation of memory has not been released, * * * is still nothing. This leads to the release of the released, not the release of the release of * * * FREE (p) is undefined error */
Solutions.
/* * We pass the pointer to solve the value of insufficient delivery. * We can also use similar ideas to solve this problem. * */#include <stdio.h> #include <stdlib.h>char *allocate_p (char * str) {*str= (char *) malloc (sizeof (char));} int main (void) {char *p;//in order to test uninitialized null    allocate_p (&p);    Free (p);    printf ("OK"); return 0;} /* * ALLOCATE_P (&P); str=p's address. * So *str=p. therefore * *str= (char *) malloc (sizeof (char)); * Equivalent to  p== (char *) malloc (sizeof (char)); * When free (p) is equivalent to free (str), note that there is no memory leak */
Let's look at a classic example.
#include <stdio.h>char *returnstr () {    char *p= "Hello world!";    return p;} int main () {    char *str;     Str=returnstr ();    printf ("%s/n", str);        return 0;} #include <stdio.h>char *returnstr () {    char p[]= "Hello world!";    return p;} int main () {    char *str;     Str=returnstr ();    printf ("%s/n", str);        return 0;} /* Analysis: The only difference between these two functions is the local variable in returnstr *p/p[], "Hello world" is actually stored in the data area as a static string, but the person writing the program does not know the address, and the program itself knows it. When a function with {char p[] = "Hello world"; ...} When using this static string in a way that is actually equivalent to:  char p[12];  strcpy (P, "Hello World");  .... p[12] is temporarily allocated in the stack. Although P is pointing to "Hello world", this is a copy, not an original. When the function ends, char p[] is recycled by the program, so the content of p[] is no longer "Hello world". But if you use char *p= "Hello World", p points to the location where the static string is stored, that is, to the original of "Hello World", of course there is no problem. If you want to persist with char p[] without using char *p, the effective method must be: Char *returnstr () {static char p[]= "Hello World"; return p;} The static char [] is static and stored in the data area. */
Reference: http://blog.csdn.net/ssff1/article/details/5006722
linux-(c + +) dynamic memory allocation malloc and related learning