c++ - Segmentation fault: 11 when iterating over arguments of argv -


I write this C ++ program, the echo command is set to regenerate:

  #include & lt; Iostream & gt; #include & lt; Queue & gt; #include & lt; String & gt; # Include & lt; Iterator & gt; # Include & lt; Unistd.h & gt; using namespace std; Int main (int argc, char * argv []) {// Step 1: To change into a silly four * [] line & lt; String & gt; From queue & lt; String & gt; Arg; For (int i = 1; i & lt; = argc; i ++) {args.push (string (argv [i])); } // Step 2: Use this queue & lt; String & gt; (Because a non-used variable, which is useless) string erg, var, toos; Bool showEndl = true; For the implementation of the AGR (int i = 0; i & lt; = argc; i ++) // Arg is so crazy {arg = argsfront ()); // I can arg = args [i] but for nothing, I'm making queues for the cashier, he takes it in front of the customer, he does not count the number of customers. Args.pop (); // pop if arg (arg [0] == '$') // if it is a variable {var = ""; // variable 'var' to 'reset' (string :: alt = iterator = arg.begin (); it! = Arg.end (); it ++) // Because C ++ is so complex in Python, This is just var = arg [1:] {var + = * it; } Tos + = string (getenv (var.c_str ())); Tos + = ''; } Else if (arg == "-n") // Elif ... No, C ++ elif is not included ... otherwise if this is the -n argument {ShowEndl = false; } And {tos + = arg; Tos + = ''; }} // Step 3: Show the string to show so easy. Cout & lt; & Lt; TOS; // Step 4: Never forget the endless (showEndl) {cout & lt; & Lt; Endl; } String A; }   

It compiles precisely, but when I run it, I mean "segmentation fault: 11" in the console, what does this mean I use LLVM? Why is it like this?

PS: I use LLVM.

Segmentation error is due to memory access violation - Delete invalid indicator:

 (int i = 1; i & lt; = argc; i ++) {args.push (string (argv [i])); }   

When someone is indexed with argc - 1 , then pre> (int i = 0; i & lt; argc; i ++) // includes a program name, argv [0] {args.push (string) (argv [i])); }

or:

 for  (int i = 1; i & lt; argc; i ++) // Include a name for a program Does not, argv [0] {Args.push (string (argv [i])); }   

I suggest using a debugger. This will show you a mistake in the line so that you can check the invalid indicator.


Also change to:

 for  (int i = 0; i & lt; args.size (); ++ i) {arg = argsfront ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -