There is something strange with the c++ "delete" command [duplicate] - c++

This question already has answers here:
Pointers in c++ after delete
(3 answers)
Closed 1 year ago.
If deleting the pointer, why is the output 5 5?(I'm new)
#include <iostream>
using namespace std;
int main(){
int* i = new int(5);
cout<< *i <<endl;
delete i;
cout<< *i <<endl;
return 0;
}

Delete doesn't set 0 or any value to the memory i is pointing to. It just flags it as free so something can use it later. This leads to undefined behaviour

Related

Segmentation fault when deleting template array [duplicate]

This question already has answers here:
Is delete[] equal to delete?
(6 answers)
delete vs delete[] [duplicate]
(4 answers)
C/C++ delete vs delete[] [duplicate]
(1 answer)
What is the array form of 'delete'?
(6 answers)
Closed 2 years ago.
So i need to create an array of dynamic size containing pairs of objects and I have the following piece of code:
#include <utility>
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hey #1" << endl;
pair<string, int> *array;
array = new pair<string, int>[4];
cout << "Hey #2" << endl;
delete array;
cout << "Hey #3" << endl;
}
and the output I get is
Hey #1
Hey #2
Segmentation fault (core dumped)
which means that something goes wrong when the delete operator gets called?
What am I missing here? Any help would be appreciated!
You need to use the operator delete []
delete [] array;
When deleting a dynamic array use delete[] instead of delete to delete multiple dynamically allocated data. Hope this solves your problem.
When you allocate objects with new, you need to free them with delete; when you allocate objects with new[] you need to free them with delete[]. Change your code to:
delete[] array;

Is this a Segmentation fault or not? [duplicate]

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 2 years ago.
Is this a segmentation error or not? Why not? Please answer me, why this doesn't give me segmentation error?
#include <iostream>
using namespace std;
int main()
{
int ** T;
T = new int*[5];
for(size_t i=0; i<5; i++){
T[i] = new int[5];
}
T[4][7] = 10;
return 0;
}
Looks OK to me except for the 7 at the end. T is a pointer to pointer to ints, and it's assigned to an array of 5 pointers to ints (OK so far). Then each member of that array is assigned to a pointer to 5 ints. Still fine. Lastly, T[4] is a pointer to ints, but only 5 are assigned. So T[4][7] goes off the end of the array.
Now, will this actually segfault? That depends on memory alignment. If that address, &T[4][7] is on a different memory page than the actual allocated array, you'll get a segfault. Otherwise, you're just reading unallocated memory and you get whatever's there.

calling delete on the shallow copy not blow up [duplicate]

This question already has answers here:
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 4 years ago.
In the below code, memory is allocated for an integer and later a shallow copy is being made and finally delete is being called on it. How does it still print 23 as the output and why doesn't the delete call on q, cause a run time exception.
#include <iostream>
using namespace std;
int main() {
int* p = new int(23);
int* q = p;
delete p;
cout << *p << endl;
delete q;
return 0;
}
Undefined behaviour means anything can happen.
It might crash.
It might crash your car.
It might crash your brain.
It might crash Sagittarius A* into your brain.
It might crash your brain into your car, then crash them both into Sagittarius A*.
It might appear to work.
But it's still undefined.
Don't expect results.

Deleting part of array not allowed, why? [duplicate]

This question already has answers here:
Can I delete[] a pointer that points into an allocated array, but not to the start of it?
(7 answers)
Why can't I delete pointer alone from an array of objects?
(4 answers)
Closed 5 years ago.
Consider, below program which gives runtime error. Point of this question is to understand memory view and management.
#include<iostream>
using namespace std;
int main(void) {
char* arr = new char[10];
char* ptr = NULL;
for(int i = 0; i < 10; i++) {
arr[i] = 'a';
}
cout << arr;
ptr = &arr[5];
delete ptr;
cout << arr;
return 0;
}
new allocates a block of memory. You can free that memory using delete, but you must pass the same address that was returned by new. That's how it works. You can't pass arbitrary addresses to delete.
Another option is to use malloc() and free(). These are older function but then you can also use realloc() to resize the memory. Then, if you want to delete part of the array, you can resize it to be smaller. BUT... you must still copy any data as needed to correctly form the resized array. That is not automatic.

What does the operator delete[] do? [duplicate]

This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A{
public:
int s;
// ~A(){}
};
int main(){
A *c = new A[10];
delete c;
return 0;
}
Code above can run successfully, but when i code will get an error. Who can tell me why?
The behaviour of your code is undefined.
You must write delete[] c; if c is a pointer to memory allocated with new[].
(Interestingly, some compilers sort out this mess for your, but don't rely on that since then you're not writing portable C++).