title: Segmentation Fault with `getopt`
body: <p>I have a function which handles arguments two three global variables.
  It works fine with <code>program -s3</code>, but if I put a space between the <code>s</code> and the argument, I get a segmentation fault even though I'm using <code>atoi</code> to remove whitespace.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>bool handleArgs(int argc, char *argv[])
  {
      int arg;
      bool rtVal = true;
  
      while (true)
      {
  
          static struct option long_options[] =
               {
                 {"steps",             optional_argument, 0, 's'},
                 {"walks",         optional_argument, 0, 'w'},
                 {"dimensions",  optional_argument, 0, 'd'},
                 {nullptr, 0, 0, 0}
               };
          int option_index = 0;
  
          arg = getopt_long (argc, argv, "s::w::d::",long_options, &amp;option_index);
      if(arg == -1)
          {
              break;
          }
  
  
          switch(arg)
          {
           case 0:
                       std::cout &lt;&lt; long_options[option_index].name  &lt;&lt; std::endl;
                       if (optarg)
                           std::cout &lt;&lt; " with arg " &lt;&lt; optarg &lt;&lt; std::endl;
                       break;
              case 's':
                      std::cout &lt;&lt; "option -s with value " &lt;&lt; atoi(optarg) &lt;&lt; std::endl;
                  break;
              case 'w':
                      std::cout &lt;&lt; "option -w with value " &lt;&lt; atoi(optarg) &lt;&lt; std::endl;
                  break;
              case 'd':
                      std::cout &lt;&lt; "option -d with value " &lt;&lt; atoi(optarg) &lt;&lt; std::endl;
                      break;
              case '?':
                  /* getopt_long already printed an error message. */
                  rtVal = false;
                  break;
              default:
              rtVal = false;
          }
      }
      return rtVal;
  }
  </code></pre>