1
int *A [10];
A[2][3]=15;

This statement gives no compile time error, yet when I run the program it gives a runtime error. Why?

Edit: I specifically want to know why there is no compile time error?

1
  • 2
    Why should this be a runtime error? Where's the logic to do the kind of checking that would create an error? Commented Jan 3, 2017 at 9:43

3 Answers 3

2

This statement gives no compile time error in c

Because there is no syntactical error or constraint violation that compiler should be judging. C does not do any bound-checking for arrays (or pointer arithmetics).

You'll be perfectly allowed to write a code which makes use of invalid memory (example: dereference an invalid memory location) but in case, the compiler has produced a binary for such code, running the binary would invoke undefined behaviour.

yet when I run the program, it gives a runtime error

In your code,

  int *A [10];

A is an array of 10 int *s, and they are not initialized explicitly. Looking from the snippet, it appears A is not in global scope, i.e., not having static storage, so the contents of each of those pointers are indeterminate.

So, later in process of writing A[2][3]=15;, you're trying to access A[2] (a pointer), which points to an invalid memory. This invokes undefined behavior.

Sign up to request clarification or add additional context in comments.

7 Comments

A[2] is a valid element of the array A... The wording in your last sentence can be better
@StoryTeller right, but it does not point to anything valid. Please suggest or feel free to edit in, no worries. :)
@StoryTeller Thanks, I've also added a little more. :)
Sourav, to be pedantic, A[2] is likely, if uninitialised to be pointing to invalid memory, but it may not. The values for each pointer may well, depending what was on the stack at "instantiation" (during run time), may well have (and just by fluke) have a value that is within the process' memory space. This of-course is much more dangerous as you will end up pulling the ground from beneath the process elsewhere.
@cdcdcd it may, it may not, it's not guaranteed, the one thing guaranteed is indeterminate and attempt to use indeterminate "value" leads to UB, right?
|
1

Because accessing the uninitialized pointer A[2] invokes undefined behavior, which means anything is allowed to happen as far as the C standard is concerned.

why there is no compile time error

Because the standard doesn't require a diagnostic (such as a compile error) to be issued in this case.

1 Comment

I'd argue that better wording would be "uninitialized pointer at A[2]". But it's just me.
0

It's for the same reason that

int a, d = 0;

a = 1 / d;

compiles cleanly. To detect the invalid operation the compiler would need to run the program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.