0

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?

10
  • What are the "error" you get if you try to delete[] the arrays? Are you sure you don't go out of bounds of the allocated memory? The definePoly function definitely go out of bounds. Commented Jul 29, 2019 at 11:28
  • "visual studio tells me that there is an error" what error? please include it in the question Commented Jul 29, 2019 at 11:28
  • What happens if you uncomment the deletes? This should work on the first glance. Also, necessary comment: use std::vector instead of manual memory management. Commented Jul 29, 2019 at 11:28
  • compiles fine here: wandbox.org/permlink/nMPPpQ4LmRyipfcv Commented Jul 29, 2019 at 11:30
  • 1
    If the delete of a heap allocated pointer isn't working it tells you that your program has a bug, not that you were doing the delete wrongly. New programmers often think bugs are caused by deletes, but almost always something has gone wrong some time before the delete and that is the real bug. Commented Jul 29, 2019 at 11:44

1 Answer 1

7
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is it considered okay to use new and delete in different scopes, so to say? The new is called in one function but delete is called in another function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.