3 ms·
Question 10 is just crazy broken. I'm not a standards expert, but I don't think argv[0] is guaranteed to be big enough to hold the 7 characters (plus \0) of Ne
by mturmon 14y ago
Question 10 is just crazy broken. I'm not a standards expert, but I don't think argv[0] is guaranteed to be big enough to hold the 7 characters (plus \0) of NewName (i.e., even if it is writable, it may not be big enough).
As the comments said, argv[0] does not have to point to writable memory, because argc can be 0. In this case argv[0] is supposed to be NULL, which would segfault the program.
If you had to solve this question (it would be better not to ask it, because you don't learn much), I think the solution:
argv[0] = "NewName";
would be better. It's at least shorter.
- ambrice 14y agoIt also wouldn't work. This just reassigns your local copy of argv[0] to a different memory location. Also, in what circumstances can argc be 0?
- tedunangst 14y agoint main() { char *args[] = { 0 }; execve("./zero", args, 0L); } zero will have argc == 0.
- mturmon 14y agoOn my MBP with gcc 4.2.1, it does work -- I tried it before posting. (Your interpretation of what it does is correct.)
- tedunangst 14y agoI think the best solution would be something like "Hope setproctitle() is available and use it".