0

为什么看起来如此简单的事情会导致我的程序崩溃?

我正在尝试获取一个值,n以使数组的大小N和对其执行各种操作,但这不是重点。无论如何,每次我尝试访问时它都会崩溃argv[1]

int main(int argc, char * argv[])
{
  int n;
  n = atoi(argv[1]); //Crashes here!
  cout << "\nN: " << n << endl;
}
4

1 回答 1

1

Does argv[1] exist? To prevent your code from accessing memory it should not check how many arguments were passed.

if(argc >= 2)
  n = argv[1];
else
  std::cout << "Proper usage: .....\n";

This seems like a great time to learn how to use your debugger to view the contents of argv and argc.

于 2013-04-13T23:48:36.063 回答