It is hard to understand conceptually what is happening when we create a string with and without malloc() allocation.
That is what we will see in this tutorial.
Let’s see some examples of allocation memory, with and without malloc(), and let’s see what is happening in the memory:
#include <stdio.h> #include <stdlib.h> int main() { char *str1; char *str2; char *str3; char *str4; char *str5; str1 = "hell"; str2 = "hello"; str3 = "hello"; str4 = "hello"; str5 = malloc(sizeof(*str2) * 5 + 1); str5[0] = 'h'; str5[1] = 'e'; str5[2] = 'l'; str5[3] = 'l'; str5[4] = 'o'; str5[5] = '\0'; printf("str1 | %c - %c - %c - %c\n", str1[0], str1[1], str1[2], str1[3]); printf("str1 | %p - %p - %p - %p\n\n", &str1[0], &str1[1], &str1[2], &str1[3]); printf("str2 | %c - %c - %c - %c - %c\n", str2[0], str2[1], str2[2],str2[3], str2[4]); printf("str2 | %p - %p - %p - %p - %p\n\n", &str2[0], &str2[1], &str2[2], &str2[3], &str2[4]); printf("str3 | %c - %c - %c - %c - %c\n", str3[0], str3[1], str3[2],str3[3], str3[4]); printf("str3 | %p - %p - %p - %p - %p\n\n", &str3[0], &str3[1], &str3[2], &str3[3], &str3[4]); printf("str4 | %c - %c - %c - %c - %c\n", str4[0], str4[1], str4[2],str4[3], str4[4]); printf("str4 | %p - %p - %p - %p - %p\n\n", &str4[0], &str4[1], &str4[2], &str4[3], &str4[4]); printf("str5 | %c - %c - %c - %c - %c\n", str5[0], str5[1], str5[2],str5[3], str5[4]); printf("str5 | %p - %p - %p - %p - %p\n\n", &str5[0], &str5[1], &str5[2], &str5[3], &str5[4]); free(str5); return (0); } Let’s compile it and execute it:
...