-3

One of my friend, yesterday sent me this code in C language:

int main()
{
    int n = 3 ;
    int a[n];
    for (int i = 0; i <= 5; ++i)
    {
        a[i] = i;
        printf("%d ", a[i]);
    }
    return 0;
}

When I saw this first I thought there will be error of index out of bound since inside a for loop we have a[3] = 3 and same for index 4,5 and we are allocating memory for 3 blocks of memory only but then he sent me the output :

0 1 2 3 4 5

And both of us don't have any idea why is this happening , can somebody help ?

PATHIK GHUGARE
  • 137
  • 1
  • 9
  • "we are allocating memory for 3 blocks of memory only" Why do you think this to be the case? – Philip Kendall Sep 15 '20 at 04:51
  • Even if there was an out of bounds access in that code, the compiler would not tell you. Also the program might run as if it was correct. Accessing illegal memory is causing undefined behaviour. It can show up or not. It can crash every time or only once in 100 runs or not at all. – Gerhardh Sep 15 '20 at 04:57
  • Guys sorry I wanted to write n=3 but I mistakenly wrote n = 10 – PATHIK GHUGARE Sep 15 '20 at 05:00
  • C doesn't enforce the rules. You, as the programmer, are expected to follow the rules. The rule here is simple: don't read or write past the end of an array. – user3386109 Sep 15 '20 at 05:21
  • 1
    Yes exactly. Changing 10 to 3 has substantially changed the question from containing working, clean code to one with undefined behaviour. If you check the answer then you will see that you got two sets of different answers. The first states "Clean code no problem." (and was answered before your change) the other "UB." (answered after your change). Changing two characters in a program code can make a huge difference, including the effect on a question here. That is precisely what I meant and is in direct full conflict to "I haven't changed my question". – Yunnosch Sep 15 '20 at 06:09
  • 1
    Changing a question, even substantially, (after learning that it is unclear or does not cover what you actually want to ask about) is OK, even appreciated, for the sake of interesting Q/A pairs. But once you got answers you should not change so that the existing answers are not applicable anymore. I checked and I cannot see how all answers are applicable. Neither to the initial version of your question, nor the current version. You have now put the people who answered early at risk of being downvoted for being "wrong", by users who do not see the history of your question. – Yunnosch Sep 15 '20 at 06:14

4 Answers4

3

In C there is no automatic range check. C just let you access what ever you say you want to access.

So even though your array only reserves 3 ints, C still let you access a[3], a[4] or even a[100]. No run-time checks, no compile-time checks. There are several "static code analyzers" available that can (help) check your code for such problems but C compilers doesn't have to (and normally won't).

When you access out-of-bounds it is called UB, i.e. undefined behavior. In that case anything may happen. Maybe the program will crash. Maybe it will just do what you expect. Maybe it will do something "wierd" like printing strange output but otherwise continue to run. We can't tell...

So it's all your responsebility to avoid out-of-bounds access. No help from C...

notice: While C doesn't include run-time checks for out-of-bounds access, there may be other things on your system that (in some situations) can/will detect illegal memory access. But that is a system feature - not a C feature.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Yeah but still which memory I am accessing for a[3], a[4], a[5] ? – PATHIK GHUGARE Sep 15 '20 at 05:03
  • 2
    @PATHIKGHUGARE On most systems there is the concept of a stack. That's a pre-allocated memory area that belongs to your program. Your array is (most likely) placed on the stack. So when you access the array out-of-bounds you (most likely) just access a part of the stack. However, this is implementation dependant... – Support Ukraine Sep 15 '20 at 05:10
  • Have a look at this https://stackoverflow.com/questions/14588767/where-in-memory-are-my-variables-stored-in-c . This is sure to help you. – Mohit Sharma Sep 15 '20 at 08:55
2

we are allocating memory for 3 blocks of memory

No you have 10 blocks of memory,

int n = 10;
int a[n];

Valid indexing range is 0...9.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
1

The size of the array is 10: the program is correct. Can you be more precise ?

Raphael Mansuy
  • 213
  • 1
  • 4
  • 10
  • Sorry actually I wanted to write n = 3 not n = 10 and I am not able to edit this now :( – PATHIK GHUGARE Sep 15 '20 at 04:58
  • The clarification question at the end of this answer belongs into a comment instead. You can comment on your own post. Also, if you are aware that the question is probably not phrased exactly as intended, then it would probably be wise to answer elsewhere. You almost knowingly put yourself at risk of being hurt by this moving target question. Compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – Yunnosch Sep 15 '20 at 06:17
1

a[n] is actually a pointer to the first address of the array. There is no range check in C. What your program is doing is accessing memory not assigned to it initially. When doing that you program may or may not work as intended.

In this case, the program is accessing the memory not assigned to the array but assigned to the program(but not being used), that's why not throwing any error.

Mohit Sharma
  • 338
  • 2
  • 13