help about this stack overflow?
-
#include #include int min(int a, int b, int c) { int temp=a<=b?a:b; return temp<=c?temp:c; } int MinOperation(int len1, char str1[], int len2, char str2[]) { int count[10001][10001]; int i,j; for(i=0; i<1001; i++) count[0][i]=0; for(i=0; i<1001; i++) count[i][0]=0; for(i=1; i<=len1; i++) for(j=1; j<=len2; j++) { if(str1[i]==str2[j]) count[i][j]=min( count[i-1][j-1], count[i-1][j]+1, count[i][j-1]+1); else count[i][j]=min( count[i-1][j-1]+1, count[i][j-1]+1, count[i-1][j]+1 ); } return count[len1][len2]; } void main() { int i=0; int len1; int len2; char str1[1001]; char str2[1001]; scanf("%d%s", len1, (str1) ); scanf("%d%s", len2, (str2) ); puts(str1); puts(str2); printf("%d", MinOperation(len1 ,str1 ,len2 ,str2)); } I am not sure why there exist a bug stack overflow?
-
#include #include int min(int a, int b, int c) { int temp=a<=b?a:b; return temp<=c?temp:c; } int MinOperation(int len1, char str1[], int len2, char str2[]) { int count[10001][10001]; int i,j; for(i=0; i<1001; i++) count[0][i]=0; for(i=0; i<1001; i++) count[i][0]=0; for(i=1; i<=len1; i++) for(j=1; j<=len2; j++) { if(str1[i]==str2[j]) count[i][j]=min( count[i-1][j-1], count[i-1][j]+1, count[i][j-1]+1); else count[i][j]=min( count[i-1][j-1]+1, count[i][j-1]+1, count[i-1][j]+1 ); } return count[len1][len2]; } void main() { int i=0; int len1; int len2; char str1[1001]; char str2[1001]; scanf("%d%s", len1, (str1) ); scanf("%d%s", len2, (str2) ); puts(str1); puts(str2); printf("%d", MinOperation(len1 ,str1 ,len2 ,str2)); } I am not sure why there exist a bug stack overflow?
wendyyue wrote:
scanf("%d%s", **&**len1, (str1) ); scanf("%d%s", **&**len2, (str2) );
if you specified parameters for scanf correctly, the above, then the stack overflow is due to the stack allocation int count[10001][10001]; which is "10001 * 10001 * sizeof (int)" which is very large than the default stack size which is 1MB. To increase the stack size /STACK (Stack Allocations)[^]. It is better to allocate in heap. And also array size in main 1001 and size in MinOperation 10001 check it for logic of your program (Any way int count[1001][1001] is also going to issue stack overflow).