I have allocated 5 int space using new and trying to access just beyond that allocated boundary (array[size] = 2; ). Ideally it should throw some error but it's not.
int size = 5;
int* array = new int[size];
array[1] = 2;
//If I uncomment below line it gives me error
//array[size] = 2;
cout << array[1] << endl;
// Why this is not an problem
cout << array[size] << endl;
delete[] array;
But when i am trying to delete this allocated space it shows me this error.
*** glibc detected *** /home/workspace/PracticeC++/Debug/PracticeC++: free(): invalid next size (fast): 0x08add008 ***
PracticeC++: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
when i tried delete after commenting array[size] = 2; line it worked fine. I am not able to understand this strange behaviour. As per my understanding delete[] operator has information about size of array so it should not go beyond that size and this error is related to situation when we try to delete same memory twice or that is not allocated by us. Can someone tell me the reason behind this error.