News for hackers. Stuff that matters.
Re:Interesting C Interview Questions and Answers by ajross on Tuesday August 28, 2012 @06:52PM (#4446291)
Not to defend question 10 per se (I think it's dumb too) I think your criticism is wrong. The ISO C standard declares argv as having type "char*", which clearly requires writability. I don't believe this is covered by an exception like the one for string literals.

Certainly on all real-world platforms with a conforming main() implementation (i.e. there are some embedded systems which have stubbed mains, or just use something like _start directly) argv is passed on the stack and is writable.

Re:Interesting C Interview Questions and Answers by kstenerud on Tuesday August 28, 2012 @07:05PM (#4446328)
The char* formal parameter type is there for historical reasons. The implementation is under no obligation to ensure writability.

Also, while argv itself may be passed on the stack, the memory each entry of argv points to is not.

But once again, we're arguing esoteric edge cases that have no place in an interview, let alone in professional code.

Re:Interesting C Interview Questions and Answers by ajross on Tuesday August 28, 2012 @07:11PM (#4446350)
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 memory they point to is absolutely on the stack at process start.

Re:Interesting C Interview Questions and Answers by kstenerud on Tuesday August 28, 2012 @07:35PM (#4446409)
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[0x1000];
        char buff2[0x1000];
        printf("buff1 = %p\n", buff1);
        printf("buff2 = %p\n", buff2);
        printf("argv  = %p\n", argv);
        for(int i = 0; i < argc; i++)
        {
            printf("arg %d = %p\n", i, argv[i]);
        }
        return 0;
     }
I passed it the params "1 2 3" and got the following:

    buff1 = 0x7fff5fbfe7b0
    buff2 = 0x7fff5fbfd7b0
    argv  = 0x7fff5fbff7f8
    arg 0 = 0x7fff5fbff970
    arg 1 = 0x7fff5fbff9e9
    arg 2 = 0x7fff5fbff9eb
    arg 3 = 0x7fff5fbff9ed
It seems odd that there's a 376 byte gap between what's clearly the stack and what addresses the arguments reside at. They also appear in ascending order despite the fact that the stack is descending (a required convention, maybe?). It's still pretty damn close to the stack so it might very well be the stack with a bunch of process-specific stuff in it, or it could just be that the OS put the start of the stack close to the startup parameters.

Either way, this is very esoteric stuff that has no place in an interview. I've read through the complete spec a number of times and I STILL get stuff wrong!

Re:Interesting C Interview Questions and Answers by throwaway54-762 on Tuesday August 28, 2012 @08:15PM (#4446490)
The location of process arguments on the stack (and environment variables) is just a convention between your kernel and system libc's crt0.a / the dynamic loader. (Not part of the C standard.)
Re:Interesting C Interview Questions and Answers by tedunangst on Tuesday August 28, 2012 @10:36PM (#4446825)
Your program doesn't actually start executing at main(). It runs a bunch of code before it gets there, all of which is farther up the stack.