suppose i declare an array of size 10(a[10]) and i try to use even 11th or 12th element...that is i try to put some data...and later extract from a[10] or a[11] ..is it possible..ans with respect to both c and c++..

It sure is possible, since C/C++ offers no built-in bounds checking. The 11th and 12th elements will refer to the memory location of the array (a) + 10 and + 11 respectively. What is in those memory locations would be difficult to say, and would be implementation specific. You could possibly be reading/writing values from other variables/arrays, unallocated space in the heap, or the OS could terminate the program if it goes beyond the programs allocated memory.

Basically, it's undefined and you shouldn't do it. You can't be sure if the data will be the same when you go to access it, and it could mess up other values or cause the program to terminate.

Edited 3 Years Ago by nmaillet

More than likely, your program will terminate immediately. Sometimes it will keep going, overwriting memory that you're not supposed to will almost certainly cause your program to malfunction somewhere later. My advice is that you're not supposed to do it.

thanxx..:D

also will the answer be same even i declare the array dynamically??

also will the answer be same even i declare the array dynamically??

The results will typically be worse when accessing dynamically allocated memory out of bounds.

also will the answer be same even i declare the array dynamically??

That just means you don't know at compile time what size it will be, so the memory for it gets allocated during running. Once it's allocated, it's just an array like any other. There's no magic that would make it expand or shrink without you having to do it yourself.

Edited 3 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.