The compiler is allowed to optimize the whole operation down to a constant, or in fact to anything else (that's what undefined behavior means). I'm curious, is the original C code also dereferencing NULL or is this bindgen generating a bad dereference? Answer from cole-miller on users.rust-lang.org
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › code-quality › c6011
Warning C6011 | Microsoft Learn
Dereferencing NULL pointer 'pointer-name'. This warning indicates that your code dereferences a potentially null pointer. If the pointer value is invalid, the result is undefined.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 433166 › dereferencing-null-pointer-in-c-visual-studio-2019
Dereferencing NULL pointer in C - Visual Studio 2019 - Microsoft Q&A
June 11, 2021 - If you replace the initialization of dArray with malloc, I wonder if the compiler would complain about dereferencing an indeterminate value. ... the key word is "potentially". If calloc should fail then the pointer will be NULL, if it succeeds it won't be NULL. That will only be known at run time, and could be different at different times under varying circumstances. So the compiler has no way to tell what will happen at run time, but it can tell what might happen and is warning you of the possibility.
Discussions

Dereferencing null pointers - what does the standard say?
https://eel.is/c++draft/class.mfct.non-static If a non-static member function of a class X is called for an object that is not of type X, or of a type derived from X, the behavior is undefined. More on reddit.com
🌐 r/cpp_questions
40
14
April 20, 2021
What happens when dereferencing a nullptr?
Dereferencing a null pointer is undefined behavior. In practice, trying to dereference null usually results in a seg-fault, but sometimes the compiler can optimize out the operation entirely. In your example, *p == true; doesn't actually change any of the program state, so the compiler is being smart and removing the extra computation. In the cout line, your program is actually using the result of the computation so it can't be removed. Note: In some cases an aggressive optimizer may recognize that dereferencing a null pointer would be undefined behavior and assume that the pointer therefore cannot be null. This can lead to some unintuitive and hard to find bugs. More on reddit.com
🌐 r/cpp_questions
20
14
August 18, 2022
c - What does "C6011 dereferencing null pointer" mean in my program? - Stack Overflow
The program takes a vector with three components and double each component. The IDE showed me this warning (green squiggle): C6011 dereferencing null pointer v. in this line: v[0] = 12;. I think it's a bug because in the debugger I read the program exited with code 0. More on stackoverflow.com
🌐 stackoverflow.com
C++ : dereferencing NULL pointer warning - Stack Overflow
I am facing this warning in Visual Studio 2022, the code is executed succesufully and the warning is not shown in the output window or error list. But in the code window, it underlined and saying "Dereferencing NULL pointer 'dest' " More on stackoverflow.com
🌐 stackoverflow.com
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › EXP34-C.+Do+not+dereference+null+pointers
EXP34-C. Do not dereference null pointers | CERT Secure Coding
In this noncompliant code example, input_str is copied into dynamically allocated memory referenced by c_str . If malloc() fails, it returns a null pointer that is assigned to c_str . When c_str is dereferenced in memcpy() , the program exhibits undefined behavior .
🌐
Reddit
reddit.com › r/cpp_questions › dereferencing null pointers - what does the standard say?
r/cpp_questions on Reddit: Dereferencing null pointers - what does the standard say?
April 20, 2021 -
01: #include <iostream>
02: 
03: class greeter
04: {
05: public:
06:     void hello()
07:     {
08:     std::cout << "Hello world";
09:     }
10: };
11: 
12: int main()
13: {
14:     ((greeter*)nullptr)->hello();
15: }

runs with no warnings on -Weveryting -Wall on gcc, no warnings on MSVC /W4 either.

https://godbolt.org/z/779Y4Ejzz

I'm sitting with the standard open but I must admit this is taking me forever to find. Do any of you know where to look?

EDIT: So far in my own research, I've got this from 21 years ago:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232

At one point we agreed that dereferencing a null pointer was not undefined; only using the resulting value had undefined behavior.

🌐
Reddit
reddit.com › r/cpp_questions › what happens when dereferencing a nullptr?
r/cpp_questions on Reddit: What happens when dereferencing a nullptr?
August 18, 2022 -

I saw this code in A Tour of C++, but with a bit modify for illustration:

#include <iostream>

int main() {
  char s = 'a';
  char *p = &s;
  while (*p) {
    std::cout << *p;
    p++;
  }
  p = nullptr;
  //std::cout << (*p == true);
  *p == true;
}

I do not know how does while (*p) { end while I do not know what happens when p is nullptr. And std::cout << (*p == true) will induce segment fault but *p == true does not.

Find elsewhere
Top answer
1 of 3
4

First of all, note that the warning is generated by your compiler (or static analyzer, or linter), not by your debugger, as you initially wrote.

The warning is telling you that your program possibly might dereference a null pointer. The reason for this warning is that you perform a malloc() and then use the result (the pointer) without checking for NULL values. In this specific code example, malloc() will most likely just return the requested block of memory. On any desktop computer or laptop, there's generally no reason why it would fail to allocate 12 bytes. That's why your application just runs fine and exits successfully. However, if this would be part of a larger application and/or run on a memory-limited system such as an embedded system, malloc() could fail and return NULL. Note that malloc() does not only fail if there is not enough memory available, it could also fail if there is no large enough consecutive block of memory available, due to fragmentation.

According to the C standard, dereferencing a NULL pointer is undefined behavior, meaning that anything could happen. On modern computers it would likely get your application killed (which could lead to data loss or corruption, depending on what the application does). On older computers or embedded systems the problem might be undetected and your application would read from or (worse) write to the address NULL (which is most likely 0, but even that isn't guaranteed by the C standard). This could lead to data corruption, crashes or other unexpected behavior at an arbitrary time after this happened.

Note that the compiler/analyzer/linter doesn't know anything about your application or the platform you will be running it on, and it doesn't make any assumptions about it. It just warns you about this possible problem. It's up to you to determine if this specific warning is relevant for your situation and how to deal with it.

Generally speaking, there are three things you can do about it:

  1. If you know for sure that malloc() would never fail (for example, in such a toy example that you would only run on a modern computer with gigabytes of memory) or if you don't care about the results (because the application will be killed by your OS and you don't mind), then there's no need for this warning. Just disable it in your compiler, or ignore the warning message.

  2. If you don't expect malloc() to fail, but do want to be informed when it happens, the quick-and-dirty solution is to add assert(v != NULL); after the malloc. Note that this will also exit your application when it happens, but in a slightly more controlled way, and you'll get an error message stating where the problem occurred. I would recommend this for simple hobby projects, where you do not want to spend much time on error handling and corner cases but just want to have some fun programming :-)

  3. When there is a realistic change that malloc() would fail and you want a well-defined behavior of your application, you should definitely add code to handle that situation (check for NULL values). If this is the case, you would generally have to do more than just add an if-statement. You would have to think about how the application can continue to work or gracefully shutdown without requiring more memory allocations. And on an embedded system, you would also have to think about things such as memory fragmentation.

The easiest fix for the example code in question is add the NULL-check. This would make the warning go away, and (assuming malloc() would not fail) your program would run still the same.

int main(void) {
    uint32_t *v = malloc(3 * sizeof(uint32_t));
    if (v != NULL) {
        v[0] = 12;
        v[1] = 59; 
        v[2] = 83; 
        twice_three(v); 
        free(v); 
    }
    return 0; 
}
2 of 3
2

I believe your IDE is warning you that you didn't make sure that malloc returned something other than NULL. malloc can return NULL when you run out of memory to allocate.

It's debatable whether such a check is needed. In the unlikely event malloc returned NULL, your program would end up getting killed (on modern computers with virtualized memory).[1] So the question is whether you want a clean message or not on exit in the very very rare situation that you run out of memory.

If you do add a check, don't use assert. That's useless. For starters, it only works in dev builds (not production builts) where malloc returning NULL is unlikely, and where it's already super easy to find memory leaks (e.g. by using valgrind). Use a proper check (if (!v) { perror(NULL); exit(1) }).


  1. Since people are trying to debate the issue in the comments despite the rules, it looks like I'll have to go into my claim in more detail.

    A couple of people suggested in the comments that "anything could happen" if you ones doesn't check for NULL, but that's simply not true on modern computers with virtualized memory.

    When the C spec doesn't define the behaviour of something (what is called "undefined behaviour"), it doesn't mean anything can happen; it just means the C language doesn't care what the compiler/machine does in such situations. And a NULL dereference is very well defined on such systems. Catching such situations is a raison d'être of memory virtualization!

    Just like you can rely on other compiler-specific features such as gcc's field packing attributes, one can argue it's fine to rely on memory virtualization to detect a failure by malloc.

🌐
Quora
quora.com › How-do-I-avoid-dereferencing-null-pointers-in-C
How to avoid dereferencing null pointers in C - Quora
Answer (1 of 4): This is like asking “How do I avoid getting run over when I cross a street?” The answer? You check for cars before crossing. To avoid dereferencing a NULL pointer, check to make sure it’s not NULL before dereferencing it. That’s it. No fancy tricks.
🌐
Rust Programming Language
users.rust-lang.org › help
Warning about dereferencing a null pointer - #4 by cole-miller - help - The Rust Programming Language Forum
September 21, 2021 - The warning: warning: dereferencing a null pointer --> src/generated/vulkan_bundle.rs:62648:14 | 62648 | &(*(::std::ptr::null::<VkPhysicalDeviceRayQueryFeaturesKHR>())).rayQuery as *const _ …
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › x › QdcxBQ
EXP34-C. Do not dereference null pointers
February 21, 2016 - The requested service is temporarily unavailable · We would like to apologize for any inconvenience that this may cause
🌐
Stack Overflow
stackoverflow.com › questions › 65231574 › solving-c-warning-dereferencing-a-null-pointer
Solving C warning - dereferencing a null pointer - Stack Overflow
I believe the problem is at struct char_stack* stack = calloc(1,sizeof(struct char_stack));. If you just simply say struct char_stack* stack= malloc(sizeof(struct char_stack); as you want only 1 item to save space and then I believe that it probably solve it. If it does not then I would suggest checking if you correctly pronounce sizeof(struct char_stack).In the end, you always must check if (stack==NULL) because the program might not find a space to allocate space for the pointer.
🌐
Secure Coding Blog
blog.bytehackr.in › understanding-and-preventing-null-pointer-dereference
Top 5 Way to Prevent NULL Pointer Dereference
May 8, 2025 - When you run this code, it will likely result in a crash or an exception, such as a segmentation fault. The operating system detects the illegal memory access and terminates the program to prevent any further damage or instability.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › code-quality › c26823
Warning C26823 | Microsoft Learn
October 3, 2025 - void invalidate(int **pp); void ... 5; // warning: C26823 } To solve this warning, make sure there's no null pointer dereference in the code, potentially by adding null checks....