Identifying this logic error..

Sep 13, 2015 at 12:58am
The code below does not print out the arrays correctly.

Does it have to do something with the allocation of these values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int arr1[10];
int arr2[10];
int arr3[10];
int i;

for (i = 0; i < 10; i++)
{
   arr1[i] = 100 + i;
   arr3[i] = 300 + i;
}

for (i = 0; i < 15; i++)
{
   arr2[i] = 200 + i;
}
Sep 13, 2015 at 1:17am
You assign arr2 10 places but try and insert 15 items?
Sep 13, 2015 at 1:35am
Right, but why does this error happen?
Sep 13, 2015 at 1:56am
I dont get a compiler error when I run this.
Sep 13, 2015 at 1:57am
I said Logic Error. Not a compiling error.

A logic error produces unintended or undesired output or other behaviour, although it may not immediately be recognised as such.
Last edited on Sep 13, 2015 at 1:57am
Sep 13, 2015 at 2:27am
> why does this error happen?

Attempted out of range access to the array engenders undefined behaviour.

Undefined behaviour:
Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. - C FAQ
Topic archived. No new replies allowed.