I allocates memory using malloc(1) i.e it allocated one byte but it accept number more than of 1 byte. Here in this code it accept 1000 number and it is not possible to store this number in 1 byte.
code:
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main()
{
int *a;
a=(int *)malloc(1);
for(int i=0;i<1000;i++)
{
a[i]=i;
}
for(int i=0;i<1000;i++)
{
cout<<""<<a[i];
}
}
why this happen?
malloc10000000instead of1000.