-1

Sorry for my bad english) I created a dynamic array of structures. This structures contains dynamic array of doubles. When I add the structure, I fill it this way The number of sides and the length of the side are filled in calmly, but when it comes to the vertices, or rather to one vertex (along which I have to restore the rest), after entering any number, the program crashes

struct regular_polygon {
    int count_sides;
    double length;
    double square;
    double perimeter;
    double *x = new double[count_sides];
    double *y = new double[count_sides];
};



void SetData(regular_polygon* reg_pol, int amount, int* output)
{
    cout << "Enter count of sides:" << '\n';
    cin >> reg_pol[amount-1].count_sides;
    bool flag = false;
    if (reg_pol[amount].count_sides > 2) flag = true;
    while (flag == false)
    {
        cout << "Incorrect! Sides must be more than 2. Try again" << '\n';
        cin >> reg_pol[amount].count_sides;
        if (reg_pol[amount].count_sides > 2) flag = true;
    }


    cout << "Enter length of the side:" << '\n';
    cin >> reg_pol[amount].length;
    flag = false;
    if (reg_pol[amount].length > 0) flag = true;
    while (flag == false)
    {
        cout << "Incorrect! Length must be more than 0. Try again" << '\n';
        cin >> reg_pol[amount].count_sides;
        if (reg_pol[amount].length > 0) flag = true;
    }

    cout << "Enter vertex coordinates" << '\n';

    cout << "Enter x:" << '\n';
    cin >> reg_pol[amount - 1].x[ 0];   /// ТУТ ОШИБКА

    cout << "Enter y:" << endl;
    cin >> reg_pol[amount - 1].y[ 0];
    coordinates(reg_pol, amount);
}


    cout << "Enter vertex coordinates" << '\n';

    cout << "Enter x:" << '\n';
    cin >> reg_pol[amount - 1].x[ 0];   /// There is an error

I tried to replace dynamic array of doubles to static array of doubles, but it didnot help, unfortunately

5
  • 1
    double *x = new double[count_sides]; double *y = new double[count_sides]; You allocate arrays of an undetermined size, it results in undefined behavior. Doesn't the compiler warn you about that? Commented Mar 31, 2024 at 21:50
  • contains dynamic array of doubles Dynamic array of doubles is std::vector<double>, double *x = new double[count_sides]; is a valid but likely incorrect code. Commented Mar 31, 2024 at 21:53
  • You appear to be incorrectly assuming that double *x = new double[count_sides] will cause magical resizing of the dynamically allocated array whenever the value of count_sides changes. That is simply not the case. It accesses the current value of count_sides and uses that value in the new expression - only at the time when an instance of regular_polygon is created. If you want a "dynamic array" (one that grows when needed) then you MUST explicitly write code to explicitly resize it whenever needed. Commented Mar 31, 2024 at 22:35
  • Thank you so much. Compiler didn't warn me, I work in Code:Blocks. By the way, which IDE would you recommend? I am student, and I am not a programmer, so I did this silly mistake Commented Mar 31, 2024 at 22:53
  • The best advise would be to just use std::vector etc. Commented Apr 1, 2024 at 17:32

1 Answer 1

0

You need to allocate those dynamically allocated arrays when your object is initialized and count_sides has a value. Since this is C++, you can give your struct a constructor.

Assuming square and perimeter are calculated from the number of sides and length of each side:

struct regular_polygon {
    int count_sides;
    double length;
    double square;
    double perimeter;
    double *x;
    double *y;

    regular_polygon(int count_sides, double length)
    : count_sides(count_sides), 
      length(length),
      square(...),
      perimeter(...),
      x(new double[count_sides]),
      y(new double[count_sides])
    { }
};

Now you also need to worry about a destructor and the rule of three/five/zero.

You also probably want to use std::vector or std::array rather than raw C-style arrays.

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

Comments

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.