Skip to content

Commit

Permalink
Merge pull request #11 from stevenewald/stack
Browse files Browse the repository at this point in the history
Fix stack overflow by making return array unique_ptr
  • Loading branch information
stevenewald authored Nov 28, 2024
2 parents 8f115c2 + 298b05c commit 49e83f1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace fractal {

constexpr std::size_t WINDOW_WIDTH = 800UZ;
constexpr std::size_t WINDOW_HEIGHT = 600UZ;
constexpr std::size_t WINDOW_WIDTH = 800UZ * 2;
constexpr std::size_t WINDOW_HEIGHT = 600UZ * 2;
constexpr std::size_t FRAME_RATE = 60UZ;

constexpr complex_domain START_COMPLEX_DOMAIN{
Expand Down
6 changes: 3 additions & 3 deletions source/mandelbrot/mandelbrot_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ std::array<float, 8> MandelbrotWindow::draw_coordinate_(
return ret;
}

MandelbrotWindow::arr MandelbrotWindow::calculate_(
std::unique_ptr<MandelbrotWindow::arr> MandelbrotWindow::calculate_(
const DisplayDomain& full_display_domain, const DisplayDomain& new_domain_selection
)
{
Expand All @@ -45,14 +45,14 @@ MandelbrotWindow::arr MandelbrotWindow::calculate_(
return draw_coordinate_(coord, to_complex_.to_complex_projections(coord));
};

arr ret;
auto ret = std::make_unique<arr>();
auto process_chunk = [&](DisplayDomain::DisplayCoordinateIterator start,
DisplayDomain::DisplayCoordinateIterator end) {
for (auto it = start; it != end; it += 8) {
display_coordinate pos = *it;
std::array<float, 8> t = process_coordinates(pos);
for (size_t i = 0; i < 8; ++i) {
ret[pos.x++][pos.y] = Percentage{t[i]};
(*ret)[pos.x++][pos.y] = Percentage{t[i]};
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion source/mandelbrot/mandelbrot_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>

#include <memory>

namespace fractal {
class MandelbrotWindow {
using arr = std::array<std::array<Percentage, WINDOW_HEIGHT + 8>, WINDOW_WIDTH + 8>;
Expand All @@ -24,7 +26,7 @@ class MandelbrotWindow {
const DisplayDomain& display_domain, const complex_domain& complex_domain
);

arr calculate_(
std::unique_ptr<arr> calculate_(
const DisplayDomain& full_display_domain,
const DisplayDomain& new_domain_selection
);
Expand Down
6 changes: 3 additions & 3 deletions source/mandelbrot/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class Window : public DisplayEventObserver {
display_domain.get_end_coordinate().y + 1u
);

auto res = mandelbrot_.calculate_(display_domain, display_domain);
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, DISPLAY_DOMAIN);
for (display_coordinate pos : DISPLAY_DOMAIN) {
set_pixel_color(pos, res[pos.x][pos.y]);
set_pixel_color(pos, (*res)[pos.x][pos.y]);
}
}

Expand All @@ -62,7 +62,7 @@ class Window : public DisplayEventObserver {
);
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, ends);
for (display_coordinate pos : DISPLAY_DOMAIN) {
set_pixel_color(pos, res[pos.x][pos.y]);
set_pixel_color(pos, (*res)[pos.x][pos.y]);
}
}

Expand Down

0 comments on commit 49e83f1

Please sign in to comment.