Questions tagged [null-pointer]

In computing, a null pointer has a value reserved for indicating that the pointer does not refer to a valid object.

In computing, a null pointer has a value reserved for indicating that the pointer does not refer to a valid object.

Use this tag for programming questions that refers to a problems having null-pointer exception, bad memory access etc.

Resources:

365 questions
345
votes
9 answers

Is it safe to delete a NULL pointer?

Is it safe to delete a NULL pointer? And is it a good coding style?
qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
203
votes
14 answers

Can I use if (pointer) instead of if (pointer != NULL)?

Is it safe to check a pointer to not being NULL by writing simply if(pointer) or do I have to use if(pointer != NULL)?
danijar
  • 32,406
  • 45
  • 166
  • 297
163
votes
10 answers

Can't find @Nullable inside javax.annotation.*

I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot…
user2354035
129
votes
2 answers

When does invoking a member function on a null instance result in undefined behavior?

Consider the following code: #include struct foo { // (a): void bar() { std::cout << "gman was here" << std::endl; } // (b): void baz() { x = 5; } int x; }; int main() { foo* f = 0; f->bar(); // (a) …
108
votes
1 answer

What is the overhead of Rust's Option type?

In Rust, references can never be null, so in case where you actually need null, such as a linked list, you use the Option type: struct Element { value: i32, next: Option>, } How much overhead is involved in this in terms of…
Thilo
  • 257,207
  • 101
  • 511
  • 656
93
votes
3 answers

Is it guaranteed to be safe to perform memcpy(0,0,0)?

I am not so well-versed in the C standard, so please bear with me. I would like to know if it is guaranteed, by the standard, that memcpy(0,0,0) is safe. The only restriction I could find is that if the memory regions overlap, then the behavior is…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
74
votes
7 answers

Why not call nullptr NULL?

In C++11 the nullptr keyword was added as a more type safe null pointer constant, since the previous common definition of NULL as 0 has some problems. Why did the standards committee choose not to call the new null pointer constant NULL, or declare…
user253751
  • 57,427
  • 7
  • 48
  • 90
69
votes
9 answers

Why are there two ways of expressing NULL in C?

According to §6.3.2.3 ¶3 of the C11 standard, a null pointer constant in C can be defined by an implementation as either the integer constant expression 0 or such an expression cast to void *. In C the null pointer constant is defined by the NULL…
Brad Jones
  • 815
  • 7
  • 10
68
votes
9 answers

How do we check if a pointer is NULL pointer?

I always think simply if(p != NULL){..} will do the job. But after reading this Stack Overflow question, it seems not. So what's the canonical way to check for NULL pointers after absorbing all discussion in that question which says NULL pointers…
cpuer
  • 7,413
  • 14
  • 35
  • 39
52
votes
8 answers

Accessing class members on a NULL pointer

I was experimenting with C++ and found the below code as very strange. class Foo{ public: virtual void say_virtual_hi(){ std::cout << "Virtual Hi"; } void say_hi() { std::cout << "Hi"; } }; int main(int argc,…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
46
votes
9 answers

What is a void pointer and what is a null pointer?

So I was going through some interview questions and I came across one about void and null pointers, which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void…
Shouvik
  • 11,350
  • 16
  • 58
  • 89
41
votes
5 answers

Why is C++'s NULL typically an integer literal rather than a pointer like in C?

I've been writing C++ for many years, using nullptr for null pointers. I also know C, whence NULL originates, and remember that it's the constant for a null pointer, with type void *. For reasons, I've had to use NULL in my C++ code for something.…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
41
votes
8 answers

Uninitialized pointers in code

I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program. Now if that is the case we should never have this line in any…
munish
  • 4,505
  • 14
  • 53
  • 83
38
votes
4 answers

Calling a method on an uninitialized object (null pointer)

What is the normal behavior in Objective-C if you call a method on an object (pointer) that is nil (maybe because someone forgot to initialize it)? Shouldn't it generate some kind of an error (segmentation fault, null pointer exception...)? If this…
Florin
  • 1,844
  • 2
  • 16
  • 21
34
votes
5 answers

C standard compliant way to access null pointer address?

In C, deferencing the null pointer is Undefined Behavior, however the null pointer value has a bit representation that in some architectures make it points to a valid address (e.g the address 0). Let's call this address the null pointer address, for…
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
1
2 3
24 25