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
33
votes
3 answers

Null pointer check via "myPtr > 0"

In some legacy code I came across the following null pointer check. if( myPtr > 0 ) { ... } Are there any technical risks of checking for a null pointer via this if-check?
ucczs
  • 609
  • 5
  • 15
29
votes
3 answers

Is it undefined behaviour to delete a null void* pointer?

I know that deleteing a null pointer is a no-op: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. (C++ Standard 5.3.5 [expr.delete] p2) And also that deleting a void* pointer is…
Xeo
  • 129,499
  • 52
  • 291
  • 397
29
votes
4 answers

Why "bool c = nullptr ;" compiles (C++11)?

I don't understand why following code compiles ? int main() { //int a = nullptr; // Doesn't Compile //char b = nullptr; // Doesn't Compile bool c = nullptr; // Compiles return 0; } whereas the commented section…
P0W
  • 46,614
  • 9
  • 72
  • 119
26
votes
3 answers

Is dereferencing null pointer valid in sizeof operation

I've come across a snippet of code that to me should crash with a segmentation fault, and yet it works without a hitch. The code in question plus relevant data structure is as follows (with associated comment found right above): typedef struct { …
Veeg
  • 315
  • 4
  • 7
22
votes
2 answers

Is (int *)0 a null pointer?

This could be thought of as an extension to this question (I'm interested in C only, but adding C++ to complete the extension) The C11 standard at 6.3.2.3.3 says: An integer constant expression with the value 0, or such an expression cast to type…
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
22
votes
3 answers

Is performing arithmetic on a null pointer undefined behavior?

It looks to me like the following program computes an invalid pointer, since NULL is no good for anything but assignment and comparison for equality: #include #include int main() { char *c = NULL; c--; printf("c: %p\n",…
Phil Miller
  • 36,389
  • 13
  • 67
  • 90
21
votes
2 answers

How does c++ nullptr implementation work?

I am curious to know how nullptr works. Standards N4659 and N4849 say: it has to have type std::nullptr_t; you cannot take its address; it can be directly converted to a pointer and pointer to member; sizeof(std::nullptr_t) == sizeof(void*); its…
Fullfungo
  • 345
  • 2
  • 13
20
votes
3 answers

Pointer conversion issue with Ternary operator

I know the ternary operator has some surprising restrictions, but I was a bit baffled that this fails to compile for me: void foo(bool b) { int* ptr = ((b) ? NULL : NULL); } Obviously that's the minimum needed to show the problem. The error…
Roddy
  • 66,617
  • 42
  • 165
  • 277
19
votes
3 answers

How do I prevent trouble arising from std::string being constructed from `0`?

void foo (const std::string &s) {} int main() { foo(0); //compiles, but invariably causes runtime error return 0; } The compiler (g++ 4.4) apparently interprets 0 as char* NULL, and constructs s by calling string::string(const char*, const…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
19
votes
4 answers

In which cases is the "this" pointer null in a constructor?

I've experienced an issue where in a constructor, I was using the this pointer, but the this pointer happened to be null at that time. For example: MyClass::MyClass() { // The 'this' pointer happened to be null in this case, // and the program…
user454083
  • 1,337
  • 3
  • 16
  • 31
18
votes
6 answers

Is every null pointer constant a null pointer?

From the C17 draft (6.3.2.3 ¶3): An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.67) If a null pointer constant is converted to a pointer type, the resulting pointer,…
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
17
votes
3 answers

shared_ptr that cannot be null?

Using a std::shared_ptr expresses shared ownership and optionality (with its possibility to be null). I find myself in situations where I want to express shared ownership only in my code, and no optionality. When using a shared_ptr as a function…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
16
votes
4 answers

NULL macro or NULL const

I am currently in the process of reading a book on C++, and in this book the author explains that it is better to use a constant rather than NULL macro, but without really explaining why or giving any advantages or disadvantages. So why it's better…
simon
  • 1,180
  • 3
  • 12
  • 33
15
votes
3 answers

Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

First of all, I've seen this question about C99 and the accepted answer references operand is not evaluated wording in the C99 Standard draft. I'm not sure this answer applies to C++03. There's also this question about C++ that has an accepted…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
14
votes
2 answers

Why does gorm db.First() panic with "invalid memory address or nil pointer dereference"?

I can't figure out if I've done something silly or if I've found a bug in gorm. While I'm very well aware of what "invalid memory address or nil pointer dereference" means, I am completely mystified as to why it appears here. In short, I call…
Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
1
2
3
24 25