c++ - Why in Malloc() function it allocated more byte than actual specified? -
this question has answer here:
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
Post a Comment