after delete can still acess the memory !!


tryig to understabd usage of new/delete with/without array,
few doubts (provided as comments) , request to help make me clear,


int * arr = new int;

arr[0] = 21;
arr[2] = 21;
arr[344] = 521; /*santosh: was expecting segmentation fault!! why did not generate?*/

delete [] arr; /*santosh: inspite having pointer to single int, [] operator shouldnt it throw error? */

cout<<arr[2]<<endl;/*still accessable after freeing !!!*/


Last edited on
You created a dynamic array thus arr[244] will not produce a segmentation fault because the array doesn't have a fixed size.

When i tried to run your program, the delete[] operator broke because you are using a dynamic array with unknown size.
Last edited on
@ GodPyro: It looks to me that the OP is asking a question about 'C' though, notice that main() actually has no type. So I wonder if the OP is

A.) using some compiler so out of date that these things do not matter to it.


B.) Trying to do some C++ stuff in C.

There are few actual differences between C and C++ when you are starting out. Unfortunatly syntax like this is one of the big ones.
Topic archived. No new replies allowed.