Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Now I need to dig in to find out the reason for the segfault. If only I knew C as well as Perl.

    If you've not got a coredump already from it, I'd suggest running it under gdb and using that to see where it goes boom. All the code looks legal, and I can't see anything that can actually go wrong. Except that argv[0] might be a null pointer. Which seems unlikely, but is the only possible problem that I can spot.

  • Unless something weird is going on, argv[0] should be the program name. So doing an atoi will generally produce nothing useful, unless the program's name is 666 or some such.

    That said, it seems a far simpler way to do what the code's doing is to just use sprintf to do the work:

    sprintf(, "%d", argv[0]);

    Which would do nothing -- including not segfault -- if argv[0] isn't numeric.

    Fair warning: it's 4:30 am as I write this, after a bout of insomnia. Use at your own risk. ;-)

    • Worse yet, calling atoi on it will give you back an integer, which is then being cast to a char *, so you're looking in whatever bit of memory the integer value you get refers to and treating it as a character array. There's nothing good that can come of this, it's quite broken.
    • Unless something weird is going on, argv[0] should be the program name.

      True. There's insufficient context to see whether anyone modified argv (++argv or somesuch). I'm not sure if I'd count modifying argv as weird though, as I've been known to write code that increments it in a while loop to process all command line arguments.

      • I meant unless argv isn't even the command line: if it's an array internal to the program or something. Nothing in C enforces using argc and argv for program arguments; it's just a convention that is all too often broken.