debug时报错,但搞不清哪里出问题

jacksonsnake 2008-07-21 05:13:46
复合数组是不是不能设太大?比如m[20000][200000],不过我要读的数据有可能达到2万,该怎么办?


// re.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define FILENAME_SIZE 512
#define LINE_SIZE 512
#define DATA_SIZE 20000

int main()
{
int i = 0;
int k = 0;
int m = 0;/* divident */
int n = 0;/* dividor */
int r = 0;/* remainder */
int a = 0;
int gcd = 0;/* great common divider */
int lcm = 0;/* least common multiplier */
int column = 0;
int row = 0;
int count = 0;
int Length1 = 0;/* length of first sequence */
int Length2 = 0;/* lencth of second sequence */
int this_line_is_data;
int degree = 0;/* degree of sequence */
int addresult =0;
char word[] = ".txt";/* file type */
char temp[100];
char Infile1[FILENAME_SIZE];/* first input file name */
char Infile2[FILENAME_SIZE];/* second input file name */
char Outfile[FILENAME_SIZE];/* output file name */
char buf[LINE_SIZE];
int sequence[DATA_SIZE][DATA_SIZE];/* new sequences */
int matrix[DATA_SIZE][DATA_SIZE];/* generate matrix */
int data1[DATA_SIZE];/* first sequence */
int data2[DATA_SIZE];/* second sequence */
FILE *fp = NULL;
char *ptr = NULL;

//Read the first sequence
printf("\nEnter first file path\n");/* appoint file */
gets(Infile1);
fp = fopen(Infile1, "r");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}

while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL) /* reed one line of file buf */
break; /* if fgets==NULL,it is the end of file,then break */
this_line_is_data = 1;
for (i = 0; buf[i]; i++) /* judge the content of the read line*/
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{ /* if there is any character of this line is not digit or space, ignore this line */
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue; /* swith to next line */

ptr = buf;/* now is sequence line */
while (1)
{ /* read digits */
while (isspace(*ptr))
ptr++; /*ignore space*/
if (sscanf(ptr, "%d", data1+Length1) <= 0) /* if it is the end of this line read another line */
break;
Length1++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);/* close file */
printf("The length of first sequence is %d.\n\nAnd the sequence is\n", Length1);/* print lenght of sequence on screen */
for (i = 0; i < Length1; i++)/* print sequence */
printf("%d ", data1[i]);
printf("\n\n");

//Read the second sequence using the same technique as first sequence
printf("\nEnter second file path\n");
gets(Infile2);
fp = fopen(Infile2, "r");
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile2);
return 1;
}

while (1)
{
if (fgets(buf, LINE_SIZE, fp) == NULL)
break;
this_line_is_data = 1;
for (i = 0; buf[i]; i++)
{
if (!isdigit(buf[i]) && !isspace(buf[i]))
{
this_line_is_data = 0;
break;
}
}
if (!this_line_is_data)
continue;


ptr = buf;
while (1)
{
while (isspace(*ptr))
ptr++;
if (sscanf(ptr, "%d", data2+Length2) <= 0)
break;
Length2++;
while (isdigit(*ptr))
ptr++;
}
}
fclose(fp);
printf("The length of second sequence is %d.\n\nAnd the sequence is\n", Length2);
for (i = 0; i < Length2; i++)
printf("%d ", data2[i]);
printf("\n\n");

//Porcess sequences
printf("Enter the degree of the sequences\n");/* set degree of sequence */
scanf("%d",degree);

if(Length1==Length2)
{/* Tow squences can not in the same length */
printf("\n\nWrong sequences! These tow sequence can not be same length\n");
return 1;
}

//Put sequences into matrix
for(i = 0; i < Length2; i++)
{/* first sequence to column */
for(k = 0; k < Length1; k++)
{/* second sequence to row */
addresult = data1[k]+data2[i];/* adding digit of sequences */
if(addresult < degree)
matrix[i][k] = addresult;
else
matrix[i][k] = addresult-degree;
}
}

//Set first column as degree/2 or (degree-1)/2
for(i = 0; i < Length1; i++)
{
if(degree%2 == 0)
matrix[1][i] = degree/2;
else
matrix[1][i] = (degree-1)/2;
}
//Set first row as 0
for(i = 0; i < Length2; i++)
matrix[i][0] = 0;

//Use Euclid arithmetic to calculate gcd & lcm
m = Length1;
n = Length2;

if(n > m)/* Let m alway larger than n */
{
a = n;
n = m;
m = a;
}


if(m%n == 0)/* calculate the gcd and lcm of length1 and length2 */
{
lcm = m;
gcd = n;
}
else
{
while(m%n == 0)/* gcd(m,n)=gcd(n,r) when r is the remain */
{
r = m%n;
m = n;
n = r;
}
gcd = n;
lcm = (Length1*Length2)/n;/* lcm=(m*n)/gcd */
}


//Get new sequences
column = 0;
row = 0;

for(i = 0; i < gcd; i++)
{/* the number of sequences is gcd */
for(k = 0; k < lcm; k++)
{/* the length of each sequence is lcm */
sequence[i][k] = matrix[column][row];/* get sequences slanting */
column++;
row++;
if(row = Length1)
row = 0;
if(column = Length2)
column = 0;
}
}
//Output sequences to files
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);/* to lower the path namen */

