2014-09-21 69 views
0

我的代码:无效存储器引用(SIGSEGV)错误

#include <iostream> 
#include <stdio.h> 
#include <string.h> 


int main(int argc, char *argv[]) 
{ 

    std::cout << "test\n"; 

    std::string test = "test"; 

    if (test == argv[1]) { 
     std::cout << "a test"; 
    } 


} 

我正在与克++代码上Rextester:g++ -Wall -std=c++11 -O2 -o a.out source_file.cpp

我收到错误Invalid memory reference (SIGSEGV)

为什么我会收到此错误?

编译后,我运行的代码与参数test 0.1

这是在Rextester的代码:http://rextester.com/FGVB49226

+1

你传递任何参数的程序,当你运行它?如果不是那么'argv [1]'是'NULL'。 – 2014-09-21 11:50:47

+0

@JoachimPileborg:或超出界限。 – 2014-09-21 11:51:18

+0

'(argc> 1 && test == argv [1])' – 2014-09-21 11:51:36

回答

0

您将通过test 0.1作为标准输入。

我不知道该怎么做就rextester.com

但是你可以在coliru

实现这一见This

+0

谢谢你的帮助!你会在你的回答中解释'g ++ -pedantic -Wall main.cpp -std = C++ 11 && ./a.out test 0.1'的'&&'部分,以及为什么需要它? – user4063326 2014-09-21 12:07:11

+0

@ user4063326如果第一个命令以状态0(成功)退出,'&&'只运行第二个命令, – P0W 2014-09-21 12:16:30