Skip to content

Commit 48d4413

Browse files
committed
Fix stack overflow by making return array unique_ptr
1 parent 8f115c2 commit 48d4413

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

source/config.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace fractal {
99

10-
constexpr std::size_t WINDOW_WIDTH = 800UZ;
11-
constexpr std::size_t WINDOW_HEIGHT = 600UZ;
10+
constexpr std::size_t WINDOW_WIDTH = 800UZ * 2;
11+
constexpr std::size_t WINDOW_HEIGHT = 600UZ * 2;
1212
constexpr std::size_t FRAME_RATE = 60UZ;
1313

1414
constexpr complex_domain START_COMPLEX_DOMAIN{

source/mandelbrot/mandelbrot_window.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::array<float, 8> MandelbrotWindow::draw_coordinate_(
3535
return ret;
3636
}
3737

38-
MandelbrotWindow::arr MandelbrotWindow::calculate_(
38+
std::unique_ptr<MandelbrotWindow::arr> MandelbrotWindow::calculate_(
3939
const DisplayDomain& full_display_domain, const DisplayDomain& new_domain_selection
4040
)
4141
{
@@ -45,14 +45,14 @@ MandelbrotWindow::arr MandelbrotWindow::calculate_(
4545
return draw_coordinate_(coord, to_complex_.to_complex_projections(coord));
4646
};
4747

48-
arr ret;
48+
auto ret = std::make_unique<arr>();
4949
auto process_chunk = [&](DisplayDomain::DisplayCoordinateIterator start,
5050
DisplayDomain::DisplayCoordinateIterator end) {
5151
for (auto it = start; it != end; it += 8) {
5252
display_coordinate pos = *it;
5353
std::array<float, 8> t = process_coordinates(pos);
5454
for (size_t i = 0; i < 8; ++i) {
55-
ret[pos.x++][pos.y] = Percentage{t[i]};
55+
(*ret)[pos.x++][pos.y] = Percentage{t[i]};
5656
}
5757
}
5858
};
@@ -89,7 +89,8 @@ MandelbrotWindow::arr MandelbrotWindow::calculate_(
8989

9090
MandelbrotWindow::MandelbrotWindow(
9191
const DisplayDomain& display_domain, const complex_domain& complex_domain
92-
) : to_complex_{display_domain, complex_domain}
92+
) :
93+
to_complex_{display_domain, complex_domain}
9394
{}
9495

9596
} // namespace fractal

source/mandelbrot/mandelbrot_window.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <SFML/Graphics/Sprite.hpp>
1111
#include <SFML/Graphics/Texture.hpp>
1212

13+
#include <memory>
14+
1315
namespace fractal {
1416
class MandelbrotWindow {
1517
using arr = std::array<std::array<Percentage, WINDOW_HEIGHT + 8>, WINDOW_WIDTH + 8>;
@@ -24,7 +26,7 @@ class MandelbrotWindow {
2426
const DisplayDomain& display_domain, const complex_domain& complex_domain
2527
);
2628

27-
arr calculate_(
29+
std::unique_ptr<arr> calculate_(
2830
const DisplayDomain& full_display_domain,
2931
const DisplayDomain& new_domain_selection
3032
);

source/mandelbrot/window.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class Window : public DisplayEventObserver {
4242
display_domain.get_end_coordinate().y + 1u
4343
);
4444

45-
auto res = mandelbrot_.calculate_(display_domain, display_domain);
45+
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, DISPLAY_DOMAIN);
4646
for (display_coordinate pos : DISPLAY_DOMAIN) {
47-
set_pixel_color(pos, res[pos.x][pos.y]);
47+
set_pixel_color(pos, (*res)[pos.x][pos.y]);
4848
}
4949
}
5050

@@ -62,7 +62,7 @@ class Window : public DisplayEventObserver {
6262
);
6363
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, ends);
6464
for (display_coordinate pos : DISPLAY_DOMAIN) {
65-
set_pixel_color(pos, res[pos.x][pos.y]);
65+
set_pixel_color(pos, (*res)[pos.x][pos.y]);
6666
}
6767
}
6868

0 commit comments

Comments
 (0)