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; 
}
Answer from wovano on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › code-quality › c6011
Warning C6011 | Microsoft Learn
September 22, 2025 - This warning indicates that your code dereferences a potentially null pointer. If the pointer value is invalid, the result is undefined.
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.

Discussions

Cannot resolve C6011 Dereferencing NULL pointer
Hello, I'm naving C6011 on a pointer even if I'm calling assert to verify it is not NULL. I'm calling assert in a custom macro that extend the actions performed during the assert: #define my_assert(condition, format, ...) … More on learn.microsoft.com
🌐 learn.microsoft.com
0
0
SDL: Fixed warning C6011: Dereferencing NULL pointer 'display'. - SDL Commits - Simple Directmedia Layer
From 4ccc53edfeb2444101dc1f3a900ea88bf45f3b89 Mon Sep 17 00:00:00 2001 From: Sam Lantinga <[EMAIL REDACTED]> Date: Mon, 4 Dec 2023 20:35:14 -0800 Subject: [PATCH] Fixed warning C6011: Dereferencing NULL pointer 'displa… More on discourse.libsdl.org
🌐 discourse.libsdl.org
0
December 5, 2023
warnings - Error C6011:Dereferencing NULL pointer 'NAME'. C - Stack Overflow
I'm getting the warning in the title in my C program, referring to the variable "sequence" in the first for loop. Can someone help me figure out what it means and how to fix it? This is the More on stackoverflow.com
🌐 stackoverflow.com
SDL: Fixed warning C6011: Dereferencing NULL pointer 'SDL_disabled_events[hi]'. - SDL Commits - Simple Directmedia Layer
From 3e54061fa8f6f2af530f3643796dfb38c9f56935 Mon Sep 17 00:00:00 2001 From: Sam Lantinga <[EMAIL REDACTED]> Date: Mon, 4 Dec 2023 19:45:54 -0800 Subject: [PATCH] Fixed warning C6011: Dereferencing NULL pointer 'SDL_d… More on discourse.libsdl.org
🌐 discourse.libsdl.org
0
December 5, 2023
🌐
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1055009 › cannot-resolve-c6011-dereferencing-null-pointer
Cannot resolve C6011 Dereferencing NULL pointer - Microsoft Q&A
#define my_assert(condition, format, ...) \ if (!(condition)) \ { \ printf("***********************************************************"); \ printf("Assert at %s:%s", __FILE__, std::to_string(__LINE__).c_str()); \ printf(format, __VA_ARGS__); \ printf("***********************************************************"); \ } \ assert(condition); ... my_assert(ptr != NULL);
🌐
PySDL
discourse.libsdl.org › sdl commits
SDL: Fixed warning C6011: Dereferencing NULL pointer 'display'. - SDL Commits - Simple Directmedia Layer
December 5, 2023 - From 4ccc53edfeb2444101dc1f3a900ea88bf45f3b89 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 4 Dec 2023 20:35:14 -0800 Subject: [PATCH] Fixed warning C6011: Dereferencing NULL pointer 'display'. --- src/video/SDL_video.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 3f416f2b738a..74cd5d50c1c7 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -1508,7 +1508,7 @@ static int SDL_U...
🌐
Developer Community
developercommunity.visualstudio.com › t › warning-C6011:-Dereferencing-NULL-pointe › 10583605
warning C6011: Dereferencing NULL pointer & assert()
Skip to main content · Visual Studio · Guidelines Problems Suggestions Code of Conduct · Downloads · Visual Studio IDE Visual Studio Code Azure DevOps Team Foundation Server Accounts and Subscriptions · Subscriber Access · Microsoft Security Azure Dynamics 365 Microsoft 365 Microsoft ...
🌐
PySDL
discourse.libsdl.org › sdl commits
SDL: Fixed warning C6011: Dereferencing NULL pointer 'SDL_disabled_events[hi]'. - SDL Commits - Simple Directmedia Layer
December 5, 2023 - From 3e54061fa8f6f2af530f3643796dfb38c9f56935 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 4 Dec 2023 19:45:54 -0800 Subject: [PATCH] Fixed warning C6011: Dereferencing NULL pointer 'SDL_disabled_events[hi]'. --- src/events/SDL_events.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index 040e5bdc8bfc..93588772c20a 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -1355,7 +1355,14 ...
Find elsewhere
🌐
Microsoft
social.msdn.microsoft.com › Forums › vstudio › en-US › b0c2c71a-e4ac-4384-9d7a-31af15ff4e6a › how-to-solve-warning-c6011-dereferencing-null-pointer-39getidresult39
How to solve warning C6011: Dereferencing NULL pointer 'get_IdResult'? | Microsoft Learn
WCHAR id[100]; HRESULT Foo::get_Id(WCHAR** get_IdResult) { *get_IdResult = id; //C6011 warning here... Try checking for NULL-ness with if: if (get_IdResult != NULL) { *get_IdResult = id; } else { return E_POINTER; } Giovanni · Thursday, September 29, 2011 10:18 PM ·
🌐
wxWidgets
forums.wxwidgets.org › board index › wxwidgets programming forums › compiler / linking / ide related
VS17 Intellisense Warns on "Deferencing NULL pointer" from many WxWidgets header file - wxWidgets Discussion Forum
Warning C6011 Dereferencing NULL pointer 'm_node'. window.h Warning C6011 Dereferencing NULL pointer 'm_node'. any.h Warning C6011 Dereferencing NULL pointer 'm_node'. dataobj.h Warning C6011 Dereferencing NULL pointer 'm_node'. gdicmn.h Warning C6011 Dereferencing NULL pointer 'm_node'. list.h Warning C6011 Dereferencing NULL pointer 'm_node'. list.h Warning C6011 Dereferencing NULL pointer 'm_node'. menu.h Warning C6011 Dereferencing NULL pointer 'm_node'. menu.h Warning C26495 Variable 'wxModule::m_state' is uninitialized.
🌐
Mmarsrc
mmarsrc.com › husl › c6011:-dereferencing-null-pointer
c6011: dereferencing null pointer
A NULL pointer dereference exception occurs when an application dereferences an object that is expected to be valid but is NULL We are examining the use of True Sky for a project but one possible blocker is that it fails static analysis with the following errors: e:\tc\87e855efd1971b\ue4\e...
Top answer
1 of 2
2

The problem lies in your deleteFromList function, with this code:

while (temp->data != n && temp != NULL) {
//...

Here, you are trying to check the value of temp->data before you have verified whether or not temp is NULL. Thus, you will, at some point (when you're at the end of the list, and temp is NULL be dereferencing a null pointer - which ain't good!

Instead, just invert the order of the comparisons:

while (temp != NULL && temp->data != n) {
//...

This way, as soon as temp is NULL, the comparison's result will be fully known (see short circuiting), temp->data will not be evaluated, and the loop will stop running.

2 of 2
1

As pointed out by Adrian and Andy, this line causes temp to be dereferenced before you check if it's NULL:

while (temp->data != n && temp != NULL)

so, just check that it's not NULL first, then dereference it.

Other mentionable problems are the memory leaks. You should have exactly one delete for each new (unless you surrender the pointer to a smart pointer that will do delete for you).

void List::deleteFromList(int n) {
    Node* temp = head;
    Node* prev = head;         // set this if you need to delete head

    if(temp != nullptr && temp->data == n) {
        head = prev->next;
        delete prev;           // you forgot this
        return;
    }

    while(temp != nullptr && temp->data != n) {
        prev = temp;
        temp = temp->next;
    }

    if(temp == nullptr) return;

    prev->next = temp->next;
    delete temp;              // you forgot this
}

You also need to implement a destructor in List to delete all the nodes in the List when it is destroyed.

A trickier bug is in your deleteLowerThan() function. You iterate over the nodes in your list and call deleteFromList() which will delete the very node you are currently on. In the next iteration, you use the same node pointer in if (temp->data < n) { causing undefined behaviour. In my case, the program seemed to just hang forever.

One possible fix:

void List::deleteLowerThan(int n) {
    Node* temp = head;
    int tmpdata;

    while(temp != nullptr) {
        tmpdata = temp->data; // save the nodes data
        temp = temp->next;    // step before you delete
        if(tmpdata < n) {
            deleteFromList(tmpdata);
        }
    }
}
🌐
GitHub
github.com › MicrosoftDocs › cpp-docs › blob › main › docs › code-quality › c6011.md
cpp-docs/docs/code-quality/c6011.md at main · MicrosoftDocs/cpp-docs
Allocate memory inside these functions before you dereference the parameter. The following code generates warning C6011 because an attempt is made to dereference a null pointer (pc) inside the function without first allocating memory:
Author: MicrosoftDocs
🌐
Stack Overflow
stackoverflow.com › questions › 67454732 › error-c6011dereferencing-null-pointer-mat-c
Error C6011:Dereferencing NULL pointer 'mat'. C++ - Stack Overflow
learn.microsoft.com/en-us/cpp/code-quality/c6011?view=msvc-160 · Retired Ninja – Retired Ninja · 2021-05-09 06:09:58 +00:00 Commented May 9, 2021 at 6:09 · Okay, a bit more pointedly: If this is an attempt at C++ code, what's with all the calloc? Even if you don't want to use std::array you can do something like int[6][6] mat; instead of this.
🌐
Reddit
reddit.com › r/c_programming › dereferencing null ptr
r/C_Programming on Reddit: Dereferencing Null Ptr
May 16, 2020 -

I would like to ask a question about sending the address of a pointer to structure to a function.

The code below contains code that was minimized.

Minimized version of the code.

The whole project can be seen below;

Linked List

When I call AddBeforeTheNodeX() from main function like AddBeforeTheNodeX(&first, 142, 430); I got an error that says you are dereferencing Null pointer in that function(error code C6011). I use VS 2019 on windows 10. I checked my code and I don't think I'm referencing a Null pointer.

How to handle this warning? Things seem working but I don't why I get this error. Is there any way to tell the compiler that the mentioned pointers are not null pointers?