![](/img/trans.png)
[英]Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
[英]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);
链接此错误的图片 。
这: seqB[lenA]=holder; lenA++;
seqB[lenA]=holder; lenA++;
除非您的文件很短(不超过10个字符,并且seqB
和lenB
同样如此),否则将溢出。
您已将seqA和SeqB初始化为10个字符。 我认为该文件大于该文件,并且您正在外部写入序列数组,从而破坏了堆栈。 为序列分配更多的内存。
您读取文件的方法不安全。 您有一个为char [10]的变量,但是如果文件超过10个字符,该怎么办? 您将使用seqA[10] = holder;
在内存中进行写入seqA[10] = holder;
(例如),并且您将覆盖堆栈。 我认为这就是问题所在。 一种解决方案是在读取10个字符后中断,或使用动态分配的数组并在达到其大小时重新分配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.