SFML community forums

Help => Window => Topic started by: Akrotrili on August 28, 2013, 02:04:07 am

Title: [Solved] RenderWindow - Stack Overflow
Post by: Akrotrili on August 28, 2013, 02:04:07 am
Hey there,

I just made a little program using the sieve of Eratosthenes to create an image which show if a number is prime or not (just like that (http://commons.wikimedia.org/wiki/File:PrimeNumbersSmall.png)).

It works just fine, but if the size of the window is over 498*498. There's a stack overflow, the error is supposed to be on the 16th line (where I'm creating the window).

Using SFML 2.1, and working with Visual Studio 2012.

Here's the source code :
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>

#include <cmath>

#define WIDTH 500
#define HEIGHT 500
#define MAX WIDTH*HEIGHT
#define SCALE 1

int WinMain()
{
   sf::RenderWindow window(sf::VideoMode(WIDTH*SCALE, HEIGHT*SCALE), "Primes");
      window.setVerticalSyncEnabled(true);

   sf::Event event;

   sf::Image image;
      image.create(WIDTH, HEIGHT);

   sf::Texture texture;
      texture.create(WIDTH, HEIGHT);

   sf::Sprite sprite;
      sprite.setTexture(texture);
      sprite.setScale(SCALE, SCALE);
     
   unsigned int numbers[MAX], primeBase = 2;
   
   // Initializing the array
   for(size_t i = 0 ; i < MAX ; ++i)
        numbers[i] = i+1;
   
   // Finding primes
   while(primeBase < sqrt(MAX))
   {
      // Replacing non-primes by 0
      for(size_t i = primeBase ; i*primeBase <= MAX ; ++i)
         numbers[i*primeBase-1] = 0;

      // Changing the value of primeBase
      for(size_t i = primeBase ; i < MAX ; ++i)
      {
         if(numbers[i] != 0)
         {
            primeBase = numbers[i];
            break;
         }
      }
   }
   
   // Creating the image
   for(size_t i = 0 ; i < MAX ; ++i)
   {
      if(numbers[i] > 1)
         image.setPixel(i%WIDTH, i/WIDTH, sf::Color::Green);
   }

   texture.update(image);
   
   // Main loop
   while(window.isOpen())
   {
      // Event loop
      while(window.pollEvent(event))
      {
         if(event.type == sf::Event::Closed)
            window.close();
      }

      // Displaying the image
      window.clear();
      window.draw(sprite);
      window.display();
   }
}

Thank you for the help. \o
Title: Re: RenderWindow - Stack Overflow
Post by: FRex on August 28, 2013, 02:26:37 am
Use std::vector for numbers, you ran out of stack space probably.
Title: Re: RenderWindow - Stack Overflow
Post by: Akrotrili on August 28, 2013, 12:14:03 pm
The shame of this is that I thought about that, but the debugger has troubled me. :P
Anyway, thanks.
Title: Re: [Solved] RenderWindow - Stack Overflow
Post by: Nexus on August 28, 2013, 02:55:52 pm
#define WIDTH 500
#define HEIGHT 500
#define MAX WIDTH*HEIGHT
#define SCALE 1
Prefer constants over macros; they have several advantages, most notably the scope:
const unsigned int WIDTH = 500;
const unsigned int HEIGHT = 500;
const unsigned int MAX = WIDTH*HEIGHT;
const float SCALE = 1.f;