char *p = ...;
Now, the implication is not that p is uninitialized, but that it's not important what its value is, which is likely what the interviewee thought. As for dynamically allocated memory, that's what pointers are used for the vast majority of the time, right?I would react to stories like this with skepticism, whether told by the interviewee or interviewer, especially when accompanied with a pretentious attitude ("Get out"), because the most fallible part is likely to be human communication.
This is why people advocate for alternatives like Rust where the compiler can be precise on your behalf. But as long as C is still in use, its practitioners need to be exceptionally pedantic with their code.
Though it might be a good thing to say "well, p is uninitialized," it might also come across as obnoxious if that wasn't what the interviewer intended. So it comes down to guessing which response the interviewer is looking for.
The final answer that the memory was dynamically allocated by the compiler is as wrong as it can be.
If I saw this code then my first reaction was that this code probably will produce a segmentation fault in the best case scenario. Well, that was after I verified that this is not some pointer usage question, as I am not an experienced C developer.
It's C code. Your interpretation is not the one used by the compiler.
The point was to catch trivial bugs in code. In this case, the interview didn't.
In fact, it will generate a compiler warning (with -Wall).
The conversation (which OP said was condensed) clearly demonstrates that the interviewee does not understand that the pointer is uninitialized.
To suggest that the interviewee confused it with
char *p = ...;
*p = 'c';
makes no sense, because he said the memory was dynamically allocated 'by the compiler'.Also it would still matter what ... is here.
For example if it was a constant initializer like "string", then the memory would be statically allocated and write-protected.
If on the other hand it was a call like malloc(1), then you would expect some error checking to see if malloc failed.
It's actually a pretty neat little snippet.
Regarding pointers, though, you have a point that char * is commonly used for statically allocated strings. I concede that it's fair to criticize the interviewee for saying the memory was dynamically allocated (by the compiler??), though I don't know if that rises to the level of "Get out."
Then you would just write it as a function like this:
void foo(char *p) {
*p = 'a';
}Do you know how to read? Apparently not. I said explicitly that he conversations are a bit longer that that.
My comment was described as a short summary to illustrate a point. Instead of taking it at face value, you've read all kinds of nefarious emotions into it, and read magical interpretations into the technical portion, too.
This is called "projection".
Yay, the joys of uninitialized variables!
And that's a remarkably unsuited question for a coding interview as it's going to depend on which compiler you use, which flags you have set, and whether the declaration is inside the same function as the reference. The C FAQ (when there was such a thing) used to contain a whole section on things like this.
As a conversation starter it could perhaps be useful, if you can phrase it suitably. My own experience is that trick questions posed as if they had a singular answer will do strange things to nervous people. It's just doesn't tell you much.
If I had this question dropped at me, my hunch would be that the interviewer had a slightly inflated idea of their own knowledge of the language and would likely not take kindly to being taken down. That might well colour my take on the answer. It's not always rewarding to argue minutiae.
It's pretty clearly a conversation starter and there are lots of valid ways to answer the question.
> If I had this question dropped at me, my hunch would be that the interviewer had a slightly inflated idea of their own knowledge of the language and would likely not take kindly to being taken down. That might well colour my take on the answer.
If the interviewer responds poorly to this kind of answer then I don't want to work there anyway. Also, this kind of answer is not "taking down" anyone.
No, the question was "what happens here" and the obvious answer is "you write 'a' to an undefined location, likely segfaulting". Since the candidate failed to spot this a hint was given hoping that the candidate would realize that the pointer didn't point at a valid location.
Bugs like these are one reason why we can not have the nice things on Earth, and an experienced C developer should notice them even when not looking for a bug.
Well, that is at least my impression, as I am not a one, it is just based on my experience with the language.