1

Possible Duplicate:
Max Array Size in C

Here is my code. I read two strings from two text files and store them. Then I define two two-dimensional arrays, but the question is, the size is very limited. For example, if I define size as 400, I encounter stack overflow when compiling.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <time.h>

#define BUFSIZE 2000
#define size 400

int main()
{
    int a = 0, len1 = 0, len2 = 0;
    char string1[BUFSIZE];
    char string2[BUFSIZE];
    char *suc;

    FILE *fp1 = fopen("input1.txt", "r");
    FILE *fp2 = fopen("input2.txt", "r");
    if ((fp1 == 0)||(fp2 == 0))
    {
        fprintf(stderr, "Error while opening");
        return 0;
    }

    suc = fgets(string1, BUFSIZE, fp1);
    if (!suc) {
        // fgets failed, what now? exit?
        return EXIT_FAILURE;
    }

    suc = fgets(string2, BUFSIZE, fp2);
    if (!suc) {
        // see above
        return EXIT_FAILURE;
    }

    len1=strlen(string1);
    len2=strlen(string2);

    int LCSLength[size][size];
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            LCSLength[i][j]=0;
        }
    }

    int index[size][size];
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            index[i][j]=0;
        }

    }

    printf("The two strings are: \n\n");
    printf("%s\n", string1);
    printf("The length is %d\n\n", len1);
    printf("%s\n", string2);
    printf("The length is %d\n\n", len2);

    int x=0;
    scanf("%d", &x);
    fclose(fp1);
    fclose(fp2);
    return 0;

}

Does it have something to do with my compiler? It is MS visual C++ 2010 Express. Thank you in advance.

Community
  • 1
  • 1
phil
  • 255
  • 4
  • 16
  • 2
    Allocate that data on the heap instead (`malloc` and `free`). – DCoder Nov 22 '12 at 18:58
  • Language standard doesn't imposes this. Yes it's completely system defined if you are not able to it then. – Omkant Nov 22 '12 at 18:58
  • 2
    You need a larger stack, apparently. If it's the typical 1MB Windows stack, two 400×400 `int` arrays don't fit there, but two 300×300 do. Or `malloc` them, the heap is large enough. – Daniel Fischer Nov 22 '12 at 19:01
  • 1
    Usually a good idea to put large data structures on the heap. – Ed Heal Nov 22 '12 at 19:04
  • Thank you guys, I set the stack reserve size to 10Mb, so it works now. – phil Nov 22 '12 at 19:10
  • @WhozCraig: intriguing. I remember seeing the question; I'm the last editor at the moment, but mostly what I changed was 'lenght' in the text to 'length'. I didn't fix the consistent misspelling in the code. And I didn't spend time noting the array sizes. Gee whiz! Good thing that virtual memory can work in more space than the physical memory. Bad for the performance, though, if you need to access all of the arrays. – Jonathan Leffler Nov 22 '12 at 19:58

1 Answers1

2
int LCSLength[size][size];
int index[size][size];

Those are the two big ones that dominate. 2 x 4 x 400 x 400 = 1,280,000 bytes. The default stack size for the main thread of a process is one megabyte, they don't fit. The __alloca_probe() debug function that probes the stack at the main() entry point generates the exception that this site is named for.

The stack size for a thread is specified in CreateThread() but the main thread of a process is started by Windows. It retrieves the requested size from the EXE header, IMAGE_OPTIONAL_HEADER.SizeOfStackReserve field. Which is written by the linker when you build your program. You override the default with the /STACK linker option, in the IDE it is set by Project + Properties, Linker, System, Stack Reserve Size option.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Being variables local to main, they can also be defined as static, and so moved from the stack to the data section, where there is no such limit. – rodrigo Nov 22 '12 at 20:32