C language function malloc (): dynamically allocate memory space

Header file: #include <stdlib.h>

malloc () function is used to dynamically allocate memory space (if you do not understand the dynamic memory allocation, please see: C language dynamic memory allocation and variable storage class ), its prototype:
void * malloc (size_t size);

parameter Description] size is the size of the memory space to be assigned to the byte (byte) weight.

[DESCRIPTION] Function malloc () allocates a specified memory size for storing the data in the heap area. This memory space will not be initialized after the function execution is completed, their values are unknown. If you want to allocate memory is initialized at the same time, use  calloc ()  function.

[Return Value] Returns a pointer to the memory allocated successfully address the failure NULL is returned.

Since there may or may not apply when the memory space, it is necessary to independently determine the application is successful, then subsequent operations.

If size is 0, then the return value will vary depending on the standard library implementation to implementation, may be NULL, or may not, but returned pointer should not be referenced again.

Note: The return value type of the function is void *, void does not mean that there is no return value or returns a null pointer, but returns a pointer type is unknown. Therefore, the use malloc () is usually required casts to convert the void pointer type we want, for example:

. 1  char * PTR = ( char *) the malloc ( 10 );   // allocate 10 bytes of memory space used to store character

Dynamic memory allocation example:

. 1  . 1 #include <stdio.h>   / * the printf, Scanf, NULL * / 
2   2 #include <stdlib.h>   / * the malloc, Free, RAND, System * / 
. 3   . 3  
. 4   . 4  int main ()
 . 5   . 5 {
 . 6   . 6      int I, n-;
 . 7   . 7      char * Buffer;
 . 8   . 8  
. 9   . 9      the printf ( " enter the length of the string: " );
 10  10      Scanf ( " % D " , & I);
 . 11 . 11  
12 is  12 is      Buffer = ( char *) the malloc (I + . 1 );   // string containing the last \ 0 
13 is  13 is      IF (Buffer == NULL) Exit ( . 1 );   // determines whether the allocation is successful 
14  14  
15  15      // random generated strings 
16  16      for (n-= 0 ; n-<I; n-++ )
 . 17  . 17          Buffer [n-] = RAND ()% 26 is + ' A ' ;
 18 is  18 is      Buffer [I] = ' \ 0 ' ;
 . 19 . 19  
20 is  20 is      the printf ( " random string is generated:% S \ n- " , Buffer);
 21 is  21 is      Free (Buffer);   // free up memory 
22 is  22 is  
23 is  23 is      System ( " PAUSE " );
 24  24      return  0 ;
 25  25 }

The result:
the length of the input string: 20
randomly generated string: phqghumeaylnlfdxfirc

the program generates a string of specified length, and filled with a randomly generated characters. Limited only by the length of the string length of the available memory.

http://c.biancheng.net/cpp/html/137.html

Guess you like

Origin www.cnblogs.com/xyb617/p/10984658.html
Recommended