Earlier quoted context omitted.
Again, I think that's wrong. Can you cite anything in the standard that allows a non-writable argv? Again, the only comparable situation I can think of is the allowed implicit conversion of a string literal to char* even though the memory is allowed to be read-only. That doesn't apply here. And the bit about argv not being passed on the stack is simply wrong on all real platforms. The argv[] array of pointers and the…
Hmm actually you are right on the writable part: 5.1.2.2.1 Program Startup: "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination." However, nothing says they must be on the stack. I ran the following program to test this: int main(int argc, const char * argv[]) { char buff1[0x10…
Interesting C Interview Questions and Answers
31–40 of 41 posts
Re: Interesting C Interview Questions and Answers
#32Earlier quoted context omitted.
Further to point one, it's under no obligation to provide you with writable memory of the size you'd like. strncpy(argv[0], "NewName", 7); WTF does 7 come from, the source or the destination?
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 th…
Re: Interesting C Interview Questions and Answers
#33The G choice was always "No, looks good".
It's timed.
Re: Interesting C Interview Questions and Answers
#34Earlier quoted context omitted.
Further to point one, it's under no obligation to provide you with writable memory of the size you'd like. strncpy(argv[0], "NewName", 7); WTF does 7 come from, the source or the destination?
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 th…
Re: Interesting C Interview Questions and Answers
#35Earlier quoted context omitted.
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 th…
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?
int main()
{
char *args[] = { 0 };
execve("./zero", args, 0L);
}
zero will have argc == 0.Re: Interesting C Interview Questions and Answers
#36Earlier quoted context omitted.
Again, I think that's wrong. Can you cite anything in the standard that allows a non-writable argv? Again, the only comparable situation I can think of is the allowed implicit conversion of a string literal to char* even though the memory is allowed to be read-only. That doesn't apply here. And the bit about argv not being passed on the stack is simply wrong on all real platforms. The argv[] array of pointers and the…
Hmm actually you are right on the writable part: 5.1.2.2.1 Program Startup: "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination." However, nothing says they must be on the stack. I ran the following program to test this: int main(int argc, const char * argv[]) { char buff1[0x10…
Re: Interesting C Interview Questions and Answers
#37Earlier quoted context omitted.
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 th…
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
#38Re: Interesting C Interview Questions and Answers
#39Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits. Well this is always true, so I guess that means there's no such thing as memory leaks in C. Checkmate garbage collectors.
An actual memory leak is when the amount of memory program allocates grows with runtime while the amount of data the program is processing remains the same.
Re: Interesting C Interview Questions and Answers
#40Earlier quoted context omitted.
None of these are especially interesting C questions... Most rely on assumptions about the x86 architecture and GCC (stack grows downwards, ordering of stack variables in memory (not registers), ...). #3 notes a warning for main not returning int, but doesn't point out that without stdlib.h, malloc() and free() will be declared implicitly with incorrect types. (Edit: this produces a warning on 4.7 GCC and Clang 3.0.)…
Also the casts to (char *) from malloc suggest that the author is a C++ programmer.