Is deleting a null pointer in C++ considered undefined behaviour? [duplicate] - c++

This question already has answers here:
Is it safe to delete a NULL pointer?
(8 answers)
Test for void pointer in C++ before deleting
(3 answers)
Closed 8 years ago.
Consider the following piece of code:
int main() {
int *i = nullptr;
delete i;
}
Questions:
Is deleting a null pointer considered undefined behaviour?
Did something changed in C++11 or C++14?

Is deleting a NULL pointer in C++ considered undefined behaviour?
No, this is perfectly legal operation.
From N3242, [expr.delete]
the value of the operand of delete may be a null pointer
value

Related

Dereferencing a function pointer, C++ [duplicate]

This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Calling a function through a function pointer - dereference the pointer or not? What's the difference?
(2 answers)
Closed 12 months ago.
what's the difference between the second and third line:
int (*myFunction)() = fooFunction;
(*fooFunction)();
fooFunction();
Why do both do the same thing? Is it just how C++'s syntax is? I thought that a function pointer needs to be dereferenced in order to call it. How does the third line work, it isn't dereferenced.

Is nullptr the same as zero? [duplicate]

This question already has answers here:
What exactly is nullptr?
(14 answers)
Do you use NULL or 0 (zero) for pointers in C++?
(21 answers)
What is the difference between NULL, '\0' and 0?
(11 answers)
Closed 3 years ago.
I am currently learning C++ and have gotten to pointers. I am struggling to understand the difference between initializing a pointer to zero and initializing it to nullptr, as demonstrated below.
int *p = 0;
int *p1 = nullptr;
My understanding is that p and p1 are both null pointers. Does this mean that nullptr is equivalent to zero, and that both p and p1 are pointing to the same address (that is, the address for zero)?
If they are the same, why would I want to use nullptr when I could just use zero? If they aren't the same, why is it useful to have multiple ways to declare a null pointer instead of one way?

Memory occupied by reference to a variable in cpp [duplicate]

This question already has answers here:
How does a C++ reference look, memory-wise?
(9 answers)
Closed 5 years ago.
Declaring pointer:
int a;
int *x=&a;
x occupies 8 bytes of memory
Likewise if we declare a reference to a variable:
int a;
int &x =a;
How much memory does the the reference to a occupy?
That's undefined. Not as in Undefined Behavior, but the question simply doesn't have an answer.

Are nullptr references undefined behaviour in C++? [duplicate]

This question already has answers here:
Assigning a reference by dereferencing a NULL pointer
(5 answers)
Closed 9 years ago.
The following code fools around with nullptr pointer and reference:
#include <cstdio>
void printRefAddr(int &ref) {
printf("printAddr %p\n", &ref);
}
int main() {
int *ip = nullptr;
int &ir = *ip;
// 1. get address of nullptr reference
printf("ip=%p &ir=%p\n", ip, &ir);
// 2. dereference a nullptr pointer and pass it as reference
printRefAddr(*ip);
// 3. pass nullptr reference
printRefAddr(ir);
return 0;
}
Question: In C++ standards, are commented statements 1..3 valid code or undefined behavior?
Is this same or different with different versions of C++ (older ones would of course use 0 literal instead of nullptr keyword)?
Bonus question: are there known compilers / optimization options, which would actually cause above code to do something unexpected / crash? For example, is there a flag for any compiler, which would generate implicit assertion for nullptr everywhere where reference is initialized, including passing reference argument from *ptr?
An example output for the curious, nothing unexpected:
ip=(nil) &ir=(nil)
printAddr (nil)
printAddr (nil)
// 2. dereference a nullptr pointer and pass it as reference
Dereferencing a null pointer is Undefined Behaviour, and so whether you pass it as a reference or by value, the fact is that you've dereferenced it and therefore invoked UB, meaning from that point on all bets are off.
You've already invoked UB here:
int &ir = *ip; //ip is null, you cannot deref it without invoking UB.
Since ir is just a shadow of *ip it will not cause an undefined behavior on its own.
The undefined behavior is using a pointer which points to nullptr_t. I mean using *ip. Therefore
int &ir = *ip;
^^^
Causes an UB.

NULL pointer and deleting it [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there any reason to check for a NULL pointer before deleting ?
Is there a bug in this code
Type * p = 0;
delete p;
p is a null pointer here. will there be any issues in implementing this.
No, it's specified in the C++ specification that delete on a null pointer does nothing.