Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无效内存引用(SIGSEGV)错误_C++_String - Fatal编程技术网

C++ 无效内存引用(SIGSEGV)错误

C++ 无效内存引用(SIGSEGV)错误,c++,string,C++,String,我有密码: #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";

我有密码:

#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";
    }


}
我正在rextest上用g++运行代码:g++-Wall-std=c++11-O2-oa.out source_file.cpp

我收到错误无效内存引用SIGSEGV

为什么我会犯这个错误

编译后,我将使用参数test 0.1运行代码

以下是Rextester的代码:。

您作为标准输入通过了测试0.1

我不知道如何在rextester.com上做到这一点

但你可以做到这一点


请参见

运行程序时是否向程序传递任何参数?如果不是,则argv[1]为空。@JoachimPileborg:Or-out-of-bounds.argc>1&&test==argv[1]我认为您通过测试0.1是作为标准输入,而不是作为程序参数。@JoachimPileborg argc==0是允许的,在这种情况下,argv[0]中没有有效的字符串指针。谢谢您的帮助!请您在回答中解释一下g++-pedantic-Wall main.cpp-std=c++11&&./a.out test 0.1的&&部分,以及为什么需要它?@user4063326&&仅当第一个命令以0成功状态退出时才运行第二个命令