Help deconsturct error. Proceedural program outputs correctly / Functional version output is erroneous / unexpected

My gut tells me I am doing something wrong here, but I am stumped.

This program runs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw

int const MAXINDEX = 50;

using namespace std;

int main(int argc, const char *argv[])
{
    double alpha[MAXINDEX];


    // Giving lower half of array values
    for (int i = 0; i < 24; i++) {
        alpha[i] = sqrt(i);
    }

    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }

    int largestIndex = 0;
    for (int i = 0; i < MAXINDEX; i++) {
        if (alpha[largestIndex] < alpha[i]) {
            largestIndex = i;
        }
    }

    cout << "Largest index in alpha is: " << alpha[largestIndex] << " at postion " << largestIndex << endl;

    return 0;
}

output is "correct" or as expected
 
Largest index in alpha is: 147 at postion 49


but.
When this program runs (moved last code block as a function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw

int const MAXINDEX = 50;

using namespace std;

void largestIndex(const double alpha[]);

int main(int argc, const char *argv[])
{
    double alpha[MAXINDEX];


    // Giving lower half of array values
    for (int i = 0; i < 24; i++) {
        alpha[i] = sqrt(i);
    }

    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }

    largestIndex(&alpha[MAXINDEX]);

    return 0;
}
void largestIndex(const double alpha[]){
    int largestIndex = 0;
    for (int i = 0; i < MAXINDEX; i++) {
        if (alpha[largestIndex] < alpha[i]) {
            largestIndex = i;
        }
    }

    cout << "Largest index in alpha is: " << alpha[largestIndex] << " at postion " << largestIndex << endl;
}


output erroneous / unexpected

 
Largest index in alpha is: 3.06837e+257 at postion 48


Can someone help me deconstruct what is going wrong here?
I Think the issue is with the de-reference? As arrays can only be passed by reference. I am having trouble compiling the program unless I add the "&" to
1
2
    largestIndex(&alpha[MAXINDEX]);



Another layer to my confusion is
I just recently wrote this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 #include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw

int const MAXINDEX = 50;

using namespace std;

int smallestIndex(const double array[]);

int main(int argc, const char *argv[])
{
    double alpha[MAXINDEX];


    // Giving lower half of array values
    for (int i = 0; i < 24; i++) {
        alpha[i] = sqrt(i);
    }

    // Giving upper half of array values
    for (int i = 25; i < MAXINDEX; i++) {
        alpha[i] = i * 3;
    }

    // Print Table
    cout << setprecision(7);
    int k = 1;
    for (int j = 0; j < MAXINDEX; j++, k++) {
        cout << setw(8) << alpha[j] << " ";
        if (k == 10) {
            cout << endl;
            k = 0;
        }
    }


    int smindx = smallestIndex(&alpha[MAXINDEX]);

    cout << "Smallest index in alpha is: " << alpha[smindx] << " at postion " << smindx << endl;

    return 0;
}

// Smallest index
int smallestIndex(const double alpha[MAXINDEX])
{
    int index, smindx = 0;
    for (index = 0; index < MAXINDEX; index++) {
        if (alpha[index] < alpha[smindx]) {
            smindx = index;
        }
    }
    
    return smindx;
    
}



And the above program does not seem to have an issue with the de-reference.
So I am confused and want to understand my error.

The more "functional" version. e.g one function.
Last edited on
You should be passing the pointer to an array and not a specific value. alpha[MAXINDEX] is not a pointer but an out-of-bounds access to alpha.
When you pass the dereference, you are sending a pointer to an array that starts just outside of alpha (and the array doesn't exist).
 
largestIndex(alpha);
Last edited on
In the function version, you are passing the address of past the end of the array to the function, so it reads random memory you don't own. Just pass the array name, or otherwise &alpha[0] if you must.
1
2
3
4
5
6
7
8
9
    // declaration of alpha as an array of MAXINDEX elements:
   double alpha[MAXINDEX];

    // ...

    // feeding largestIndex the address of element MAXINDEX, which does not exist.
    largestIndex(&alpha[MAXINDEX]);
    // remember that valid indices into an array are 0 to size-1
    // should be:  largestIndex(alpha) ; 

Last edited on
Thank you everyone for your input!! Really helped.
Topic archived. No new replies allowed.