我編寫了一個用於處理命令行輸入的程序。當長字符串用作輸入時,我在memcpy()函數期間出現分段錯誤。memcpy(),更改預定義變量的值時的分段錯誤
下面是代碼:
int main(int argc, char * argv[])
{
// initialize input variables
char inputFileName[] = "sequence.txt"; //default input file name
//check if a different file name is given
for(int i = 0; i < argc-1; i++){
if(string(argv[i])=="-i"){
cerr << "string: " << string(argv[i+1]).c_str() << endl;
cerr << "string size: " << string(argv[i+1]).size() << endl;
inputFileName[string(argv[i+1]).size()]=0;
cerr << "filename: " << inputFileName << endl;
memcpy(inputFileName,string(argv[i+1]).c_str(),string(argv[i+1]).size());
cerr << "filename after memcpy: " << inputFileName << endl;
break;
}
}
}
當短文件名被賦予(-i sequence.fasta)它正常工作:
$ ./Program -i sequence.fasta
string: sequence.fasta
string size: 14
filename: sequence.txt
filename after memcpy: sequence.fasta
filename final: sequence.fasta
但是長的名字引起分段錯誤:
$ ./Program -i sequencesequencesequencesequencesequencesequencesequencesequencesequencesequence.fasta
string: sequencesequencesequencesequencesequencesequencesequencesequencesequencesequence.fasta
string size: 86
filename: sequence.txt
Segmentation fault: 11
我錯過了什麼嗎?我應該如何處理memcpy()?
'inputFileName'將不會足夠大以存儲結果,因此您將寫入未分配的內存,這是未定義的行爲,崩潰是一種可能的結果。 –
最簡單的方法是不使用'memcpy'。 – juanchopanza
只需將'std :: string'用於'inputFileName',並省略'operator ='的'memcpy'。 – Chad