Home > Software engineering >  How can I access variables stored in the dynamic memory in a different function?
How can I access variables stored in the dynamic memory in a different function?

Time:08-05

So I made this little piece of code that asks the user the size of an array and the contents of the array (in order) and makes the array in the dynamic memory (heap?).

void leesgetallen() {
    int *n = new int();
    cout << " Wat is de lengte van de Array?" << endl;
    cin >> *n;
    int g;
    int *A = new int[*n];
    cout << "Geef de getallen van de Array in stijgende volgorde." << endl;
    for (int i = 0; i < *n; i  ) {
        cout << "Geef getal nummer " << i << " :";
        cin >> g;
        A[i] = g;
    }
    cout << "Ter controle, dit is de ingegeven Array: ";
    for (int *pa = A; pa != A   *n; pa  ) {
        cout << *pa << " ";
    }
}

On its own this works perfectly.

However when I then try to use this array (A) and size of said array (*n), it doesn't recognize A and *n. I don't quite understand how I can use these variables as to my understanding if they are in the dynamic memory they should be global?

The reason I need to access them is because I want to use a different function to calculate the average of an inputted array.

like so.

int gem(int a[], int n) {
    int som = 0;
    for (int *pa = a; pa != a   n; pa  ) {
        som  = *pa;
    }
    int gemiddelde = som / n;
    cout << "Het gemiddelde van deze array is: " << gemiddelde << endl;
    return gemiddelde;
}


void leesgetallen() {
    int *n = new int();
    cout << " Wat is de lengte van de Array?" << endl;
    cin >> *n;
    int g;
    int *A = new int[*n];
    cout << "Geef de getallen van de Array in stijgende volgorde." << endl;
    for (int i = 0; i < *n; i  ) {
        cout << "Geef getal nummer " << i << " :";
        cin >> g;
        A[i] = g;
    }
    cout << "Ter controle, dit is de ingegeven Array: ";
    for (int *pa = A; pa != A   *n; pa  ) {
        cout << *pa << " ";
    }
}


int main() {
    leesgetallen();
    gem(A, *n);
    delete *n;
    delete[] A;
}

Can anyone help me out?

ps: all text is in dutch, but that shouldn't really matter I hope.

CodePudding user response:

new int[*n] creates new, global (in a sense), unnamed array, and returns pointer to its beginning. That pointer is the only way to access that array. But A, the variable you store it in, is local to leesgetallen so can only be accessed from said function. To overcome that, you can define A and n in main, for example, and pass references (or pointers) to these variables into leesgetallen. (Or, as @lorro suggested, return their values in a tuple)

Also note that n doesn’t need dynamic allocation as you know for sure it will be exactly one int for your whole program.

CodePudding user response:

Main ways of communicating something from within a function to caller / external:

  • return the variable. That's what functions are about, anyways. You might return multiple variables using either a std::tuple<>, or simply declare a struct for the return type (preferred for complex types).

  • Make it a member function and update the object it's applied to.

  • Use an output argument (e.g. an int*& A). This is usually less preferred over returning the variable; in the past, it was very common to return e.g. status code this way.

  • Update a global variable (usually static variable)

  • Call a function that processes the result. This might just update a global, or do something else. The processing function is either known in your function, or is passed to it as a template (or in some cases, function pointer / std::function<>) argument. Template argument has the benefit that you don't need to know the type in advance (e.g. int or long). It's common to implement factories this way. When it's the last call in your function, it's called continuation passing.

  • Update the environment (e.g. a file, or other externals). This is especially useful in large systems where there might be multiple instances of your code working together, but is also orders of magnitude slower (for your actual thread) than the other methods.

Note that variables won't be globally visible just because they're dynamically allocated. You still need to choose a way to have them visible for the caller (or other part of the code where you'd use them).

CodePudding user response:

Thanks guys, I found the solution. And understand a lot more about dynamic allocation and memory management.

Here is the solution for future reference.

int gem(int a[], int n) {
    int som = 0;
    for (int *pa = a; pa != a   n; pa  ) {
        som  = *pa;
    }
    int gemiddelde = som / n;
    cout << "Het gemiddelde van deze array is: " << gemiddelde << endl;
    return gemiddelde;
}


void leesgetallen(int *n, int *A) {
    cout << " Wat is de lengte van de Array?" << endl;
    cin >> *n;
    int cn = *n;
    cout << "controle: n = " << *n << endl;
    int g = 0;
    cout << "Geef de getallen van de Array in stijgende volgorde." << endl;
    for (int i = 0; i != cn; i  ) {
        cout << "Geef getal nummer " << i   1 << " :";
        cin >> g;
        A[i] = g;
    }
    cout << "Ter controle, dit is de ingegeven Array: " << endl;
    for (int *pa = A; pa != A   *n; pa  ) {
        cout << *pa << " ";
    }
    cout << endl;
}


int main() {
    int n = 0;
    int *A = new int[n];
    leesgetallen(&n, A);
    gem(A, n);
    delete[] A;
}
  • Related