Re:Interesting C Interview Questions and Answers
by tedunangst on Tuesday August 28, 2012 @05:44PM
(#4446035)
Further to point one, it's under no obligation to provide you with writable memory of the size you'd like.
WTF does 7 come from, the source or the destination?
[ Parent
| on HN ]
Re:Interesting C Interview Questions and Answers
by mturmon on Tuesday August 28, 2012 @06:21PM
(#4446176)
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.
Re:Interesting C Interview Questions and Answers
by ambrice on Tuesday August 28, 2012 @08:16PM
(#4446497)
It 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?
Re:Interesting C Interview Questions and Answers
by tedunangst on Tuesday August 28, 2012 @10:27PM
(#4446804)
int main()
{
char *args[] = { 0 };
execve("./zero", args, 0L);
}
zero will have argc == 0.
Re:Interesting C Interview Questions and Answers
by mturmon on Tuesday August 28, 2012 @11:44PM
(#4447000)
On my MBP with gcc 4.2.1, it does work -- I tried it before posting. (Your interpretation of what it does is correct.)
Re:Interesting C Interview Questions and Answers
by tedunangst on Tuesday August 28, 2012 @10:23PM
(#4446795)
I think the best solution would be something like "Hope setproctitle() is available and use it".