377 questions
10
votes
1
answer
741
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 ...
3
votes
1
answer
105
views
Is it defined behaviour to compare two null pointers for order
I have implemented begin and end functions for MFC containers. The iterators are pointers to the internal data and may be null pointers for an empty container. What happens if both are null pointers ...
2
votes
1
answer
154
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 ...
5
votes
2
answers
195
views
Is dereferencing a volatile null pointer value undefined behavior?
It seems unclear, but there are claims that dereferencing a nullpointer is undefined behavior:
A comment by M.M.
This note was in fact removed as the result of DR 1102 with the stated reasoning being ...
0
votes
1
answer
126
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 ...
1
vote
2
answers
167
views
C23 and memory representation of nullptr
I'm reading the C document n3042 Introduce the nullptr constant
and when it enumerates the properties of nullptr there is the following:
In memory, nullptr is represented with the same bit-pattern as ...
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.
#...
7
votes
2
answers
207
views
Is a non-constant zero integer cast to `void *` still a null pointer?
The expresssion (void *)0 is called a null pointer.
But how about the following:
int i = 0;
void *s = (void *)i;
Is s also a null-pointer? The C-language standard says:
6.3.2.3 Pointers
3 An integer ...
0
votes
2
answers
137
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
0
answers
46
views
defining the start of a string in a CSV file to guarantee proper comparison
Im reading information from a CSV file and im getting random symbols at the start of my first input, my understanding is that compiler doesn't know where the start of the string is so looks for the ...
0
votes
1
answer
61
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 ...
38
votes
2
answers
3k
views
Why isn't the keyword false an integer constant expression in gcc C23?
Latest gcc 13.x (trunk) gives a compiler error (gcc -std=c23) for this code:
int* p = false;
error: incompatible types when initializing type 'int *' using type '_Bool'
How can this be correct?
C23 ...
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 ...
5
votes
2
answers
670
views
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 ...
-1
votes
1
answer
379
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 ...
1
vote
1
answer
61
views
Defining a Macro having struct argument
I have a struct named Row :
typedef struct
{
uint32_t id;
char username[COLUMN_USERNAME_SIZE];
char email[COLUMN_EMAIL_SIZE];
} Row;
Also, I have defined a Macro :
#define ...
-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{ ...
2
votes
0
answers
115
views
Android SDK emulator: crasher strlen-NULL
https://source.android.com/docs/core/tests/debug/native-crash says:
"You can reproduce an instance of this type of crash using crasher strlen-NULL."
How to do that actually? Open a "adb ...
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 ...
2
votes
1
answer
47
views
For loop segmentation fault
I am attempting to create a DVR simulation in C. I know that I need a nested for loop, but whenever I compile and run the program it produces a segmentation fault after entering the number of routers ...
-1
votes
3
answers
115
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
1
vote
1
answer
60
views
Why do I get null?
My console says
Math: 5.0
false
English: 3.0
true
Physics: null
false
Chemistry: null
false
This sound ok, but in my Code I had a Exception-Code. I do not understand why I get null here:
Physics: ...
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 ...
0
votes
1
answer
391
views
Why am I getting an "a.exe has stopped working" error on Visual Studio Code for my linked list program?
I'm playing around with a linked list, and I'm getting an error when I try to print out the value of the tail and the address of the next value:
struct Node {
int n;
Node *next;
};
class ...
2
votes
3
answers
541
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
126
views
fopen() returns NULL while running program from Comand Line
Any of my C programmes utilizing fopen() run perfectly from IDE or Windows environment, but fail when opened from Command Line (cmd.exe), as fopen("r") keeps returning NULL pointer. The ...
0
votes
1
answer
449
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
1
answer
2k
views
Potential null pointer dereference or compiler bug?
I found one more case when compiler thinks that static_cast may return nullptr on non-nullptr argument. It seems that this time there is no undefined behavior, like was before, but maybe I'm missing ...
0
votes
0
answers
274
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; }...
70
votes
9
answers
9k
views
Why are there two ways of expressing NULL in C?
According to §6.3.2.3 ¶3 of the C11 standard, a null pointer constant in C can be defined by an implementation as either the integer constant expression 0 or such an expression cast to void *. In C ...
-1
votes
2
answers
287
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(...
1
vote
1
answer
64
views
Why can I assign NULL to a pointer in my main function, but with the exact same code not in a other function? [duplicate]
I am fairly new to C and wanted to create a linked list.
For the list-elements I created a structure and wanted to initialize the head element in a function. The last element of the list shall contain ...
5
votes
2
answers
547
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
0
answers
144
views
Are there any platforms with nonzero null pointer values? If so, how common are they? [duplicate]
I've given up here and I'm starting over with a clean new question 😭
Let me try to rephrase the question as objectively and simply as possible:
I want someone to either give me a list of platforms (...
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'...
0
votes
2
answers
77
views
Segmentation Fault While Manipulating String
I'm trying to create a Ceasar Cipher. For this test, I used a key of 1 and plain text of "hello." When doing so, I get the error message "Segmentation fault (core dumped)". I know ...
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 (...
0
votes
1
answer
106
views
Exception thrown: read access violation this. was nullptr [duplicate]
I tried to work with the points but nothing I do seem to work.
Essentially this will be run in main and a number n is inserted to find all the prime numbers from 0 to n. This is then written into a ...
-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
1
vote
3
answers
461
views
Last node is not printed in Linked List
I was trying to learn the Linked list and perform insertion operations from beginning of the list. while printing the nodes, the first node is not printed. Here is the core functions which I have ...
3
votes
0
answers
261
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....
1
vote
0
answers
102
views
Is null pointer a valid iterator? [duplicate]
I'm implementing std::vector-like class. Its iterators are just plain pointers. The problem is that if the vector is empty (there is no allocated buffer for elements) it returns nullptr as begin and ...
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 ...
1
vote
1
answer
124
views
what is the termination condition for this C for loop
i found the linked code and found this for loop which is a bit strange for me. i would appreciate if someone could explain me the syntax of this loop to me.
MFG
void patch(Ptrlist *l, State *s)
{
...
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) {
...
2
votes
1
answer
73
views
Trying to make a binary search tree, but I keep getting invalid memory address or nil pointer dereference errors
I'm trying to create a BST, add a node, and print the data in that node, but I keep getting invalid memory address or nil pointer dereference errors. I know something is wrong with my pointers, and I'...
1
vote
1
answer
116
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 ...
34
votes
3
answers
3k
views
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?
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 ...
1
vote
3
answers
303
views
Can the unary & operator yield the address 0 (null pointer)?
C2x, 6.5.3.2 Address and indirection operators, Semantics, 3:
The unary & operator yields the address of its operand.
A simple question: can the unary & operator yield the address 0 (null ...