while(strstr(Outfile, word) != NULL)/* search ".txt" in the file path, if it is exist, print fail and enter again */
{
printf("Wrong path!\n");
printf("\nEnter the path to store new sequences without file name or \".txt\"\n");/* set output file path again */
gets(Outfile);
for(i = 0; (Outfile[i] = tolower(Outfile[i])) != '/0'; i++);
}

while(Outfile[count] != '\0')/* count the number of letters of the path */
count++;

for(i=0; i < gcd; i++)
{/* output each sequence to one file */
temp = itoa(i+1, temp, 10);/* integer to array */
Outfile = strcat(Outfile, temp);/* copy the file number to the end of the path */
Outfile = strcat(Outfile, word);/* set the file as txt file */

fp = fopen(Outfile, "w");/* open file */
if (fp == NULL)
{
printf("Failed to open file %s\n", Infile1);
return 1;
}


fprintf(fp, "!New sequence is generated, this is the %d sequence and its degree = %d\n\n", i, degree);
for(k=0; k < lcm; k++)/* sequence write on the file */
fprintf(fp, "%d ", sequence[i][k]);
fprintf(fp, "\n\n!Length of this sequence is %d", lcm);
}

}
...全文
116 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChamPagneZ 2008-07-22
  • 打赏
  • 举报
回复
栈的大小缺省情况下很小,1M还是2M?
可以更改,最大到你的寻址空间大小(4G).
你应该去编译选项里面更改这个设置,否则会分配失败,20000*20000*4已经远远大于默认值了
tfnadnt 2008-07-22
  • 打赏
  • 举报
回复
插入多行代码的时候请套用格式
11000000 2008-07-22
  • 打赏
  • 举报
回复

很明显,数组太大了,一定要动态分配,边处理边保存才好。
jacksonsnake 2008-07-22
  • 打赏
  • 举报
回复
取值全部代码
==================================

column = 0;
row = 0;
int sequence[gcd][lcm];
for(i = 0; i < gcd; i++)
{/* the number of sequences is gcd */
for(k = 0; k < lcm; k++)
{/* the length of each sequence is lcm */
sequence[i][k] = data1[row]+data2[coloum]);/* get sequences slanting */
if(sequence[i][k] >= degree)
squence[i][k] -= degree;
if(row == 0)
sequence[i][k] = 0;
else if(column == 0 && row != 0)
{
if(degree%2 == 0)
sequence[i][k] = degree/2;
else
sequence[i][k] = (degree-1)/2;
}

column++;
row++;
if(row = Length1)
row = 0;
if(column = Length2)
column = 0;
}
}
jacksonsnake 2008-07-22
  • 打赏
  • 举报
回复
好像也不行,内存需求要20000×20000×4byte=1.49Gbyte,还是太大了。
我现在打算不建立矩阵,在取值时直接调用两数组相应的项相加
=======================================
int squence[gcd][lcm];
for(i = 0; i < gcd; i++)
{/* the number of sequences is gcd */
for(k = 0; k < lcm; k++)
{/* the length of each sequence is lcm */
sequence[i][k] = data1[row]+data2[coloum]);/* get sequences slanting */
if(sequence[i][k] >= degree)
squence[i][k] -= degree;
if(row == 0)
sequence[i][k] = 0;
else if(column == 0 && row != 0)
{
if(degree%2 == 0)
sequence[i][k] = degree/2;
else
sequence[i][k] = (degree-1)/2;
}
}
}
=====================================================
但问题是在建sequence[][]的时候好像不能用未知量来定大小
brookmill 2008-07-22
  • 打赏
  • 举报
回复
    int *matrix[DATA_SIZE];
for (i=0; i<DATA_SIZE; i++)
{
matrix[i] = (int *)malloc(DATA_SIZE * sizeof(int));
if (matrix[i] == NULL)
{
printf("Out of memory\n");
exit(1);
}
}


http://blog.csdn.net/ningboweimin/archive/2007/09/03/1769736.aspx
http://blog.csdn.net/xiaoxiongli/archive/2007/11/05/1868552.aspx
agurick 2008-07-21
  • 打赏
  • 举报
回复
马涝克函数,就是干这个用的 。不过你这个空间需要太大了, 恐怕他也搞不腚
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
可不可以这样:
int *pSequence[Length2]=NULL;
pSequence=(int *)malloc(Length1*sizeof(int));
for(i=0; i<Length2; i++)
{
for(k=0; k<Length1; k++)
{
addresult=data1[k]+data2[i];
if(addresult<degree)
*(pSequence[i]+k)=addresult;
else
*(pSequence[i]+k)=addresult-degree;
}
}

还有,原程序里itoa和strcat两个函数都有点问题,能帮帮忙吗?
jacksonsnake 2008-07-21
  • 打赏
  • 举报
回复
不太明白-_-|||~~~~~~~~~
因为两组数据需要点对点相加放进矩阵里,然后从左上角往右斜下取值,而一组数据的长度最大为20000
brookmill 2008-07-21
  • 打赏
  • 举报
回复
在函数里面定义的局部变量是在栈上分配的,没有这么大的栈能放下这个数组的。
最好的办法,如1楼所说用malloc
要想省点事,可以试试把数组定义成全局变量,说不定也可以。
ForestDB 2008-07-21
  • 打赏
  • 举报
回复
动态内存管理,另外改改你的算法。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