简体   繁体   中英

Arrays and Pointers as arguments and return values

#include <iostream>
using namespace std;

int getDegree()
{
    int degree;
    cout << "Enter degree of polynomial" << endl;

    cin >> degree;

    return degree;
}

int* getPoly(int degree)
{
    cout << "Enter coefficients in order of power of x. e.g. for 2 + x + 3x^2, enter 2 then 1     then 3" << endl;

    int coeff [degree +1];
    for (int i = 0; i <= degree; i++)
    {
        cin >> coeff[i];
    }

    return coeff;
}

int* polyder(int p[], int degree)
{
    int dp[degree];

    for(int i = 0; i < degree; i++)
    {
        dp[i] = p[i+1] * (i+1);
    }

    return dp;
}

int main(int argc, const char * argv[])
{
    int degree = getDegree();
    int p = *getPoly(degree);
    int dp = *polyder(&p, degree);

    for(int i = 0; i < degree +1; i++)
        cout << "   " << p[i] << " x^" << i;
    cout << endl;

    for(int i = 0; i < degree +1; i++)
        cout << "   " << dp[i] << " x^" << i;
    cout << endl;

    return 0;
}

I am getting an error during the print statements. I am not worried about the math involved, just how to pass the arrays between functions/methods.

Can anyone find why this is not working? I am new to C++, used to Java.

Can anyone find why this is not working?

In C++ variables are destroyed when the scope in which they were declared ends. You return an address of a variable that doesn't exist when the function ends:

int* getPoly(int degree)
{
    int coeff [degree +1];
    // ...
    return coeff;
}

If you wish the variable still exists after the function ends, you have to allocate it using new :

int* getPoly(int degree)
{
    int *coeff = new int[degree +1];
    // ...
    return coeff;
}

And, at the end of your main function (or wherever you don't need the array anymore), call delete[] to deallocate the memory:

int *p = getPoly(degree);
delete[] p;

The Array name is essentially a pointer to the first element of the array (as you can see in the code above, I've declared p as a pointer to int ), so you pass them to other functions just by providing their name:

int* polyder(int p[], int degree){/*do something here*/}
// <-- some other code
int *p = getPoly(degree);
int* returnedArray = polyder(p,degree);

First of all, the line

int coeff [degree +1];

is not a valid C++ statement unless 'degree' is a constant, even through some compilers may allow it.

Even if some compiler allowed it, the space for coeff is allocated from the stack and will be invalid after the function returns. Hence, the line

return coeff;

returns an memory location that will be invalid at its usage.

In order to return valid memory function, replace the line

int coeff [degree +1];

by

int* coeff = new int[degree];

You don't need degree+1 items in the array.

Similar changes are needed in the function polyder .

The thing to remember about arrays in C++ is that unlike Java, they are not objects (at least in the way they are in Java). They're simply pointers to a block of allocated memory, and the [] operator simply automatically does the math to move the pointer to the correct location.

When passing arrays between functions, you're basically just passing a pointer. Unless you want to get into some highly complicated and likely too much for your use case code, you should always pass the size of the array along with it to ensure that your indexes always stay in bounds.

And as the other answer points out, you need to ensure that the life cycle of the array lasts as long as you need it to. Simply returning a pointer to an object doesn't keep it alive like returning a reference does in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM