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 ]

Naveen
  • 7,944
  • 12
  • 78
  • 165
  • 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. – Paul R Oct 22 '15 at 14:34
  • 2
    1000*1000*3.check the name of the website – machine_1 Oct 22 '15 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? – Naveen Oct 22 '15 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. – Martin James Oct 22 '15 at 14:51
  • You can increase the stack size at run-time if you really need to - see e.g. [this answer](http://stackoverflow.com/questions/2279052/increase-stack-size-in-linux-with-setrlimit/2279084#2279084) - this is only a last resort though, e.g. for badly-written legacy code that would take too much work to fix properly. – Paul R Oct 22 '15 at 15:18
  • If you want to see how you could declare the large 2D arrays on the heap (with malloc) you could see this [SO Answer](http://stackoverflow.com/a/26077933/3857942) . – Michael Petch Oct 22 '15 at 21:11
  • Possible duplicate of [Segmentation fault in scanf](http://stackoverflow.com/questions/26077874/segmentation-fault-in-scanf) – Michael Petch Oct 22 '15 at 21:48

2 Answers2

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.

ABcDexter
  • 2,751
  • 4
  • 28
  • 40
tapananand
  • 4,392
  • 2
  • 19
  • 35
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.

Martin James
  • 24,453
  • 3
  • 36
  • 60