Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Core dump with 2D array with RectangleShape type  (Read 1923 times)

0 Members and 1 Guest are viewing this topic.

allmatters

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[SOLVED]Core dump with 2D array with RectangleShape type
« on: July 04, 2024, 05:10:52 pm »
Hello friends, I'm pretty new to C++.
Sorry for the lack of context because I really don't know what causes it to segfault.
Basic data types work, but sf::RectangleShape doesn't? Why is that?

        std::array<std::vector<sf::RectangleShape>,3> bricks {{ {},{},{} }};
        for (int x {0};x<bricks.size();x++)
        {
                bricks[x].reserve(4);
                for (int y {0}; y<bricks[x].capacity();y++)
                {
                        bricks[x][y].setSize(sf::Vector2f(160.f,40.f));
                }
        }
 
« Last Edit: July 05, 2024, 08:14:46 am by allmatters »

kojack

  • Sr. Member
  • ****
  • Posts: 337
  • C++/C# game dev teacher.
    • View Profile
Re: Core dump with 2D array with RectangleShape type
« Reply #1 on: July 04, 2024, 07:45:10 pm »
Vector's reserve function allocates memory, but it doesn't actually make the elements of the vector, you still have to push_back new rectangles before you call setSize on them.
Instead of reserve try: bricks
  • .resize(4);

Resize allocates the memory, but also fills it with the correct number of valid items that you can access right away.

allmatters

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Core dump with 2D array with RectangleShape type
« Reply #2 on: July 05, 2024, 08:13:56 am »
That did it. Thank you so much!