0

I've given up here and I'm starting over with a clean new question

Let me try to rephrase the question as objectively and simply as possible:

I want someone to either give me a list of platforms (or answer that there are no such platforms) that is both ANSI C conforming and for which the following C program would print 0:

#include <string.h>
#include <stdio.h>

struct MyStruct {
  void *somePointer;
};

int main() {
  struct MyStruct ms;
  memset(&ms, 0, sizeof(ms));
  printf("%d\n", ms.somePointer == 0);
}
ORIGINAL QUESTION:

Consider the following:

struct MyStruct {
  void *somePointer;
};

int main() {
  struct MyStruct ms;
  memset(&ms, 0, sizeof(ms));
  /* Can I assume ms.somePointer == NULL here? */
}

My understanding from posts about null pointers such as this one and this one is that the actual bytes in memory representing a null pointer may be non-zero, even though you could substitute the 0 literal for NULL in source.

In particular, my understanding from the linked posts is that it is possible in ANSI C (C89, C99 or any of the other rervisions) to have ms.somePointer != NULL on some platforms (please correct me if I'm wrong about this).

I have two questions about this:

  1. Are there any real platforms for which I will actually observe ms.somePointer != NULL?
  2. If such platforms exist, how common are these platforms? Could I have a few examples?

EDIT:

To clarify, this question is related, but is not asking the same question.

The linked question is asking about when NULL used to be some nonzero literal, presumably pre-ANSI C.

This question is asking: when I can assume I am working with an ANSI C standards compliant environment where NULL really is interchangeable with 0 constant literal at the source level, are there any platforms/environments that have non-zero representations for null pointers in memory.

math4tots
  • 8,540
  • 14
  • 58
  • 95
  • 2
    I believe a few obscure mainframes have non-zero NULL pointers, but they are few and far between. However, I don't have a record of which mainframes are in that category. (I'm thinking maybe CDC or Honeywell; maybe ICL VME machines.) Such machines are vanishingly rare. The chances are your code won't be ported to such machines. – Jonathan Leffler Oct 03 '22 at 05:22
  • Yes, such platforms exist, but you do not need to care. C explicitly insulates you from that. In C, a `NULL` pointer is always `0`, regardless of what it _actually_ is on the target machine. – Dúthomhas Oct 03 '22 at 05:22
  • 4
    @Dúthomhas: Programmers for exotic systems do need to care because `struct foo *p = calloc(1, sizeof *p);` will not send pointer members in the `struct foo` to null pointer values. – Eric Postpischil Oct 03 '22 at 05:26
  • That was three questions. I edited it, but your intent was not clear for question 3. Do you mean “how common are platforms where null pointers are actually non-zero?” – Dúthomhas Oct 03 '22 at 05:26
  • @EricPostpischil Yes, but that is the caveat of using functions like `memset` and `calloc` to play with pointers — they have never been portable to that kind of architecture. It used to be taught to always initialize pointers to `NULL` explicitly. – Dúthomhas Oct 03 '22 at 05:28
  • @Dúthomhas I think the standard does when I use a `0` compile time literal. But when the value is dynamically set to zero at runtime, esp with something like `memset`, I'm not sure how the compiler could ensure the pointer field is properly set to NULL – math4tots Oct 03 '22 at 05:29
  • @math4tots That is correct. It cannot ensure anything, because `memset` has no idea it is playing with pointers. That is why you should not use `memset`. Create a function specifically designed to zero/NULL-initialize members of your `struct`. Alas. – Dúthomhas Oct 03 '22 at 05:31
  • You can choose not to care, knowing in the back of your mind that on some systems this might be a problem. To counter that, you could use `assert` to verify that a `calloc`-ed struct has pointers that evaluate to `NULL`. Even a single test in your entire codebase could warn about this. – Cheatah Oct 03 '22 at 05:38
  • 2
    Does this answer your question? [When was the NULL macro not 0?](https://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0) specifically https://c-faq.com/null/machexamp.html – Allan Wind Oct 03 '22 at 05:40
  • @AllanWind thanks it's really helpful, but doesn't seem to actually address how likely I will encounter it today. Also, the question is specifically about the NULL macro not being zero, as opposed to my question which is about when the macro expands to zero but has an underlying non-zero representation – math4tots Oct 03 '22 at 05:45
  • How would anyone know "how likely I will encounter it today."? My guess is 0% chance but that's not really a technical question but an opinon. I would argue there is a 1:1 mapping between your perceived problem and `NULL != 0`. Of course I could be wrong. – Allan Wind Oct 03 '22 at 05:53
  • @AllanWind Essentially I'm asking asking domain experts whether this is something that platform providers or compiler writers have been known to do. If as of 2022, someone who's deep in the world of compilers says that this is bad industry practice that basically no one does, that's the sort of answer I'm looking for. – math4tots Oct 03 '22 at 05:56
  • 1
    @AllanWind Arguably the whole point of my question is asking about that 1:1 mapping. The reason why I ended up going down this paranoia hole in the first place is exactly because the standard specifies that `0` literals can specify NULL pointers, but still leaves room for them to be nonzero in memory – math4tots Oct 03 '22 at 06:02
  • 1
    There have existed systems in the past where the null pointer was not represented by all-bits-zero, but that it should be rare with modern systems. [Apparently POSIX requires that all-bits-zero be equivalent to the null pointer](https://stackoverflow.com/a/69219312/). Also, this question is *not* the same as "When was the NULL macro not 0?", although there is very likely to be an existing question about this. – jamesdlin Oct 03 '22 at 06:10
  • @jamesdlin Ooooh, thanks! That's really helpful! At the same time, it feels like non-unique NULL pointers open up a whole nother can of worms XD – math4tots Oct 03 '22 at 06:15
  • @jamesdlin I would've thought so too. It should go without saying but if someone shows me a link to an older question that is actually a duplicate of this one, I'm ready to vote to mark as duplicate – math4tots Oct 03 '22 at 06:29
  • This isn't really a duplicate since the core problem of this question is a (very common and understandable) misconception between NULL and null pointers. – Lundin Oct 03 '22 at 07:03
  • I suggest to change the title as well, since the question explained in the comments seems very different (to me) than the one in the title. Besides, adding "how common are they" makes this "multiple questions" _and_ opinion-based/off-topic. An updated title would improve the question and likely get it reopened. My 2 cents. – wovano Oct 03 '22 at 08:54
  • @math4tots, no need to start a new question, IMHO. This can easily be fixed/improved and get reopened. That's the point of the feedback, I think. – wovano Oct 03 '22 at 09:02
  • @wavano thank you for your feedback, but I'm just really tired. I just want an answer to my question, and I feel like I'm in an infinite loop of trying to re-explain my question and maybe because there's so much attached to this question at this point that new people are just making assumptions about the question before actually reading it :/ – math4tots Oct 03 '22 at 09:12
  • 1
    This question should never have been marked as a duplicate of "When was the NULL macro not 0?" I've changed this question to instead be a duplicate of the new question that the OP created. – jamesdlin Oct 03 '22 at 15:26

0 Answers0