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
4
votes
2 answers

What is the special status of the value 0 in c++?

This is purely a philosophical question. I assume that there's no reasonable context in which the result will prove to be useful (given nullptr). According to this - https://en.cppreference.com/w/cpp/language/integer_literal, the type of integral…
Benny K
  • 868
  • 6
  • 18
4
votes
3 answers

Is indirection of null pointer to array type UB?

Consider this code: #include int *f(int (*p)[2]) { return *p; //Possible UB here? } int main() { printf("%p", f(NULL)); } Is the fact that we are applying indirection to null pointer create UB? Maybe it wouldn't because lvalue…
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
4
votes
1 answer

Initializing SharedPreferences gives: Attempt to invoke virtual method on a null object reference

When I launch my app it just crashes and gives me this error code.: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference This is my main…
doritoman159
  • 51
  • 1
  • 3
4
votes
7 answers

Is there a difference between initialiazing variables and pointers with NULL or 0?

Which of the following methods is a more correct way to initialize a variable? int x = 0; int x = NULL; What about the pointers? I have been told discordant things about NULL, like : "NULL is best for initializing pointers" or "Don't use NULL but 0…
Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21
4
votes
5 answers

Creating a file using fopen()

I am just creating a basic file handling program. the code is this: #include int main() { FILE *p; p=fopen("D:\\TENLINES.TXT","r"); if(p==0) { printf("Error",); } fclose(p); } This is giving Error, I cannot create files tried…
Popeye
  • 41
  • 1
  • 1
  • 3
4
votes
1 answer

Fortran array of derived types and memory leaks despite finalization

I defined a derived type and encountered some problems with memory deallocation although I had written the final procedure. The code is as follows module ModuleCoordinate implicit none type :: TCoordinate real(8),dimension(:),pointer ::…
Waltergu
  • 51
  • 5
4
votes
3 answers

Literal zero instead of null-pointer-constant warning by MISRA

I have this function: void InitS(unsigned int &numS){ // this function returns a container for unsigned int // but it has a cast for int numS = props.numOfS(); if (numS > 0) { .. } } It compiles but gives me this MISRA…
or.nomore
  • 915
  • 1
  • 10
  • 20
3
votes
2 answers

Is there an equivalent of __attribute__((nonnull)) in C23?

Several compiler vendors have implemented a non standard extension __attribute__((nonnull)) to specify that a pointer must not be a null pointer. C99 introduced a new syntax to specify that a function argument must point to the first element of an…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
3
votes
2 answers

Getting address boundary error when working with pointers in C

The following code gives me a terminated by signal SIGSEGV (Address boundary error): void rec(int x, int *arr, int *size) { if (x < 0) { rec(-x, arr, size); return; } arr = realloc(arr, sizeof(int) * ++(*size)); *(arr + (*size) -…
3
votes
1 answer

Three questions: Is NULL - NULL defined? Is (uintptr_t)NULL - (uintptr_t)NULL defined?

Is NULL - NULL defined.? Is (char *)NULL - (char *)NULL defined.? Is (uintptr_t)NULL - (uintptr_t)NULL defined? I know that it works in all used by me implementations. But how does it look like from the standard point of view? I cant find the…
0___________
  • 60,014
  • 4
  • 34
  • 74
3
votes
3 answers

Is NULL a valid FILE*?

I would like to have a function along the lines of /*In header*/ void foo(FILE *outpt=stdout); /*In implementation*/ void foo(FILE *outpt) { if(outpt) fprintf(outpt, "Hello!"); } But this code is (obviously) broken if NULL==stdout. (Edit:…
Jacob Manaker
  • 723
  • 4
  • 17
3
votes
1 answer

Does forming a reference to an object constitute access?

Does forming a reference to an object constitute access? Here's what GCC and Clang currently do: void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does…
3
votes
2 answers

C++ Losing pointer reference after scope end

I'm getting a really weird error where after I leave the for scope I can't access whatever my pointer was pointing during the loop even if the array holding the objects is declared in the class header. This is the basic of the code: Class CTile{…
Luke B.
  • 1,258
  • 1
  • 17
  • 28
3
votes
2 answers

Firebase null pointer exception

I am getting null pointer exception in firebase I knows that there is not any value like that to which database reference I am giving but I wants that when the value is generated then it will be shown automatically... Just wants to get that single…
Kawal Singh
  • 65
  • 1
  • 6
3
votes
3 answers

Null pattern with QObject

(C++/Qt) I have a smart pointer to a QObject. Let's say a QWeakPointer. For some external reason (something that might happen in another object or due to an event), it is possible that the pointed object gets destroyed. Since I have a smart pointer…
user427569
  • 143
  • 2
  • 11