System Calls - Memory
Memory Allocation
Memory API malloc
Description:
- Allocate a memory region on the heap.
Syntax:
void* malloc(size_t size)size_t size: the size of the memory block (in bytes)size_t: is an unsigned integer type
Return:
- Success: a void type pointer to the memory block allocated by
malloc - Fail: a null pointer
- Success: a void type pointer to the memory block allocated by
Function sizeof
Description:
Usually use
sizeinmallocinstead of typing a number.Two types of results of
sizeof1
2
3
4
5
6
7
8/* The actual size of 'x' is known at run-time */
int *x = malloc(10 * sizeof(int));
printf("%d\n", sizeof(x)) // 4 (size of a pointer)
/* The actual size of 'y' is known at compile-time */
int y[10]
printf("%d\n", sizeof(y)) // 40 (y is static)
Memory API free
Description:
- To free a memory region allocated by a call to
malloc.
- To free a memory region allocated by a call to
Syntax:
void free(void *ptr)void *ptris a pointer to a memory block allocated withmalloc
Return
none
Memory API calloc
Description:
- Allocate memory on the heap and set with zero before returning.
Syntax:
void* calloc(size_t num, size_t size)size_t num: number of block to allocatesize_t size: size of each block (in bytes)
Return:
- Success: a void type pointer to memory block allocated by
calloc - Fail: a null pointer
- Success: a void type pointer to memory block allocated by
Memory API realloc
Description:
- Change the size of memory block.
Syntax:
void* realloc(void *ptr, size_t size)void *ptr: pointer to memory block allocated withmalloc,callocorreallocsize_t size: new size for the memory block (in bytes)
Return:
- Success: a void type pointer to memory block
- Fail: a null pointer
Common Error
Forget Memory Allocation
Description:
- created a pointer but forgot to allocate the memory
- if operating (e.g.
strcpy) that pointer, then it reports an error
Incorrect Code (Error):
1
2
3
4/* Incorrect Code */
char *src = "hello"; // character string constant
char *dst; // unallocated
strcpy(dst, src); // segfault and die
Correct Code:
1
2
3char *src = "hello";
char *dst = malloc(strlen(src) + 1); // allocated
strcpy(dst, src);
Insufficient Memory Allocation
Description:
- do not allocate the pointer with enough memory
- it works properly
Incorrect Code (but work properly):
1
2
3char *src = "hello";
char *dst = malloc(strlen(src)); // too small
strcpy(dst, src);
Forget Initialization
Description:
- use an uninitialized the pointer
- it will allocate with the value used before
Incorrect Code:
1
2int *x = (int *)malloc(sizeof(int)); // allocated
printf("*x = %d\n", *x); // uninitialized memory access
Memory Leak
- Description:
- do not
freethe used memory access - the program will run out of memory

- do not
Dangling Pointer
- Description:
- freeing the memory before it is finished using
- the program accesses to that memory with invalid pointer

Double Free
- Description:
- Free the memory that was freed before
- it reports an undefined error

All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


