Home > Back-end >  what does this null pointer exception mean in my program?
what does this null pointer exception mean in my program?

Time:11-11

I have this simple exercise (in C): twice_3.c
the program takes a vector with three components and double each component. the debugger showed me this green 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. what do you think about it?

#include <stdlib.h>
# include <stdint.h> 

void twice_three(uint32_t *x) {
    for (size_t i = 0; i < 3;   i) {
        x[i] = 2 * *x; 
    }

}


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

}

CodePudding user response:

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

  • Related