Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:17:08+00:00 2026-06-15T22:17:08+00:00

I’m working on a problem in C++, but I’m getting a stack overflow exception

  • 0

I’m working on a problem in C++, but I’m getting a stack overflow exception and I can’t tell why. The main method calls problem28(), but the first line should print “check” to my output, which isn’t happening. If I define gridsize as 501 or less it runs fine, but anything more than that and it throws a stack overflow exception.

Any help would be appreciated.

#define right 0
#define down 1
#define left 2
#define up 3
#define gridsize 1001

int* next(int row, int col, int dir) {
    int* newPos = new int[2];
    newPos[0] = row;
    newPos[1] = col;
    switch(dir) {
    case right:
        newPos[1] += 1;
        break;
    case down:
        newPos[0] += 1;
        break;
    case left:
        newPos[1] -= 1;
        break;
    case up:
        newPos[0] -= 1;
        break;
    }
    return newPos;
}

int problem28() {
    cout << "check" << endl;
    int grid[gridsize][gridsize];
    for (int i = 0; i < gridsize; i++)
        for (int j = 0; j < gridsize; j++)
            grid[i][j] = 0;
    int* pos = new int[2];
    pos[0] = pos[1] = gridsize / 2;
    int dir = right;


    for (int i = 1; i <= 1001; i++) {
        grid[pos[0]][pos[1]] = i;
        pos = next(pos[0], pos[1], dir);
        int* npos;

        npos = next(pos[0], pos[1], (dir + 1) % 4);
        if (grid[npos[0]][npos[1]] == 0)
            dir = (dir + 1) % 4;
    }
    cout << "generated grid" << endl;

    int total = 0;
    for (int i = 0; i < gridsize; i++) {
        total += grid[i][i];
        total += grid[i][gridsize - i - 1];
    }
    total -= grid[gridsize / 2][gridsize / 2];

    return 0;
}

int main() {
    problem28();

    system("pause");
    return EXIT_SUCCESS;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:17:09+00:00Added an answer on June 15, 2026 at 10:17 pm

    Your stack is typically quite limited compared to memory overall. Since problem28 doesn’t seem to be recursive, by far the easiest fix that’s most likely to work is to change:

    int grid[gridsize][gridsize]; 
    

    to:

    static int grid[gridsize][gridsize];
    

    That will allocate the memory for that array statically instead of locally, which will typically mean it’s no longer on the stack.

    Another possibility would be to use a std::vector instead of an array. This will will normally allocate its memory from the free store instead of locally. The minor problem is that vector (by itself) doesn’t provide 2D addressing, so you’d have to handle that separately (e.g., using the array_2D I posted in a previous answer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.