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.

Recommended Answers

All 4 Replies

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.

@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?

>> 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)

Ok i will go through the link. Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.