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;
Related
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
This question already has answers here:
Are data members allocated in the same memory space as their objects in C++?
(6 answers)
Closed 4 years ago.
how does tmp get memory from the machine, from heap or stack?
I thought it was from the stack, but it seem that the code can run properly
#include<bits/stdc++.h>
using namespace std;
struct node {
int a[1000000];
};
int main() {
node tmp;
memset(tmp.a, -1, sizeof(tmp.a));
cout << tmp.a[0];
return 0;
}
In stack, since it's an automatic variable to the main function.
PS: This code doesn't compile, for example with this error: error: type 'node' does not provide a subscript operator: cout << tmp[0];.
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.
This question already has answers here:
How serious is the new/delete operator mismatch error?
(4 answers)
Closed 6 years ago.
Consider the following piece of code:
#include <iostream>
#include <limits>
using namespace std;
struct my{
int a;
~my(){
cout << "d\n";
}
};
int main(){
my* a = new my[100];
// cout << (void*)a << " " << (void*)&(a[0]) << endl;
delete a; // I know I should use delete [],
// but just to find out what would happen if I do this.
}
The program is printing d and then gives a segmentation fault. The implementation of delete assumes that an area of size sizeof(type) has been allocated and that much memory has to be freed. Whereas delete [] first reads the number of elements which has been allocated. I found that most implementations store this number n in the beginning of the allocated memory. delete [] fetches n from the beginning and the goes on to free n * sizeof(type) bytes of allocated memory. I don't understand why the above program gives segfault. I tried printing the addresses a and &(a[0]) and they are exactly the same, so any possibility that delete was trying to clear the area where n was stored is simply not true. Can someone please tell what is going on?
Note: This does not happen when we allocate arrays of basic types like int, double etc.
The behaviour on using delete on a pointer acquired by new[] is undefined. (Some C++ runtime libraries are implemented in such a way that an array of plain old data types is deleted correctly when you write delete).
Really there is nothing else to say.
what was allocated with new must be freed by delete, what was by new[] must be freed by delete[].
don't mix new for allocating and another APIs to free like Windows GlobalFree().
to delete correctly a:
delete[] a;
if you call delete in a dynamic array will cause an undefined behaviour
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
Here is a simple of example of a question I have:
I create a pointer of integer (value 5), I print the pointer (hence the address) of the memory case it points to, as well as its content (5).
Then I delete that pointer, which is supposed to delete the content in the memory case that has the address of the pointer.
But then when I print a and its content, as expected the address still exists. Nevertheless the content remains the same (5) as if a was not deleted...
could you explain me? :-)
#include <iostream>
int main()
{
int * a = new int(5);
std::cout<< a << std::endl;
std::cout<< "--------" << std::endl;
delete a;
std::cout<< a << std::endl;
std::cout<< *a << std::endl;
// end of my main
return 0;
}
result:
0x7fff28403a90
--------
0x7fff28403a90
5
It may or may not print the same value after delete operation. So simply what you are observing is an undefined behavior.