Added declaration of malloc (#31439)

* Added declaration of malloc

* Updated synopsis of malloc function
This commit is contained in:
Jacob Kim
2019-04-02 08:19:55 +09:00
committed by Christopher McCormack
parent 3357a25629
commit 7a644820d0

View File

@ -6,8 +6,15 @@ malloc() is a library function that allows C to allocate memory dynamically from
malloc() is part of stdlib.h and to be able to use it you need to use `#include <stdlib.h>`.
## Synopsis
```
void *malloc(size_t size)
void *free(void *ptr);
```
## Using Malloc
malloc() allocates memory of a requested size and returns a pointer to the beginning of the allocated block. To hold this returned pointer, we must create a variable.
Here we'll make a pointer to a soon-to-be array of ints
```C
int* arrayPtr;
@ -30,7 +37,6 @@ This statement will deallocate the memory previously allocated. C does not come
* Allocating memory allows objects to exist beyond the scope of the current block.
* C passes by value instead of reference. Using malloc to assign memory, and then pass the pointer to another function, is more efficient than having the function recreate the structure.
Difference between malloc() and calloc()
1. Malloc() does not assignes the created memory to zero where as calloc() assignes the memory created to zero.
2. malloc() is best suitable for Character data and calloc() for Numeric data.