C - Character manipulation - Allocation memory

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:

$ cc main.c && a.out

Result:

str1 | h - e - l - l
str1 | 0x8048884 - 0x8048885 - 0x8048886 - 0x8048887

str2 | h - e - l - l - o
str2 | 0x8048889 - 0x804888a - 0x804888b - 0x804888c - 0x804888d

str3 | h - e - l - l - o
str3 | 0x8048889 - 0x804888a - 0x804888b - 0x804888c - 0x804888d

str4 | h - e - l - l - o
str4 | 0x8048889 - 0x804888a - 0x804888b - 0x804888c - 0x804888d

str5 | h - e - l - l - o
str5 | 0x829f008 - 0x829f009 - 0x829f00a - 0x829f00b - 0x829f00c

And this is not what we could expect.
Indeed, when we created the first string, named str1, we just wrote hell. In the memory we have reserved the block 0x8048884 up to 0x8048887.

And when we created the str2, str3 and str4, we have reserved the block 0x8048889 up to 0x804888d memory.
It means that the 0x8048888 is reserved for the '\0' and this is why we jump from the 0x8048887 to 0x8048889.

So there is a real difference between these two states.
We see clearly that when the string is the same as the others, the memory allocated is strictly the same!

In the last example, we created a string with a malloc and allocated each char separately, otherwise the malloc will not be freed.
And the memory are different from str2, str3 and str4 even if the letters are the same.

Very interesting, isn't it?

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.