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

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -