0

Hello.

Can anyone please explain why the following code is not producing any runtime error?

int a[] = {1, 0};
 printf("%d", a[-1]);

PS:im using VC++ 2008.

Thanks.

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by myk45
0

Apparently you are accessing memory to which you have read-access. Generally, there are no built-in safety nets with regards to e.g. reading/writing out of array bounds and other things alike - you just have to be careful.

0

@mitrmkar
>Apparently you are accessing memory to which you have read-access

Thanks for the reply. Well, does'nt dereferencing unallocated memory lead to memory access violation(which is what im doing).

Also i added this line:

a[-1] = 10;

which gave the following error:

Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted

What exactly does the above error mean?

0

>> Well, does'nt dereferencing unallocated memory lead to memory access violation

Not necessarily. Simply don't expect anything, that would indicate that something is seriously wrong, to happen.

>> Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
>> What exactly does the above error mean?

When you build a Debug configuration with /RTCs option on, the compiler inserts code that tries to detect stack corruption - this time you did an out-of-bounds write, which got detected.

See RTC (Run-Time Error Checks)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.