I have a sub-optimal solution to return two variables from a function by creating a pointer to an array with two indexes. (just using my current knowledge base from class. I know vectors or std::pair would be a better solution)
However, when I try to delete the array via the pointer, I'm getting the below error.
Am I misunderstanding the procedure? Is this array not created in heap and therefore no need for explicit deletion?
Edit: Quick Comment
thanks to those that are providing more ideal formulations for the type of problem I'm trying to solve. I am (was) just trying to understand the specific behavior of this error I was getting.
I realize this is not an optimal way to store two variables. Just code to experiment with some features of C++. Feel free to provide better formulations for the general benefit of others.
#include <iostream>
#include <cmath>
double* quadraticEquation(double a, double b, double c) {
double x1, x2;
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
double arr[2] = {x1, x2};
return arr;
}
int main()
{
double* solution = quadraticEquation(3, 5, -8);
double x1 = solution[0];
double x2 = solution[1];
std::cout << "x1 = " << x1 << std::endl << "x2 = " << x2 << std::endl;
delete[] solution;
solution = nullptr;
}
