[英]Run-Time Check Failure #2 - Stack around the variable 'seqA' was corrupted
i have windows 8 with vs2010, when i run this part of program this error shown: Run-Time Check Failure #2 - Stack around the variable 'seqA' was corrupted. 我在Windows 8中使用vs2010,当我运行程序的这一部分时,显示此错误:运行时检查失败#2-变量'seqA'周围的堆栈已损坏。 ..............................................................................
................................................... .....................................
#include <iostream.h>
#include <stdio.h>
#include <string>
#include<stdlib.h>
#include <fstream>
using namespace std;
int main(){
int lenA = 0;
int lenB = 0;
FILE * fileA, * fileB;
char holder;
char seqA[10], seqB[10];
/*open first file*/
fileA=fopen("c:\\str1.fa", "r");
/*check to see if it opened okay*/
if(fileA == NULL) {
perror ("Error opening 'str1.fa'\n");
exit(EXIT_FAILURE);
}
/*open second file*/
fileB = fopen("c:\\str2.fa", "r");
/*check to see if it opened okay*/
if(fileB == NULL) {
perror ("Error opening 'str1.fa'\n");
exit(EXIT_FAILURE);
}
/*measure file1 length*/
while(fgetc(fileA) != EOF) {
holder = fgetc(fileA);
seqA[lenA]=holder;
lenA++;
}
lenA--;
fclose(fileA);
holder='0';
/*measure file2 length*/
while(fgetc(fileB) != EOF) {
holder = fgetc(fileB);
seqB[lenB]=holder;
lenB++;
}
lenB--;
fclose(fileB);
This: seqB[lenA]=holder; lenA++;
这:
seqB[lenA]=holder; lenA++;
seqB[lenA]=holder; lenA++;
will overflow unless your file is very short (10 characters or less (and likewise with seqB
and lenB
. 除非您的文件很短(不超过10个字符,并且
seqB
和lenB
同样如此),否则将溢出。
You initialized seqA and SeqB to 10 chars. 您已将seqA和SeqB初始化为10个字符。 I think the file is bigger than that and you are corrupting the stack by writing outside the sequence array.
我认为该文件大于该文件,并且您正在外部写入序列数组,从而破坏了堆栈。 Allocate more memory to the sequence.
为序列分配更多的内存。
Your method for reading file isn't safe. 您读取文件的方法不安全。 You have a variable which is a char [10] but what happend if your file have more than 10 character?
您有一个为char [10]的变量,但是如果文件超过10个字符,该怎么办? You'll write in the memory using
seqA[10] = holder;
您将使用
seqA[10] = holder;
在内存中进行写入seqA[10] = holder;
(for exemple) and by the way you'll overwrite the stack. (例如),并且您将覆盖堆栈。 I think that is the problem.
我认为这就是问题所在。 A solution is to break after 10 character readed or use a dynamically allocated array and reallocate it when his size is reached.
一种解决方案是在读取10个字符后中断,或使用动态分配的数组并在达到其大小时重新分配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.