![](/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.