0

I am trying to run this code in console app in vs 2013 but when I run the code give following error:

-argv[1]    0x00000000 <NULL>   char *

<unable to read memory>

this is the main function code:

int main(int argc, char **argv)
{
    set_new_handler(memory_err);

     if (strcmp(argv[1], "lit") == 0) {
    // For Rules
    TransPar par;

    get_args(par, argc, argv);   // get arguments
    gen_rules(par);              // generate rules (really, just transactions)
  }

  else if (strcmp(argv[1], "seq") == 0) {
    // For Sequences
    SeqPar par;

    get_args(par, argc, argv);   // get arguments
    gen_seq(par);                // generate sequences
  }

  else if (strcmp(argv[1], "tax") == 0) {
    // For Rules with Taxonomies
    TaxPar par;

    get_args(par, argc, argv);   // get arguments
    gen_taxrules(par);           // generate rules (really, just transactions)
  }

  else if (strcmp(argv[1], "-version") == 0) {
    print_version();
    return 0;
  }

  else {
    cerr << "Synthetic Data Generation, ";
    print_version();
    cerr << "Usage:  " << argv[0] << " lit|tax|seq [options]\n";
    cerr << "        " << argv[0] 
      << " lit|tax|seq -help     For more detailed list of options\n";
    return 1;
  }

  return 0;
}

Also I should mentioned the source code was built for Unix but I modified it for running to win.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Maral Azizi
  • 21
  • 1
  • 3
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 10 '15 at 03:55

1 Answers1

3

You need to check if argc >= 2 before using argv[1]. If the user doesn't supply any arguments then you are accessing an argument that doesn't exist.

According to the C and C++ standards, argv[argc] is guaranteed to be a null pointer, which is what you're seeing here; you're passing a null pointer to strcmp() and this is causing the crash. (Side note: accessing argv[argc + 1] is undefined behavior so you need to be especially careful when accessing indices 2+.)

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300