andy489
andy489

Reputation: 134

how can i delete dynamic array created in a function

I have to write a program (not using vector, nor using structures nor OOP) which calculates the sum and the product of two polynomials. Here is my code:

//Task 4.19 & 4.20
#include <iostream>
#include <cmath>
double* definePoly(char& symbol, char& coef, int& k)
{
    std::cout << "deg(" << symbol << "(x)) = ";
    std::cin >> k;
    symbol++;
    double* a = new double[k];
    for (int i = k; i >= 0; i--)
    {
        std::cout << coef << "_{" << i << "} = ";
        std::cin >> a[i];
    }
    coef++;
    return a;
}
void printPoly(int n, double* P, char& symbol)
{
    symbol--;
    std::cout << symbol << "(x)=";
    for (int i = n; i >= 0; i--)
    {
        if (P[i] != 0)
        {
            if (P[i] > 0)
            {
                std::cout << '+';
            }
            std::cout.setf(std::ios::fixed);
            std::cout.precision(2);
            std::cout << P[i] << ".x^{" << i << "}";
        }
    }
    std::cout << "\n";
    symbol++;
}
double* sumPoly(double* a, int n, double* b, int m, char& symbol)
{ // Task 4.19
    symbol++;
    double* c = new double[(n + m + abs(n - m)) / 2];
    if (n > m)
    {
        for (int i = 0; i <= m; i++) c[i] = a[i] + b[i];
        for (int i = m + 1; i <= n; i++) c[i] = a[i];
    }
    else if (n < m)
    {
        for (int i = 0; i <= n; i++) c[i] = a[i] + b[i];
        for (int i = n + 1; i <= m; i++) c[i] = b[i];
    }
    else for (int i = 0; i <= n; i++)c[i] = a[i] + b[i];
    return c;
}
double* prodPoly(double* a, int n, double* b, int m, char& symbol)
{ // Task 4.20
    symbol++;
    double* d = new double[n + m];
    for (int p = 0; p <= n + m; p++)
    {
        d[p] = 0;
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++)
                if (i + j == p) d[p] = d[p] + a[i] * b[j];
    }
    return d;
}
int main()
{
    int n, m;
    char symbol('P'), coef('a');

    double* firstPoly = definePoly(symbol, coef, n);
    printPoly(n, firstPoly, symbol);

    double* secondPoly = definePoly(symbol, coef, m);
    printPoly(m, secondPoly, symbol);

    double* sum = sumPoly(firstPoly, n, secondPoly, m, symbol);
    std::cout << "Sum:\n";
    printPoly((n + m + abs(n - m)) / 2, sum, symbol);

    double* prod = prodPoly(firstPoly, n, secondPoly, m, symbol);
    std::cout << "Product:\n";
    printPoly(n + m, prod, symbol);

    /*delete[] firstPoly;
    delete[] secondPoly;
    delete[] sum;
    delete[] prod;*/

    return 0;
}

It works perfectly, but when I uncomment the deleting, visual studio tells me that there is an error, otherwise there are leaks.

How can I delete/release the memory in order to solve this problem?

Upvotes: 0

Views: 714

Answers (1)

eerorika
eerorika

Reputation: 238491

double* a = new double[k];
for (int i = k;
    std::cin >> a[i];

The index of the last element of an array of k elements is k - 1 You access the arrays out of bounds in all your loops (this is just one of them) . The behaviour of the program is undefined.

how can i delete dynamic array created in a function

Just like you would delete a dynamic array created outside of a function, and just like you've attempted to delete it:

delete[] firstPoly; // etc

But you must make sure that your program doesn't corrupt its own memory with undefined behaviour.

Upvotes: 7

Related Questions