377 questions
10
votes
1
answer
733
views
Why do compilers not warn about this null dereferencing, even when they detect it?
Consider the following program:
int main() {
int* ptr = nullptr;
return *ptr + 1;
}
(also on GodBolt)
How is it, that with "decent" warnings enabled (-Wall -Wextra), both popular ...
2
votes
1
answer
148
views
Does risc-v require memory address 0 to be invalid?
So in risc-v, for a virtual memory system, I imagine it's up to the kernel to decide if 0 is a valid memory address or not? But for machine mode, or supervisor mode, is memory address 0 valid to ...
0
votes
1
answer
118
views
Strange Behavior Compiler Ignoring NULL Check Unless I Print Something in the if Statement [closed]
This is some of the strangest behavior I've ever seen and I have no answer from myself.
I tried -fno-delete-null-pointer-checks while compiling both my game and engine and still the same behavior was ...
0
votes
1
answer
95
views
An inclusion of a null pointer is making content of another pointer to have a value (most probably garbage)
I am currently learning pointers in c. I have learned that if I do not assign an address to a pointer and try to print the value of the content of the pointer, then the program will not run properly.
#...
0
votes
2
answers
128
views
GCC: null-pointer-checks and undefined-behaviour
The following code does contain UB.
The gcc docu says, that the compiler can assume a pointer is non-null after it has been dereferenced. So option <1> and <2> should lead to the same ...
0
votes
1
answer
60
views
Nested strtok() calls to tokenize given string does not work as expected [duplicate]
I want to tokenize a provided input string using strtok(). In the first step, I want to tokenize the given string by "|" symbol. Then, I tokenize each substring by ";". Finally, I ...
2
votes
1
answer
119
views
C: NULL > NULL always false?
Is it guaranteed by the C standard that, given type_t* x = NULL; type_t* y = NULL; we always have x > y evaluating as false? I ask because of the conversation in initial or terminal malloc buffer ...
-1
votes
1
answer
376
views
File pointer null in c
I am a learning file handling in C. I tried implementing a program but no matter what I do the file pointer is still null.
I checked the spelling, the directory, even tried adding and removing .txt ...
-2
votes
1
answer
54
views
Dereferencing null pointer warning and code run stuck infinitely [closed]
So i am trying to solve a problem to reverse a linked list and following is what i came up with.
class Solution
{
public:
/* void insertList(struct Node* head, int data)
{
Node* temp{ ...
18
votes
6
answers
5k
views
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 ...
-1
votes
3
answers
114
views
cpp, "\0" == NULL returns false? How to figure it is null ptr or not? [closed]
I am getting these results. What am I doing wrong?
const char *c = "\0";
cout << (c == NULL); // false
cout << (c == nullptr); //false
4
votes
0
answers
50
views
Is adding 0 to a null pointer UB? [duplicate]
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 ...
2
votes
3
answers
540
views
Is there a std:: function which is usable as a predicate (similar to std::is_null_ptr)?
I had hoped for a short, concise, elegant:
std::array<int*,10> ip_arr;
bool all_null = std::all_of(ip_arr.begin(),ip_arr.end(),std::is_null_ptr_as_fn);
instead of inventing a lambda for that ...
0
votes
1
answer
448
views
Best way to assert that a value is not null
This is my method.
public boolean authenticateAndPollCallbackResult(BankIdAuthRequest bankIdAuthRequest) {
ResponseEntity<BankIdAuthResponse> authResponse = bankIdAuthentication(...
0
votes
0
answers
272
views
Can static_cast gives nullpt for non-nullptr argument? [duplicate]
The following code gives me a warning "null pointer dereference" despite checking the pointer value before casting:
struct ID { virtual ~ID() = default; };
struct IF { virtual void g() = 0; }...
-1
votes
2
answers
286
views
why we use two conditions in head!=nullptr && head->next != nullptr in floyd cycle detection algorithm?
i want to know that why we use two conditions
i am confused.. while head and head->next are not equal to null which means head and head's next will point to null ho is that possible
int detectCycle(...
5
votes
2
answers
540
views
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 ...
0
votes
1
answer
161
views
how to fix a Runtime error in a char array
I am currently working on a program and have run into a runtime error, ironically I cannot seem ti find the source of it at all, I have tried playing around with the char[] size, but since the problem'...
5
votes
2
answers
715
views
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 (<struct type1> *c, <struct type2> **a)
1186 {
...
...
...
...
1196 b = *a;
1197 if (...
-1
votes
4
answers
1k
views
empty char array, the ways I check it are the same?
I have a char array and I want to check if it is null.
if(_char_array[0] != '\0') {...}
is the same as
if(_char_array != '\0') {...}
thanks
3
votes
0
answers
260
views
ReactEditText.java , Fatal Exception: java.lang.NullPointerException
Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res....
8
votes
2
answers
2k
views
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 ...
0
votes
0
answers
95
views
Why does it iterate one more time even if it is NULL?
Similar question here .
What I want to understand is that when current_node is NULL, the compiler doesn't see it as NULL and iterates one more time.
void printBranches(struct bank *head) {
...
1
vote
1
answer
115
views
Pointer to Value of another pointer in C (Embedded)
In this code: Pointer_pu32 points to variable a, but which is pointer Boot_st point to?
int * Boot_st;
int a = 15;
int * Pointer_pu32 = &a;
Boot_st = (int *)(*Pointer_pu32); /* what was this ...
0
votes
0
answers
16
views
I keep getting NullPointerException on CashRegisterClass [duplicate]
I keep getting NullPointerException on all my Tests.
I have a CashRegister Class with a Product object lastScannedProduct that is supposed to be initially null but then it's supposed to be changed I ...