0

UPDATE: I solved it, just increased the stack size to 0x40000000, and now my code runs perfectly. (Earlier, I thought the stack size would be sufficient because I was getting a different error before (SIGSEV), and I did not think SIGBUS was also due to stack size)

I am a beginner in C++ and I have to run my recursive code on some very large inputs.

After running, I got SIGSEV Error, and I figured out that it's probably due to low stack size.

I am using CLion and I added

set(CMAKE_EXE_LINKER_FLAGS "-Wl,-stack_size,0x20000000")

to CMakeLists.txt

Now, when I run my code, I get SIGBUS Error (interrupted by signal 10: SIGBUS).

Does anyone know why this could happen and how to fix this?

My Code:

void dfs(int x, int y) {
    if (vis[x][y]) {
        return;
    }
    vis[x][y] = 1;

    if (!useless[x][y]) {
        int cnt_ok = 0;
        for (int d = 0; d < 4; ++d) {
            int nx = x + dx[d];
            int ny = y + dy[d];

            if (valid(nx, ny)) {
                cnt_ok += !useless[nx][ny];
            }
        }
        useless[x][y] = cnt_ok < 2;
    }

    for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d];
        int ny = y + dy[d];

        if (valid(nx, ny)) {
            dfs(nx, ny);
        }
    }
}

This is a DFS on 2-D Grid and function valid() checks for whether the coordinates are valid or not.

dx[4] = {-1, 1, 0, 0} dy[4] = {0, 0, 1, -1}

The dimensions of vis and useless vectors are just the dimensions of the grid.

When I run with debugger in CLion:

enter image description here

(Line 62 is where the void dfs(int x, int y) is written. Line 86 is where dfs(nx, ny) is written.)

Also, if it's relevant - the file is so large that my computer becomes very slow and laggy when my code is running.

I also ran this code on smaller inputs and the code works perfectly fine.

Surprisingly, if a comment out the if (!useless[x][y]) the code runs fine even on large inputs and exits normally.

(Please focus on the code itself, I am beginner in c++, and I just implement pseudo-code given in books, so my code may not align with best practices.)

Thanks!

MangoPizza
  • 269
  • 2
  • 8
  • Dupe of: [C++: SIGBUS Error after increasing stack size to avoid SIGSEV](https://stackoverflow.com/questions/73532172/c-sigbus-error-after-increasing-stack-size-to-avoid-sigsev) – Jason Aug 30 '22 at 06:01
  • @JasonLiam It's my question only)) The last time I was asked to include code, so I ask again with code. – MangoPizza Aug 30 '22 at 06:03
  • The solution to a stack overflow is almost *never* to increase stack size. If your recursion becomes too deep rework it into an iterative solution instead. – Some programmer dude Aug 30 '22 at 06:03
  • @MangoPizza, there is an 'edit' button beneath your question, which you can use to add more details. – Refugnic Eternium Aug 30 '22 at 06:04
  • And realistically, do you really want a 500 MiB stack? That's *way* too large (on Linux the default stack size is only 8 MiB). If you need such a large stack, then your algorithm definitely needs to be changed to something that needs less. – Some programmer dude Aug 30 '22 at 06:06
  • "Why this happens": because DFS doesn't work well on 2D grids of large size. "How to fix this": try using BFS. – Dmitry Kuzminov Aug 30 '22 at 06:11
  • What are the dimensions? What is the expected search depth? – n. m. could be an AI Aug 30 '22 at 06:13
  • @n.1.8e9-where's-my-sharem. The input is several 3000x3000 grids, each is run independently (after re-initializing `vis, useless`). – MangoPizza Aug 30 '22 at 06:14
  • Without seeing a [mre] I guess you store the arrays as local variables? Perhaps you should consider something else, like dynamically allocate of the heap, or using global variables? – Some programmer dude Aug 30 '22 at 06:25
  • @Someprogrammerdude See the update, I solved it by double the stack size. I would definitely try your suggestions and iterative dfs to see if I can also work with smaller stack size. Thanks! – MangoPizza Aug 30 '22 at 06:28

0 Answers0