为什么我不能定义一个二维数组比300 * 300大? [英] Why can't I define a two-dimensional array larger than 300*300?
问题描述
可能重复:结果
用C 最大数组大小
块引用>下面是我的code。我从两个文本文件阅读两个字符串,并将其储存。然后,我定义了两个二维数组,但问题是,规模是非常有限的。例如,如果我定义大小为400,我遇到堆栈溢出编译时。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&WINDOWS.H GT;
#包括LT&;&time.h中GT;的#define BUFSIZE 2000
#定义尺寸400诠释的main()
{
int类型的= 0,LEN1 = 0,LEN2 = 0;
字符字符串1 [BUFSIZE];
字符字符串2 [BUFSIZE];
字符* SUC; FILE * FP1 =的fopen(input1.txt,R);
FILE * FP2 =的fopen(input2.txt,R);
如果((FP1 == 0)||(FP2 == 0))
{
fprintf中(标准错误,错误,同时打开);
返回0;
} SUC =与fgets(字符串1,BUFSIZE,FP1);
如果(!SUC){
//与fgets失败了,现在该怎么办?出口?
返回EXIT_FAILURE;
} SUC =与fgets(字符串2,BUFSIZE,FP2);
如果(!SUC){
// 往上看
返回EXIT_FAILURE;
} LEN1 = strlen的(字符串1);
LEN2 = strlen的(字符串2); INT LCSLength [大小] [SIZE]
的for(int i = 0; I<大小;我++)
{
对于(INT J = 0; J<大小; J ++)
{
LCSLength [I] [J] = 0;
}
} INT指数[大小] [SIZE]
的for(int i = 0; I<大小;我++)
{
对于(INT J = 0; J<大小; J ++)
{
指数[I] [J] = 0;
} } 的printf(以下简称两个字符串为。\\ n \\ n);
的printf(%S \\ n字符串1);
输出(长度为%d \\ n \\ n,LEN1);
的printf(%S \\ n,字符串2);
输出(长度为%d \\ n \\ n,LEN2); INT X = 0;
scanf函数(%d个,&安培; X);
FCLOSE(FP1);
FCLOSE(FP2);
返回0;}这是否有东西跟我的编译器?这是微软的Visual C ++ 2010例preSS。谢谢你在前进。
解决方案INT LCSLength [大小] [SIZE]
INT指数[大小] [SIZE]这些都是主宰两个大的。 2×4×400×400 = 1280000字节。一个进程的主线程的缺省堆栈大小为1 MB,他们不适合。以探测在main()的入口点堆栈的__alloca_probe()调试功能产生异常这个网站被命名为
有关线程的堆栈大小CreateThread的中指定了(),但进程的主线程是由Windows启动。它检索从EXE头,IMAGE_OPTIONAL_HEADER.SizeOfStackReserve领域所要求的大小。这是由链接器,当你建立你的程序编写。您覆盖与/ STACK链接器选项默认,在IDE它是由项目+属性,链接,系统堆栈保留大小选项。
设置Possible Duplicate:
Max Array Size in CHere 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.
解决方案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.
这篇关于为什么我不能定义一个二维数组比300 * 300大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!