This shows you the differences between two versions of the page.
— |
c:stdlib.h:malloc [2024/02/16 01:04] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{keywords>wiki library source code example reference}} | ||
+ | ====== malloc ====== | ||
+ | <code c> | ||
+ | #include <stdlib.h> | ||
+ | void *malloc(size_t size); | ||
+ | </code> | ||
+ | |||
+ | === description === | ||
+ | The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified.\\ | ||
+ | \\ | ||
+ | The order and contiguity of storage allocated by successive calls to malloc() is unspecified.\\ | ||
+ | The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer \\ | ||
+ | to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). \\ | ||
+ | Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points\\ | ||
+ | to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned.\\ | ||
+ | If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.\\ | ||
+ | |||
+ | |||
+ | === return value === | ||
+ | RETURN VALUE | ||
+ | |||
+ | Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space.\\ | ||
+ | If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned.\\ | ||
+ | Otherwise, it shall return a null pointer and set errno to indicate the error. \\ | ||
+ | |||
+ | ===== malloc c code example ===== | ||
+ | <code c> | ||
+ | /* | ||
+ | * malloc example code | ||
+ | * http://code-reference.com/c/stdlib.h/malloc | ||
+ | */ | ||
+ | #include <stdio.h> /* including standard library */ | ||
+ | //#include <windows.h> /* uncomment this for Windows */ | ||
+ | |||
+ | #include <stdlib.h> | ||
+ | |||
+ | int testit ( int *x ) | ||
+ | { | ||
+ | if (x != NULL) { | ||
+ | printf("allocation of memory was successfully.\n"); | ||
+ | } | ||
+ | else { | ||
+ | printf("allocation of memory failed.\n"); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int main ( void ) | ||
+ | { | ||
+ | int *buffer, i; | ||
+ | |||
+ | printf("how many int to malloc ?: "); | ||
+ | scanf("%i", &i); | ||
+ | |||
+ | printf("set buffer to %i * %i = %d bytes\n",i, sizeof(int), i*sizeof(int)); | ||
+ | |||
+ | buffer = (int*) malloc (i*sizeof(int)); // i * size of int... eg. 100 * 4byte | ||
+ | testit(buffer); | ||
+ | |||
+ | printf("try to free the buffer\n"); | ||
+ | free (buffer); | ||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ==== Output ==== | ||
+ | |||
+ | Output allocation successfully: | ||
+ | user@host:~$ ./malloc | ||
+ | how many int to malloc ?: 999999 | ||
+ | set buffer to 999999 * 4 = 3999996 bytes | ||
+ | allocation of memory was successfully. | ||
+ | try to free the buffer | ||
+ | |||
+ | |||
+ | Output allocation failed: | ||
+ | user@host:~$ .//malloc | ||
+ | how many int to malloc ?: 1234567890123 | ||
+ | set buffer to 2147483647 * 4 = -4 bytes | ||
+ | allocation of memory failed. | ||
+ | try to free the buffer | ||
+ | |||
+ | |||