Arrays are killing me... Please help!

Can anybody please explain to me why this is working?

Using DevC++ IDE with TDM-GCC 4.9.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int main()
{
    double array[10];
    
    for (int x = 0; x < 11; x++)
    {
        array[x] = x;
    }
    
    for (int x = 0; x <= 10; x++)
    {
        cout << "array[" << x << "]= " << array[x] << endl;
    }
    
}


This program provides the following output.
array[0]= 0
array[1]= 1
array[2]= 2
array[3]= 3
array[4]= 4
array[5]= 5
array[6]= 6
array[7]= 7
array[8]= 8
array[9]= 9
array[10]= 10

Shouldn't double array[10] create an array of 0-9?

Thanks for the help.
Shouldn't double array[10] create an array of 0-9?

You are correct.

Going out of bounds on an array is undefined behavior, it just happens to work in this case. Next time it might destroy the universe so keep an eye out.
This is more what I expect by "undefined behavior". The following is what happens when I try to read array[11]. The read on array[10] doesn't seem to be undefined behavior.

array[0]= 0
array[1]= 1
array[2]= 2
array[3]= 3
array[4]= 4
array[5]= 5
array[6]= 6
array[7]= 7
array[8]= 8
array[9]= 9
array[10]= 10
array[11]= 6.95207e-308
In C++, it is recommended to use std::vector instead of c-style arrays. Such approach is easier and has many benefits, as std::vector supports resizing and knows its own size.

Also, many implementations of standard library support bounds-checking for vectors. They would catch the error when the program is run. Visual Studio will do it in debug mode, and gcc will do it when you #define _GLIBCXX_DEBUG before including the libraries.

But, you have to use std::vector. C-style arrays are evil.
Last edited on
In this case array[10] is just as undefined as array[11]. Undefined behavior doesn't mean it will break, it means it "could" break and you can't tell when.

You put the value 10 at the memory location array[10]. You didn't reserve the memory but there was nothing important there so it just put the number there anyway. You didn't write anything to array[11] so it is just showing you whatever value happens to be at that location.

C-style arrays are evil.
Very true, use std::vector or std::array whenever possible.
closed account (E0p9LyTq)
Can anybody please explain to me why this is working?

It is "working" because C++ allows you to write and read outside the boundary of an array. Your program simply is writing to non-valid memory locations that are not critical to the program's or your operating system's operation.

array[10] is the 11th element, not the 10th.

One simple change to your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   double array[10];

   for (int x = 0; x < 11; x++)
   {
      array[x] = x;
   }

   double gateKeeper = 9999.0; // notice the change!!!!!!
   
   for (int x = 0; x <= 10; x++)
   {
      std::cout << "array[" << x << "]= " << array[x] << std::endl;
   }

}

Gives the following output:
array[0]= 0
array[1]= 1
array[2]= 2
array[3]= 3
array[4]= 4
array[5]= 5
array[6]= 6
array[7]= 7
array[8]= 8
array[9]= 9
array[10]= 9999
Thank you guys! I appreciate the assistance!
Read the comments to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int main()
{
    double array[10]; // array storing the values with starting array[0]
    
    for (int x = 0; x < 11; x++) // values is also stored in array[10].
    {
        array[x] = x;
    }
    
    for (int x = 0; x <= 10; x++) // the value of array [10] is also printing.
    {
        cout << "array[" << x << "]= " << array[x] << endl;
    }
    
}


now see below program...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
#include <iostream>
	int main()
{
double array[13]; // declare array till array[13]
    
    for (int x = 0; x <= 15; x++) // insert values till array[15]
    {
        array[x] = x;
    }
    
    for (int x = 0; x <= 18; x++) // print values till array[18]
    {
        cout << "array[" << x << "]= " << array[x] << endl;
    }
    
}

It is storing the values from array[0] to array [13], 
so we didn't consider that 13 is length of array or numbers of elements are 13.
 

OUTPUT
array[0]= 0
array[1]= 1
array[2]= 2
array[3]= 3
array[4]= 4
array[5]= 5
array[6]= 6
array[7]= 7
array[8]= 8
array[9]= 9
array[10]= 10
array[11]= 11
array[12]= 12
array[13]= 13
array[14]= 8.69169e-311
array[15]= 2.12313e-314
array[16]= 8.91255e-313
array[17]= 1.78872e-307
array[18]= 8.91255e-313
Topic archived. No new replies allowed.