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
6
votes
1 answer

Subtracting NULL pointer from a normal pointer generates arithmetic right shift

Here is the C code. int main() { int i = 10, *p = &i; printf("%ld", p - (int *) NULL); } For the pointer arithmetic part, both 'gcc' and 'clang' generate an 'sar rax, 2' instruction in their assembly output. Could someone explain how pointer…
phrack101
  • 91
  • 4
6
votes
5 answers

Is apparent NULL pointer dereference in C actually pointer arithmetic?

I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int. I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi …
karthik A
  • 655
  • 1
  • 11
  • 19
6
votes
1 answer

Can I assume NULL value in comparison as the false?

I know that NULL == (void *)0 but it is mentioned that it can be represented as a value which doesn't contain all zeros. What bothers me is if those pieces of code are equivalent for all (any_type *): any_type *val; if (val) { ... }; and if (val…
6
votes
2 answers

Is printing a null-pointer Undefined Behavior?

When studying the sample code for this question I had assumed it was Undefined Behaviour which was preventing subsequent uses of std::cout from printing. But it turns out that attempting to print a null pointer caused std::ios_base::badbit and…
David G
  • 94,763
  • 41
  • 167
  • 253
5
votes
2 answers

Null pointer dereference not leading to seg-fault in previous statements

I am debugging a crash where we have a code snippet similar to - 1184 static void 1185 xyz_delete ( *c, **a) 1186 { ... ... ... ... 1196 b = *a; 1197 if (!b) { 1198 return; 1199 } ... ... 1203 prev =…
5
votes
2 answers

Can you test for nullpointers in Fortran?

I'm trying to learn Fortran2018 using gfortran. When playing around with pointers I noticed that there doesn't seem to be a facility to test for nullpointers. So I have two questions: Is there really no (straightforward) way to test if a pointer is…
Codebird
  • 91
  • 8
5
votes
3 answers

Can we call (void *)0 a void pointer in C?

Summarizing the C standard, specifically ISO/IEC 9899:201x §6.3.2.3 - 3: If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant.…
LocalHost
  • 910
  • 3
  • 8
  • 24
5
votes
1 answer

Can pointer taken from reference ever be null in well-defined c++?

This question is motivated by uneven treatment of checking pointer value against nullptr by clang and gcc. For this they both emit a warning, but for a pointer taken by using address-of operator on an object they keep quiet. I am pretty sure that…
luk32
  • 15,812
  • 38
  • 62
5
votes
4 answers

Can the compiler cast `(void *) 0` in `execl(prog, arg, (void*) 0)` to a null pointer of the appropriate type?

From The Linux Programming Interface execl(prog, arg, (char *) 0); execl(prog, arg, (char *) NULL); Casting NULL in the manner of the last call above is generally required, even on implementations where NULL is defined as…
Tim
  • 1
  • 141
  • 372
  • 590
5
votes
2 answers

C++98 proper check for a null pointer

C++11 introduced the nullptr keyword, which I don't have available. I suppose there is the NULL macro from C, which I read about using in C++ from some stuff here and here, but I'm still unsure what's the proper way of checking for a null pointer in…
user7851115
5
votes
1 answer

On MSP430, what will happen when I dereference a null pointer?

I know dereferencing a null pointer is undefined - but I would like to know what happens on a specific target - an MSP430. I don't have a board to load this on in front of me to test this out right now. What would happen if I did this (or…
Nick
  • 1,361
  • 1
  • 14
  • 42
5
votes
1 answer

MediaMetadataRetriever getFrameAtTime: videoframe is a null pointer

I´m trying to get a frame of a video, so I´m using MediaMetadataRetriever.getFrameAtTime() like this: Uri directorio = Uri.parse("android.resource://com.extremeye/" + R.raw.video); media = new MediaMetadataRetriever(); …
Gonzalo Solera
  • 1,182
  • 12
  • 26
4
votes
0 answers

Is adding 0 to a null pointer UB?

I’m quite sure this question has been asked before, but I just couldn’t find it. If I have a pointer that is null, e.g. a char* initialized to nullptr, is it undefined behavior (UB) to add 0 to it? I haven’t actually found any claim that adding…
Quirin F. Schroll
  • 1,302
  • 1
  • 11
  • 25
4
votes
2 answers

ANSI C conforming platforms where all-bits-zero is not a null pointer representation

Are there any ANSI C conforming environments where all-bits-zero is not a representation for a null pointer? That is, environments where the following program would print 0? If so, can you list some examples? #include #include…
math4tots
  • 8,540
  • 14
  • 58
  • 95
4
votes
3 answers

NULL passed directly to a function expecting a const reference parameter (VC++ 4.2)

I am looking at something that I discovered in an old code base, and I am pretty confused. Here is a function definition: void vUpdateSequenceDetailsAndIncrement( const CallEvent& roCPEvent, const CallInfo& roCallInfo, …
Dennis
  • 3,683
  • 1
  • 21
  • 43