5 ms·
Now I'm curious how you'd like to see this question answered, or how you would answer it yourself ?
by alltakendamned 10y ago
Now I'm curious how you'd like to see this question answered, or how you would answer it yourself ?
- jsnathan 10y agoThe code snippet shows a bug, p is an uninitialized pointer. The behavior is undefined. Theoretically it could point to some valid memory, but saying that it points to dynamically allocated memory definitely implies a misunderstanding.
- gengkev 10y agoWhich is why I would disagree with this example. I suspect the interviewee's answer was due to a misunderstanding rather than a lack of knowledge. For example, change the first line to: 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.
- nitrogen 10y agoTo be fair, this is an absolutely essential thing to be a stickler about in C. It's a dangerous language, and placement of semicolons and recognition of unititialized variables should be burned deep into the psyche of an experienced C developer. 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.
- gengkev 10y agoThat is true, but my point is that in the context of an interview, the purpose of "char *p;" might simply be to indicate the type of p, but not necessarily to indicate that it is uninitialized. It's hard to know what the interviewee thought without asking them. 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.
- eveningcoffee 10y agoI do not know how to convince you as others have failed, but I really believe that trying to understand the correct understanding about the memory allocation was the essence of these questions. 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.
- adekok 10y ago> the purpose of "char * p;" might simply be to indicate the type of p, but not necessarily to indicate that it is uninitialized. 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.
- jsnathan 10y agoThat's a very gracious interpretation I think. The snippet illustrates a kind of trivial bug (uninitialized variables) that should really jump out at anyone who is used to writing C code. (And I think it's clearly C code, and not pseudo-C code.) 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.
- gengkev 10y agoAs I responded to another commenter, the purpose of "char * p" might simply be to indicate the type of p, not necessarily to indicate that it is uninitialized. If you wanted to indicate the type of p, what would you write instead? 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."
- Klockan 10y ago> If you wanted to indicate the type of p, what would you write instead? Then you would just write it as a function like this: void foo(char *p) { *p = 'a'; }
- adekok 10y ago> 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"), 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".
- amag 10y agop contains whatever happens to be on the stack at the position assigned to it by the compiler. Basically that means that p could point anywhere, so by assigning a char 'c' to a "random" address the best thing that could happen is a GPF/segfault. Most likely you'll spend weeks looking for a multitude of weird bugs that seemingly appear at different places out of nowhere, only happens sometimes and never in debug builds. Yay, the joys of uninitialized variables!
- xorcist 10y agoIt could point anywhere, but the question here was where it does point to. 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.
- vostok 10y agoI'm not a developer and I think the question makes sense. I would have given a similar response to what amag wrote, although I would have skipped this bit: "Most likely you'll spend weeks looking for a multitude of weird bugs that seemingly appear at different places out of nowhere, only happens sometimes and never in debug builds." 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.
- 10y ago
- lisivka 10y agoPointer is not initialized, program will crash.