Why do I get memory violations from passing an array by reference to a function to input data using pointers?

Hello,

I have written a function to accept user input of elements for a size of array, defined by user input. When I pass the address of the newly created array to the function and input numbers, I receive the following

"Exception thrown at 0x79845482 (msvcp140d.dll) in 5.11_Exercises_5.4.exe: 0xC0000005: Access violation writing "

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
#include <iostream>
#include <cmath>

using namespace std; 

//double DataMinusMean(double*[], double*);
void DataEntry(int, double* []);
void DisplayData(double* [], int);

/*Function to calculate the sum of x - x_mean
double DataMinusMean(double* x[], double* x_mean)
{

}
*/
//Function for data entry
void DataEntry(int x, double* DataArray[])
{
    for (int i = 0; i < x; i++)
    {
        cout << "What number would you like to enter? " << endl;
        cin >> *DataArray[i];
    }
    cout << "Data Entry Complete" << endl;
}

void DisplayData(double* DataArray[], int j)
{
    for (int i = 0; i <= j; i++)
    {
        cout << *DataArray[i] << endl;
    }
}

int main()
{
    int n = 0;

    cout << "How many numbers in your data set?" << endl;
    cin >> n;
    cout << "The total of numbers in your data set is " << n << endl;
    double* x = new double[n];
    
    DataEntry(n, &x);
    DisplayData(&x, n);

    return 0;

}
Don't pass &x. Just pass x. x is already a pointer.
Change your functions to be:

void DataEntry(int x, double DataArray[]); void DisplayData(double DataArray[], int j);

and just do DataArray[i] not *DataArray[i]
Hello Shishykish,

To try to expand on what Ganado said.

In your function calls DataEntry(n, &x);. The (&) here is not taken as a reference, but as meaning the address of.

So when you use the address of "x" in the function you are writing to memory outside the boundary of the array.

Another point. In the function definition when you say: void DataEntry(int x, double DataArray[]);. this degrades to a pointer, so you can say either:
1
2
3
4
5
void DataEntry(int x, double DataArray[]);

Or

void DataEntry(int x, double* DataArray)

And inside the function, as Ganado said, use DataArray[i] to access the array. The "[i]" part will increment the array the proper amount from the address sent to the function.

If I have something wrong due to misinterpretation some one let me know.

Andy
Hello,

It worked absolutely great. Am I correct to say that just the address of the variable is passed to the function, not the variable itself?
When passing an array to a function the array decays to a pointer to the first element of the array:
https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
While that is true, x was already a pointer before being passed to a function, in this particular case.

Am I correct to say that just the address of the variable is passed to the function, not the variable itself?
In your original code, you were passing &x, which is the address of x.

In my/Handy Andy's suggestion, you are passing x itself, not &x.
So you are now passing the variable itself, but the variable itself is a pointer, which points to an array in dynamic memory.
Last edited on
Topic archived. No new replies allowed.