Questions tagged [delete-operator]
In the C++ programming language, the delete operator calls the destructor of the given argument, and returns memory allocated by new back to the heap.
1,273
questions
-1
votes
1
answer
44
views
Why runtime error when using delete to free memory [duplicate]
I think it has to do with memory allocation issues, but there is a destructor there to free the memory of the object t of type A.
#include <stdio.h>
struct A {
int* i;
A() { i = new int[...
0
votes
2
answers
71
views
How to fix memory leak
I am working on the project which using pointer (new ext...) and I don't know how to fix it, I couldn't use the delete syntax because it will break the code literally.
list<Virus*> DoClone()
{
...
-2
votes
1
answer
60
views
Is there a reason why the delete[]p; will not delete my array values? [duplicate]
I am currently using C++ on VScode with C/C++ and CodeRunner application. For some reason, even though adding the delete[]p in my code, the values in the following array remain there. Can someone ...
0
votes
0
answers
35
views
why the second new failure cannot invoke the override delete?
class Test {
public:
Test() {
throw "error";//throw exception deliberately
}
void operator delete(void* ptr, string msg) { // 0
cout << msg << endl;
...
3
votes
3
answers
87
views
How is the size of a polymorphic object deduced in a delete operation?
I know that there are a lot of similar questions on SO related to what I am about to ask, I've read many of them and still feel a bit vague, so I decided to ask this question.
Given the following code:...
1
vote
1
answer
39
views
Reverse linked list in a function, that also has to work with a linked list
I had to create a function, that deletes nodes that have a greater element to the right. I achieved this by creating 2 functions:
1st function reverses a list
2nd function that deletes nodes, that ...
3
votes
3
answers
100
views
Deletion on a non pointer array in c++
When I have an array like this:
int* test = new int[50];
for (int i = 0; i < 50; i++)
{
test[i] = dist4(rng);
}
(Filled with random numbers for testing)
I can free the memory like this:
delete[...
-1
votes
0
answers
49
views
C++ Error deleting an instance of an class with inheritance (Base class has no virtual members)
Here is a base class with 2 inheritors.
If I create a virtual method first on the first inheritor, I get an error when I delete the instance later (Base pointer).
If the base class has any virtual ...
-1
votes
0
answers
44
views
can help me to fix valgrind memory leak
I have total 5 files and I got the output I want but the problem is at the end I got memory leak problem
So here is My First File
main.cpp (1/5)
#include <iostream>
#include "BarChart.h&...
1
vote
1
answer
53
views
Deleting all rvalue function overloads of a class
Say I have a class object that must be captured by the caller when returning this class's object from a function call.
// no_can_rvalue *must* be captured
[[nodiscard]] no_can_rvalue a_func();
I can ...
0
votes
1
answer
49
views
How do I actually delete the node(s)?
I am solving a question on LeetCode.com:
Given the root of a binary tree, collect a tree's nodes as if you were doing this:
a. Collect all the leaf nodes.
b. Remove all the leaf nodes.
c. Repeat ...
2
votes
1
answer
104
views
Why does gcc use the size-aware delete operator by default when optimizing?
If I define my own new and delete operators as shown below:
#include <cstdio>
#include <cstdlib>
#include <new>
void* operator new (size_t count)
{
printf("Calling custom ...
0
votes
0
answers
63
views
Why am I getting an error about placement new when I'm not using placement new?
I'm getting this error:
error: exception cleanup for this placement new selects non-placement operator delete [-fpermissive]
ptrCpMsgHandler = new cpMsgHandler;
Isn't this just a plain old 'new'? ...
0
votes
0
answers
16
views
MongoDB delete operation doesn't work after firebase deploy
MongoDB delete operation doesn't work after firebase deploy. But works on a local host. I checked a few more times on my local computer it is working. But it doesn't work when I deploy.
I use Heroku ...
0
votes
0
answers
19
views
Overload Resolution with Deleted Constructor C++ [duplicate]
I'm sure this question exists elsewhere, but can't find an answer. I've noticed that when a constructor is defined as deleted, it makes that type take part in overload resolution. In other words, if ...
-1
votes
2
answers
71
views
Undefined behaviour of delete operator
I am relatively new to C++ and I'm learning about pointers. I was trying to dynamically allocate some memory for an array and found this issue.
Here is my code,
#include <iostream>
int main(){
...
0
votes
1
answer
102
views
How to use delete[] in destructor
I have the following files for implementing a Bucket class. However I can't destroy the _str member in the destructor of the Buckets produced by the operator+. The error that I get is:
heap ...
0
votes
1
answer
22
views
Delete the created progress bars by pushing button on the console window
I am trying to learn myself use Qt so still struggling a lot... How would I go about deleting my bouncing object by pressing the delete button? I am struggling to implement the delete function...
...
-1
votes
1
answer
42
views
Deleting a pointer to a pointer causes "Invalid address specified to RtlValidateHeap" error
I was solving a simple math question which needs to add solutions continuouslly to the answer set. So I designed a class with a pointer to a pointer in it. Whenever a new solution is worked out, I ...
0
votes
2
answers
72
views
How to delete and reassign a dynamically allocated pointer
I'm taking a c++ programming course (we are still mostly using C) and we just got to dynamic allocation of memory. For one of my homeworks, I'm asked to create a function that transposes any given ...
-2
votes
1
answer
69
views
Why can't i delete [] tmp after using tmp to push back new element to another original array
I am in my 1st year in CS ,in the middle of an OOP course.We have started learning about dynamically allocated arrays in C++. And we are now solving a lot of problems, where we have to push back an ...
0
votes
1
answer
51
views
Function to delete dynamically allocated 2D array
Quick question here. Using this func to allocate memory for an arr:
int **createDynamicNumArray() {
int size;
cout << "Enter size: " << endl;
cin >> size;
int **numArr = ...
1
vote
1
answer
93
views
OOP C++ language
I started learning OOP in C++. I try to solve a task like this:
Create a class - a list based on a one-size-fits-all array of integers. Assign a constructor, a destructor, the functions of adding an ...
0
votes
1
answer
56
views
Should we use delete[] or delete on function that return allocated memory pointer?
Should i use delete[] or delete for Register() example below? We hit some memory leak issue and legacy code which is not allow to change the return type and input of the function. I understand that ...
0
votes
0
answers
39
views
Doubling dynamically allocated memory in a function using loops and conditions C++
So I need to add already existing films to a Listfilms function that allocates the films dynamically, and if there is no more space we have to double the allocated memory. How do I use pointers and &...
3
votes
1
answer
62
views
Pointer still able to call member function, after it was set to NULL and delete being called on it [duplicate]
#include<stdio.h>
class test2
{
public:
void testFunc()
{
printf("test");
}
test2(){}
~test2(){}
};
class test1 : test2
{
public:
test1(){
...
-4
votes
1
answer
92
views
How to use new & delete operators?
New & Delete Operators
In the Visual Studio Code editor, I tried to use the new and delete operators, but the editor throws an error which I am unable to figure out. I tried out everything to ...
1
vote
1
answer
86
views
Problems freeing memory in a copy constructor
I have this parent class
class Monster
{
char* nume;
double hp;
float* dmgAbilitati;
int nrAbilitati;
}
with this copy constructor
Monster(const Monster& src)
{
if (...
0
votes
1
answer
169
views
Secure way to sanitize an object (javascript)?
In order to avoid the JavaScript delete operator (ref:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/delete) I am currently using object destructuring to get rid of private ...
1
vote
1
answer
43
views
CRT Detected that the application wrote to memory after end of heap in C++
I have created a class of MyString. My code was working just fine but when I wrote the destructor for my class it is giving me the error at delete keyword. Please help me out where is the problem and ...
0
votes
0
answers
46
views
Pointer type object of one class fails to give output when declared in other class which doesnot inherit it
This is the code. The program terminates with no output. I searched for it a lot here and on other platforms but all I get is a problem while freeing the pointer.
I am freeing the memory in the ...
7
votes
0
answers
77
views
Extra bytes allocated for custom class array when destructor defined
I was trying out the code shown below:
class Example {
public:
Example(int val = 0) : m_val(val) { }
~Example() { }
void* operator new (std::size_t sz) {
std::cout << "...
3
votes
0
answers
103
views
Error when using the delete array operator on a CDBVariant array
I create, use, and delete an array of CDBVariant :
CDBVariant *myVars = new CDBVariant[N];
// ...
delete[] myVars;
On the delete[] line the execution meets 3 breakpoints I can't explore, then Access ...
0
votes
3
answers
76
views
`double free or corruption (out)` error on a stack QDialog with the `WA_DeleteOnClose` attribute set
Given the following fragments of code :
class MyDialog : public QDialog
{
...
};
MyDialog::~MyDialog()
{
qInfo() << "~MyDialog()";
}
and
// scope begins
MyDialog d;
d....
0
votes
1
answer
137
views
Why would you ever use heap allocation for objects you will reference through an std::vector?
I'm going through some code from this article about ECS-systems in game programming and trying to understand it, and something I'm seeing a lot is using heap memory in places where it seems like ...
0
votes
1
answer
84
views
Inexplicable free(): invalid size in delete[] in C++
I use the exact same 2d array (it's a field of the class) in both cases. The second one lsh_distances won't let me delete[] the inner 1d arrays in the destructor and gives an error of free(): invalid ...
1
vote
1
answer
67
views
C++ heap corruption when delete is called on char array
hello I cannot understand why this is leading to when I delete this char buffer that the program is returning heap corruption. I made sure I am calling delete correctly for the correct type. I can ...
1
vote
2
answers
157
views
free(): double free detected in tcache 2 on calling overloaded assignment operator
I'm working on a university project where we are to implement some of the c++ string class as Mystring. I'm working on the overloaded assignment operator and this is the current code for it:
Mystring&...
1
vote
2
answers
111
views
Should I use the "delete" for the object member which initialized by "new" operator in constructor?
I have a question about new and delete:
Should I use delete for the input parameter or member object, e.g.:
https://github.com/jwbecalm/Head-First-Design-Patterns-in-CPP/blob/main/ch01_Strategy/main....
-1
votes
1
answer
100
views
Should I delete variables if created them using "new" operator inside map::insert()? [duplicate]
I am writing scene manager which, among other things, contains a map of all objects, or rather pointers to them. I remember to delete my variables created using "new" and i tried doing it in ...
0
votes
0
answers
36
views
C++ how to implement delete operator to deallocates memory depends on context
I have a code which is like this
std::vector<Snake*>body;
and I assign something tu first element of body
body.push_back(new Snake(sf::Vector2u(1, 0)));
and then when I do not want to use it. ...
1
vote
1
answer
90
views
A misunderstanding with polymorphism and virtual destructors
Example:
class Base {
public:
virtual void f() = 0;
virtual ~Base() { std::cout << "Base::~Base()\n"; }
};
class Derived : public Base {
public:
void f() { }
~...
0
votes
0
answers
48
views
Are all the memory Dynamically Allocated by "new" being properly cleared in the following implementation
I am in a situation where I need to create a collection of classes which perform computations over Eigen Matrices. The original code is quiet big and complex to explain, so I have implemented a ...
-1
votes
1
answer
297
views
Default copy constructor
I came across the idea of a copy constructor and using a pointer as a member variable of the class. The lecturer told me that if we use the default copy constructor to create an instance for a class ...
1
vote
1
answer
71
views
Why is a pointer to pointer treated differently than a pointer in spite of corresponding corrections
I'm trying to delete a Node from a Doubly Linked List. The function deleteNode() will receive the head of the linked list and the node to be deleted. However, depending on whether I pass the node to ...
-1
votes
1
answer
49
views
PPP Stroustrup exercise - copy a C-style string into memory it allocates on the free store [duplicate]
I'm solving the following exercise (17.4) from Stroustrup's PPP book:
Write a function char* strdup(const char* ) that copies a C-style string into memory it allocates on the free store. Don't use ...
1
vote
2
answers
392
views
free: invalid pointer when deleting a dynamically created array
I'm currently practicing pointers arithmetics and I can't figure out why this code ends on a "free(): invalid pointer" error when it comes to executing the last line with delete [] arr;.
I ...
4
votes
3
answers
165
views
How do you safely clear an object from memory (with attributes) which was created using the new keyword?
As far as I know on this topic, every "new" call needs a corresponding "delete" call to that object. So is this really correct?:
using namespace std;
class Box {
public:
...
1
vote
0
answers
171
views
Cannot see a global variable declared by var/let under globalThis [duplicate]
In D.Flanagan's 'Javascript the Definite Guide' there's an example:
var x = 1;
console.log(delete globalThis.x)
When I console log this code in node v14.15.1 then instead of false I get true....
0
votes
0
answers
125
views
free(): double free detected in tcache 2, composition
I am trying to simulate a simple 2-level paging mechanism, which dynamically creates new entries into the page tables. I am facing extra memory freed errors, when I try to run it with Valgrind and ...