For the use of malloc

1, header malloc () function is stdlib.h, a function of the following statement:

     void* malloc(size_t size);

     Wherein the parameter size_t size represents the size of dynamic memory allocation space, in bytes.

    typedef size_t is redefined redefined role is to make this type of data user at a glance, the user indicates the parameter represents a length, in the size plus t, is represented by the integer data type, see later xxx_t type, integer data types are usually redefined.

    Here return malloc () function is a pointer value, or a first address of the memory space allocated

    If malloc () function for the success of some memory space is returned first address fails returns NULL

2 int * p;

     p = malloc(sizeof(int));

     Here is not written, error: invalid from the type 'void *' to type 'int' conversion should read:

       p =(int *) malloc(sizeof(int));

3, prior to the use of space malloc () function application, preferably with memset () function to clean up this memory space, because using malloc () function for the space of only guarantee is that the size of the memory space, does not guarantee memory whether there is space junk data

4, when the space is not in use malloc () function application, you should use the following function to free memory space out of this:

     void  free(void *ptr);

     Void * ptr is where malloc () return value of the function, that is, the first address of the memory space

     If you only know to use, do not know the release, then the embedded 7 days * 24 hours of operation, it is easy memory leak occurs, eventually leading to paralysis of the system

5, malloc () function allocates space dynamic programming:

     / ***************************************
the malloc () function allocates space dynamic programming:
( 1) define a type char * pointer variable p
(2) p-allocate 10 bytes of memory
copy "come on" string to the (. 3) p points to memory space
(4) p-pointer to another memory space expansion of 20 bytes
(5) ", baby!" replication character string pointed to the space behind the p
(6) to release the space pointed to by p
****************** ************************************************************ /
#include <the iostream>
#include <the cstdlib>
#include <CString>
the using namespace STD;


int main()
{
char *p;
p = (char *)malloc(10*sizeof(char));

memset(p,0,10*sizeof(char));

strcpy(p,"come on");
cout << "p: " << p << endl;

p =(char *)realloc(p,20*sizeof(char));
cout << "p: " << sizeof(p) << endl;

strcat(p,",baby!");
cout << "p: " << p << endl;

free(p);
    return 0;

}

Output:

p: come on
p: 4
p: come on,baby!

After 1 malloc memory in addition to wait until the free release, you can wait until the end of the program to run automatically released when
the process 2 program is running, if you have been malloc memory without releasing can cause this memory can not be used again, that memory leaks. This way, the application will be gradually used up all available memory and eventually causes the application to crash because of insufficient memory stop.
3 So a good habit is that we must remember that after malloc free of.

Guess you like

Origin www.cnblogs.com/invokerrr/p/11492617.html