0

I can't make int k = atoi(argv[1]); work; I've included stdlib and tried a variety of things but the program doesn't like it no matter what I've tried.

The program compiles OK, but when I run it, it says 'core dumped' before it gets to that statement.

  • 2
    Check argv[1] isn't NULL before using it. Try if (argv[1] != null) – user103853 Sep 17 '14 at 19:00
  • why would argv[1] be NULL? I'm working on the same problem but after adding something and then deleting it, my code core dumps. I don't have int k = atoi(argv[1]); at all. – Anissa Sep 18 '14 at 1:44
  • @Blackwidowninja It's a good thing to check since that argument might not of been specified by the user. This allows for a more robust program. If you don't use atoi(), then argv[1] will hold the string version of a number and wouldn't be able to be used in integer operators. That's probably why you are getting the core dumps. – user103853 Sep 18 '14 at 5:48
  • check that argc == 2 && argv[1] != NULL, immediately exiting if otherwise. – rigel Nov 20 '14 at 9:40
0

If it's getting a segmentation fault before that statement (how are you aware of that, are you using gdb??), then the problem it's before executing that assignment.

Assuming you don't know for certain the coredump is being reached before that statement it's certainly difficult to debug as it's a very small amount of code and which is correct. Perhaps the problem isn't there at all, or perhaps a not thought design it's displaying it's lack of error checking at that point.

Whatever be the case, keep in mind that an error of that type it's cause by trying to write locations in memory where you have not access or that don't exist or something like that. It's a memory related issue.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .