简体   繁体   English

为什么当在多个文件中通过命令提示符运行该C ++程序时会崩溃,而在单个文件上运行时却不会崩溃?

[英]Why this C++ program crashes specifically when run through command prompt on multiple files but not when run on individual files?

int main() {
  string xs;
  string* atom=new string[60000];
  string* aa=new string[60000];
  string* pos=new string[60000];
  string* aaid=new string[60000];
  string* chain=new string[60000];
  string* X=new string[60000];
  string* Y=new string[60000];
  string* Z=new string[60000];
  string* occupancy=new string[60000];
  string* bfactor=new string[60000]; 
  ifstream inFile;
  inFile.open ("1ASO.pdb"); //similar to 1ASO.txt
  //middle codes
  delete [] atom;
  delete [] aa;
  delete [] pos;
  delete [] aaid;
  delete [] chain;
  delete [] X;
  delete [] Y;
  delete [] Z;
  delete [] occupancy;
  delete [] bfactor;
  inFile.close();
}

This code works fine, but when I changed it to take input of files through command prompt as shown below, it crashes for specific files like 1ASO.pdb. 这段代码可以正常工作,但是当我将其更改为通过命令提示符接收文件输入时,如下所示,它对于诸如1ASO.pdb之类的特定文件崩溃。 I used the following command in command prompt to run the program for all pdb files. 我在命令提示符下使用以下命令为所有pdb文件运行程序。
for %f in (*.pdb) do prog "%f" 对于(* .pdb)中的%f,执行编“%f”
My guess is I'm doing dynamic memory allocation wrong but not sure how to fix it. 我的猜测是我做的动态内存分配错误,但不确定如何解决。

 int main(int argc, char* argv[]) {
   string xs;
   string* atom=new string[60000];
   string* aa=new string[60000];
   string* pos=new string[60000];
   string* aaid=new string[60000];
   string* chain=new string[60000];
   string* X=new string[60000];
   string* Y=new string[60000];
   string* Z=new string[60000];
   string* occupancy=new string[60000];
   string* bfactor=new string[60000]; 
   ifstream inFile;
   inFile.open (argv[1]); //similar to 1ASO.txt
   //middle codes
   delete [] atom;
   delete [] aa;
   delete [] pos;
   delete [] aaid;
   delete [] chain;
   delete [] X;
   delete [] Y;
   delete [] Z;
   delete [] occupancy;
   delete [] bfactor;
   inFile.close();
 }

Is there a special reason why you are allocating all these strings? 分配所有这些字符串是否有特殊原因? Why don't you just declare them as local variables? 您为什么不只将它们声明为局部变量?

Have you looked at what argv[1] is? 您是否看过argv [1]是什么? Are you sure it is the name of the file you want? 您确定它是您想要的文件名吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM