0

I am currently trying to create an application in C++ using a library called Simple2D.

https://github.com/simple2d/simple2d

There seems to be a problem with the line delete [] coor. I get a "has triggered a breakpoint" exception. When I delete the line everything works properly. I initialize the pointer with a new keyword so I assume I must use the delete. Why?

#include <simple2d.h>
#include <cmath>
#include <iostream>

class Function;
void render();
void update();

const int width = 800, height = 600;

class Point
{
    float x, y;
};

class Function
{
    friend class Window;
    Point * points[];
    void calculate_points()
    {

    }
public:
    virtual float func(float x) = 0;
    Function()
    {

    }
};

class Polynomial1
{
public:
    float func(float x)
    {
        float y = pow(x, 3) + pow(x, 2) + 1;
        return y;
    }
};

class Axis
{
    friend class Window;
    float * coor;
    int n_points;
    float start, end, width;
    char name;
    void calculatePoints()
    {
        float scale = (end - start) / n_points;
        for (int i = 0; i < n_points; i++)
        {
            *(coor + i) = start+(scale*i);
        }
    }
public:
    Axis(char n='X', float st=-100, float e=100, int points = 10, int w = 3)
    {
        if (st < e)
        {
            name = n;
            start = st;
            end = e;
            n_points = points;
            width = w;
            coor = new float[n_points];
            calculatePoints();
        }
        else
        {
            std::cout << "Invalid data! Start cannot be of a larger value than end!";
        }
    }
    ~Axis()
    {
        if (coor != nullptr) {
            delete[] coor;
        }
    }
};

void drawFunc(Function& func)
{

}

void drawAxis(Axis axis)
{
    S2D_DrawLine(0, 300, 800, 300,
        3,
        0, 0, 0, 1,
        0, 0, 0, 1,
        0, 0, 0, 1,
        0, 0, 0, 1);
}

void render()
{

}

void update()
{
    Axis ax1;
    drawAxis(ax1);
}

int main(int argc, char* argv[])
{
    S2D_Window* window = S2D_CreateWindow(
        "Graphing Calculator",  // title of the window
        width, height,        // width and height
        render, update,  // callback function pointers (these can be NULL)
        S2D_RESIZABLE              // flags
    );
    window->viewport.mode = S2D_STRETCH;
    window->background.r = 1.0;
    window->background.g = 1.0;
    window->background.b = 1.0;
    S2D_Show(window);
    return 0;
}
hash1
  • 89
  • 1
  • 1
  • 9
  • 3
    Quite possible your problem is due to violation of the [Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), but we can't tell without [mcve]. – Yksisarvinen Apr 04 '23 at 17:33
  • If you are on Visual Studio don't panic break into the debugger and inspect the nature of the problem. Use the "Stack Frame" combo box on the debug toolbar to switch to your code. – drescherjm Apr 04 '23 at 17:37
  • 1
    `void drawAxis(Axis axis)` This creates a copy of the Axis object, so now you have two objects holding the same pointer. This is going to cause trouble later! – BoP Apr 04 '23 at 17:37
  • I see, this library uses global functions to run. So yes, the problem is with Rule of Three, `drawAxis` creates a copy of the object, but the implicit copy constructor can't take into account your special needs. You should use `std::vector` instead of raw array and see your problem gone by the Rule of Zero (no need for any destructor or copy operations), or implement the Rule of Three correctly. – Yksisarvinen Apr 04 '23 at 17:39
  • Interestingly `void drawAxis(Axis axis)` does not even use the `axis` but that will still cause a problem since the code violates the rule of 3. – drescherjm Apr 04 '23 at 18:21

0 Answers0