Malloc() — Something Hidden

Abhay desai
3 min readDec 25, 2021

--

Something Suspicious of Malloc()

What is Malloc()..?

Memory allocation (malloc), is an in-built function in C. This function is used to assign a specified amount of memory for an array to be created. It also returns a pointer to the space allocated in memory using this function.

Syntax

ptr = (cast_type *) malloc (byte_size);

Here ,

  • ptr is a pointer of cast_type.
  • The malloc function returns a pointer to the allocated memory of byte_size.

In Real Time,

When we have multiple input or Output statements , where we had considered malloc to store an specific amount of data , But we found that malloc is storing more data as per the given data

Do you have ever tried that whether we are able to store more data than the allocated space by malloc without giving error ?

Technically it should not work and it should give error,

Bu the malloc function gives more space than the allocated space

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int i;
char *ram;
ram = (char*)malloc(15);
for (i=0;i<20;i++)
{
ram[i]='r';
printf("\n%d) %c",i+1,ram[i]);
}
return 0;
}

Output :

1) r
2) r
3) r
4) r
5) r
6) r
7) r
8) r
9) r
10) r
11) r
12) r
13) r
14) r
15) r
16) r
17) r
18) r
19) r
20) r

As you can see I gave ram 15 bytes but was able to store 20 bytes in it. What is going on?

Example 2:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
char *text;

printf("Enter limit of the text: ");
scanf("%d",&n);

/*allocate memory dynamically*/
text=(char*)malloc(n*sizeof(char));

printf("Enter text: ");
scanf(" "); /*clear input buffer*/
gets(text);

printf("Inputted text is: %s\n",text);

/*Free Memory*/
free(text);

return 0;
}

Output:

Enter limit of the text: 20
Enter text: I am abhay , and trying to give some more information.
Inputted text is: I am abhay , and trying to give some more information.

As you can see I gave given the size 20 but stored more than that

Is this the same malloc()?

Conclusion:

You allocate storage with malloc. This returns you the address of where you may store your data. However, neither C, nor malloc or the heap will check if you store only in the area given to you. If you write beyond the area allocated, you are writing in an area that is "not yours". This can lead to various types of undefined behavior.

Some memory managers will intercept your attempt and will abort your program. Other memory managers don’t do that, but since you write to an area that is not yours, you may be overwriting data from the memory manager and any next attempt to allocate memory may fail because the memory manager’s data is corrupt. Or you are overwriting memory that belongs to someone else.

So the fact that it seems you can write more than you allocated, does not mean your program is correct. In fact, it is hopelessly wrong and will lead to an error somewhere else. So your program could make for example invalid calculations and your airplane crashes… This is called undefined behavior and you as a programmer must take care you always allocate what you need and that you never go out of the bounds of what you allocated.

I Hope you find it interesting..!!

Thank You..!!

--

--