Malloc, meaning Memory Allocation

Week 4 - Memory, Malloc, Pointers, More Overflows

Created: 2022-06-20
Tags: #fleeting


Abstract:

  • What does malloc returns
  • Typecast the returned void pointer of malloc
  • Explanation about how free() works
Code Snippets

Libraries Needed

`#include <stdlib.h>

Purpose of Malloc:

  • malloc asks for how many bytes to be allocated
  • create a single large block of continguous memory according to size specified.
  • The whole idea of it is to allocate memory during run time.

What does malloc returns

malloc()

  • returns the address of the empty spaced memory.
  • Specifically, returns a void pointer pointing to the first byte of the allocated memory.
  • returns NULL when there's no longer empty spaced memory.

Typecast the returned void pointer of malloc

Malloc doesn't have an idea of what it is pointing to nor the data type it's gonna store it to. Therefore it's important to assign a data type by typecasting it. We can write (int *) in front of malloc()

person *myperson = (person *) malloc(sizeof(person));
// person here is name of the struct

sizeof values in each data type of C
malloc(sizeof(int))
Basically sizeof(int) is saying I have 1 int. Since I want three integers, give me three pieces of int -> 3 * sizeof(int). (1)