c++ - Why in Malloc() function it allocated more byte than actual specified? -


i allocates memory using malloc(1) i.e allocated 1 byte accept number more of 1 byte. here in code accept 1000 number , not possible store 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 happen?

it's undefined behavior because c , c++ doesn't boundary checking regards arrays.


Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -