0

i am trying to make sand simulation but u get stack overflow Eror:

Unhandled exception at 0x00007FF66B384BD7 in Project2.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000D975E03000).

And that is my code:


#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <Windows.h>

using std::vector;

const int Width = 1000;
const int Height = 800;

int main() {

    sf::RenderWindow window(sf::VideoMode(Width, Height), "title");

    vector <sf::CircleShape> Draw;
    float Platform[Width][Height];
    for (int i = 0; i < Width; i++) {
        for (int j = 0; j < Height; j++) {
            Platform[i][j] = 0;
        }
    }

    while (window.isOpen()) {

        sf::Event event;
        while (window.pollEvent(event)) {

            switch (event.type) {

            case sf::Event::Closed:
                window.close();
                break;

            default:
                break;

            } // event check

        } // while Event

        window.clear(sf::Color::Black);
        //////////////////////////

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            sf::Vector2i mousePosition{ sf::Mouse::getPosition(window) };
            sf::Vector2f mouseCoord{ window.mapPixelToCoords(mousePosition) };
            
            float x1 = mouseCoord.x;
            float y1 = mouseCoord.y;
            int x = (int)x1;
            int y = (int)y1;

            Platform[x][y] = 1;


        } // Mouse Pressed



         

        /////////////////////////
        window.display();

    }// main While

    return 0;
}

i try new option , but its not work and i use vector but get another eror

genpfault
  • 51,148
  • 11
  • 85
  • 139
Rwin
  • 11
  • 1
  • 4
    Your bug is `float Platform[Width][Height];` remember the stack is a very limited resource. On msvc the default stack is 1MB in total on linux its ~8 times that but still very limited. – drescherjm Jan 03 '23 at 14:17
  • Unrelated: Instead of manually assigning zero in a loop (`Platform[i][j] = 0;`), just initialize it directly: `float Platform[Width][Height]{};` – Ted Lyngmo Jan 03 '23 at 14:22
  • 1
    if you want to use a vector you should be able to do the following ```vector> Platform(Width, vector (Height, 0));``` – White Wizard Jan 03 '23 at 14:31
  • Note that `vector>` is literally a `vector` managing an array of `vector`s managing an array of `float`s. Since each `vector`'s array is separately dynamically allocated, you have no control over where the array is in memory and can incur significant performance penalties due to the low spatial locality of the structure.. You are often much better off with [a single, large `vector` with a wrapper that presents the `vector` as 2D](https://stackoverflow.com/a/36123944/4581301). – user4581301 Jan 03 '23 at 16:18

0 Answers0