3

Though this is strange, but I am getting a segmentation fault while scanning an integer value.

Here is my program :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int t,n,i,j;
    char c;
    int grid[1000][1000],right[1000][1000],down[1000][1000];
    scanf("%d",&t);
    printf("hello\n");
    while(t--)
    {
        scanf("%d",&n);

        memset(right, 0, sizeof(int) * 1000 *1000);
        memset(down, 0, sizeof(int) * 1000 *1000);

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%c",&c);
                printf("CHAR = %c\n", c);
                if(c == '.')
                    grid[i][j] = 1;
                else
                    grid[i][j] = 0;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                printf("%d",grid[i][j]);
            }
        }
    }
    return 0;
}

Doing gdb shows segmentation fault at line scanf("%d",&t);. I cannot figure out how this is happening?

[Using gcc-4.8.4 on a linux 32-bit machine ]

7
  • 12
    Stack overflow dude! Your local arrays grid, right and down are around 4 MB each. Make them static as an ugly "quick fix", or better yet allocate them dynamically. Commented Oct 22, 2015 at 14:34
  • 2
    1000*1000*3.check the name of the website Commented Oct 22, 2015 at 14:35
  • Oh!!! in that case why didn't the compiler throw an error as its the static memory allocated. Maybe because i can compile my binary on 1 system and run on different system? Commented Oct 22, 2015 at 14:47
  • ..because it's the linker that supplies the segment limits, not the compiler. It's not static, it's a local: automatic-storage. You could link it with a very large stack segment and it would run. Commented Oct 22, 2015 at 14:51
  • You can increase the stack size at run-time if you really need to - see e.g. this answer - this is only a last resort though, e.g. for badly-written legacy code that would take too much work to fix properly. Commented Oct 22, 2015 at 15:18

2 Answers 2

1

The problem is that your arrays: grid, right and down are too big to fit into the stack.

As far as the reason for no compile error is concerned:

Because there is nothing wrong with this code in terms of syntax or semantics. The linker also does not have any problem.

The problem arises when the loader tries to load the program and allocate that much memory on the stack. The stack is usually 8 MB on linux systems and your arrays surpass that.

You can make them static (as suggested in the comments) as static members are allocated on the bss or data segment. But in reality you need to rethink if you need such big arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

Set you linker to instruct the loader to allocate a max stack segment limit that is large enough to fit your big local array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.