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
13
votes
5 answers

Assigning a reference by dereferencing a NULL pointer

int& fun() { int * temp = NULL; return *temp; } In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give…
G Mann
  • 441
  • 1
  • 4
  • 12
13
votes
4 answers

Which of these will create a null pointer?

The standard says that dereferencing the null pointer leads to undefined behaviour. But what is "the null pointer"? In the following code, what we call "the null pointer": struct X { static X* get() { return reinterpret_cast(1); } void f() {…
big-z
  • 6,812
  • 5
  • 20
  • 19
11
votes
1 answer

Why is dereferencing of nullptr while using a static method not undefined behaviour in C++?

I was reading a post on some nullptr peculiarities in C++, and a particular example caused some confusion in my understanding. Consider (simplified example from the aforementioned post): struct A { void non_static_mem_fn() {} static…
11
votes
3 answers

Are NULL and 0 completely equivalent in C?

Are there "hard" reasons for using NULL in preference to 0 in C89/C99+, and interchanging them without a second thought as to deep concerns relating to standards compliance, even in code using very obscure aspects of C? I am concerned with "hard"…
Dan Sheppard
  • 248
  • 3
  • 10
10
votes
2 answers

Why doesn't std::string take a null pointer?

I recently passed a null pointer to a std::string constructor and got undefined behavior. I'm certain this is something that thousands or tens of thousands of programmers have done before me, and this same bug has no doubt crashed untold numbers of…
kdog
  • 1,583
  • 16
  • 28
10
votes
3 answers

Android - Dealing with a Dialog on Screen Orientation change

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class. I have followed the example from Reto Meier's Professional Android Application Development book, Chapter 5 to pull some XML data and then use a dialog to display the…
Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
10
votes
1 answer

CGO, how to pass NULL parameter to C function

Sometimes, a NULL pointer may be needed for a C API. is that possible in CGO? For example, I want to pass a null argument to strcmp() in a Go language program: package strutil /* #include #include */ import ( "C" …
tanbro
  • 101
  • 1
  • 4
8
votes
1 answer

Force allow dereference of NULL pointer

I have the very old (and huge) Win32 project which uses massive checks with NULL pointer by casting to pointer the dereferenced pointer. Like this: int* x = NULL; //somewhere //... code if (NULL == &(*(int*)x) //somewhere else return; And yes,…
user6416335
8
votes
6 answers

Null pointer test performance

What is the performance of testing whether a reference-type variable in C# is a null-pointer (like if (x == null) ...) compared to testing for an integer being smaller than zero or even a bool being false? Are there other issues know regarding such…
ares_games
  • 1,019
  • 2
  • 15
  • 32
7
votes
7 answers

About the non-nullable types debate

I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion dollar mistake, and Spec# has introduced non-nullable types to combat this…
zildjohn01
  • 11,339
  • 6
  • 52
  • 58
7
votes
3 answers

Is it safe to assume that the NULL constant is zero?

The book Understanding and Using C Pointers, by Richard Reese says: The null concept is an abstraction supported by the null pointer constant. This constant may or may not be a constant zero. A C programmer need not be concerned with their…
the_endian
  • 2,259
  • 1
  • 24
  • 49
7
votes
4 answers

Can we subtract NULL pointers?

Since pointer arithmetic is defined within the same array I'm in doubt if we can subtract NULL from another NULL. I'm concerned about the implementation of: //first and second can both either be from the same array //or be both NULL prtdiff_t…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
7
votes
3 answers

Using StringUtils.isEmpty without losing compiler warnings about null pointers

Instead of the usual if (myString == null || myString.equals("")) I tend to prefer using the org.apache.commons.lang.StringUtils class and do if (StringUtils.isEmpty(myString)). However this - at least the way I'm doing it - comes with a gigantic…
SantiBailors
  • 1,596
  • 3
  • 21
  • 44
6
votes
4 answers

Deleting a null pointer

Possible Duplicate: Is there any reason to check for a NULL pointer before deleting? I often see the following in code: if(pointer) delete pointer; To my understanding it is safe to delete a null pointer, so what is the point of this check?
user987280
  • 1,578
  • 1
  • 14
  • 24
6
votes
4 answers

Inconsistent C99 support in gcc and clang

When trying to take advantage of the C99 function prototype syntax specifying non null pointers for function arguments, I came across some inconsistent behavior between clang and gcc: A function can be declared and defined to receive a non-null…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1 2
3
24 25