Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The 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.


Which 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.


That'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.


As 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."


> 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';
    }


To 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.


That 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.


I 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.


> 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.


> 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".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: