#include <stdlib.h>
void * malloc(size_t size);
size_t size
: the argument specify how many bytes to allocate( can employ sizeof
function to specify the memory storage)Shallow copy:
Shallow copy just copy a pointer to the same location. The two objects share the same memory location, which means once we have changed or freed the corresponding memory locations both two objects will change.
Example:
polygon_t * p2 = p1;
or
1
2
polygon_t * p2 = malloc(sizeof(*p2));
*p2 = *p;
Deep copy:
Deep copy will duplicate the whole objects in different memory locations, which means we can freely change the previous memory location.
calloc
void * calloc (size_t num_elts, size_t elt_size);
void free(void *ptr);
void *ptr
: starting address of the memory that needs to be freed, which should match with the return value malloc.void * realloc(void *ptr, size_t size);
Tip:
ptr = realloc (ptr, new_size)
if realloc fails, the address of old block is gone!
Use a temporary variable when calling realloc.
1
2
3
4
5
int32_t * temp;
temp = ralloc (ptr, new_size);
if (NULL != new_copy){
ptr = new_copy;
}
detail:<[getline | Coursera](https://www.coursera.org/learn/interacting-system-managing-memory/supplement/4Fwiv/getline)> |
void *sbrk (intptr_t increment);
intptr_t increment
: type: an integer large enough to hold a pointer, and specify the increment of break$$Sum = N*\sum_{i=1}{\frac{1}{2^i}}$$