Malloc in C, for int * and char *

zihan
4 min readApr 24, 2021

--

Malloc, aka m-emory alloc-ation, allows you to provide memory for your variables when their size is unknown upfront.

Let’s imagine you are asking your user to insert their name in a video game you coded. There is no way to know upfront how long their name will be. In such cases, malloc comes and help us.

Int and char have by default a fixed size of memory, 4 bits for an int and 8 for a char.
When you need an array of ints or chars, you need to allocate memory for them in a row, and you need to tell your program exactly how many slots of ints or char in a row you need.

Dynamic memory allocation with malloc

CASE 1. Malloc with int *
int *array;
array = (int *)malloc(sizeof(int) * size)

In C, arrays of int are int *. We fill the array with ints, for a length of 5.

(int *)malloc → I want to malloc to create an array of ints
sizeof(int) → I want to create slots of ints, each slot must have the size of a int, i.e. 4 bits
* size → I want to create n slots of ints (how many slots I want)

CASE 2. Malloc with char *
char *mystring;
mystring = (char *)malloc(sizeof(char) * (size + 1))

(char *)malloc → I want to malloc to create an array of chars
sizeof(char) → I want to create slots of chars, each slot must have the size of a char, i.e. 8 bits
* (size + 1) → I want to create size slots of chars + 1 slot for the null-terminator (\0) — (how many slots I want, plus 1 for the \0)

CASE 3. Malloc with 2d arrays of ints (aka matrix, aka int **)
In this case we have a matrix (you can think of it as a calendar for example).
We need to allocate first memory vertically, to know how many rows we need.

int **calendar;
calendar = (int **)malloc(sizeof(int *) * 4) #number of weeks

First, we tell the program how many rows we want our matrix to have.
Each row is an array of ints (aka int *, see Case 1).
Then, we can allocate memory horizontally for each of these rows. To do that, we can simply use our case 1. So for the first week we would do:
calendar[0] = (int *)malloc(sizeof(int) * 7) # number of days

CASE 4. Malloc with 2d arrays of chars (aka char **)
In this case we have a char matrix (such as a file or a list of names for example). We first allocate memory vertically, to know how many rows we need.

char **file;
file = (char **)malloc(sizeof(char *) * (size + 1))

First, we tell the program how many lines we want our file to have.
Each line is a string (char *, see Case 2).
After we allocate memories for the number of lines we want AND we add the NULL-terminator at the end (very important, don’t forget!), we can allocate memory horizontally for each of these lines.
To do that, we use our case 2. For example for the first line we will do:
file[0] = (char *)malloc(sizeof(char) * n + 1) # n is the number of char our line has, or in other terms, how long our line is

Static memory allocation without malloc

For int *, or array of ints, you have:
int array[5] = { 1, 2, 3, 4, 5 };

→ in memory you will have a continuum space of 5 slots next to each other, each filled with a value.
Something like: [1][2][3][4][5]
array[0] equals [1]; array[1] equals [2]; etc…

For char *, or strings, you have:
char string[5] = “hello”;

→ in memory it looks like a continuous space of six slots next to each other, each filled with a value.
Something like: [h][e][l][l][o][\0] ← this is an array of chars, null-terminated with an \0.
string[0] equals ‘h’; string[1] equals ‘e’; etc…
SIDE NOTE: In C, a string is by definition an array of char null-terminated (which means putting a \0 at the end). This means, that you will need enough chars to write your word, plus 1 char to null-terminate your string .

With the brackets notation, the size won’t be modifiable later on.
And it doesn’t work when you don’t know the exact size upfront.

Last but not least.
Malloc is a function of type void *, which means it can be casted to whatever type we need (int *, char *, int **, char **). It will type-cast to the right type by default, so there is actually no need to type-cast our malloc upfront, and it’s usually not encouraged to [link] . In fact,

char *mystring = (char *)malloc(sizeof(char) * (length + 1))

is exactly equivalent to

char *mystring = malloc(sizeof(char) * (length + 1))

Happy coding!

--

--

No responses yet