delete-operator

Does Qt allready have its own new and delete operators?

自古美人都是妖i 提交于 2019-12-24 04:40:12
问题 I'm using a QGraphicsScene widget and showing upon it some points as QGraphicsRectItem . This means calling lots of new + addItem() when showing up, and removeItem() + delete to get rid of unused points. Of course, for performance issues, I've implemented my own new and delete operators, which basically recycle pre-allocated memory chunks. Those operators works very well with anything, except for Qt classes (I mean QObject derived classes). The error raised is different every time but always

How to use delete with a variable pointed to by two pointers?

走远了吗. 提交于 2019-12-23 12:54:37
问题 Let say I have a hypothetical pointer declared with new like so: int* hypothetical_pointer = new int; and create another hypothetical pointer, with the same value: int* another_hypothetical_pointer = hypothetical_pointer; If I were to go about deleting these, which were declared with new, would I have to delete both pointers, or only the one explicitly declared with new? Or could I delete either pointer? 回答1: delete destroys the dynamically allocated object pointed to by the pointer. It doesn

Why does c++ have its separate syntax for new & delete?

梦想的初衷 提交于 2019-12-23 12:13:52
问题 Why can't it just be regular function calls? New is essentially: malloc(sizeof(Foo)); Foo::Foo(); While delete is Foo:~Foo(); free(...); So why does new/delete end up having it's own syntax rather than being regular functions? 回答1: Here's a stab at it: The new operator calls the operator new() function. Similarly, the delete operator calls the operator delete() function (and similarly for the array versions). So why is this? Because the user is allowed to override operator new() but not the

Deleting vector of pointers

ぃ、小莉子 提交于 2019-12-23 07:39:38
问题 I need to create pointers of instances of a class, and the program do not know at compilation time how many pointers I will create. For deletion, I was considering storing the pointers in a vector, and then deleting them one by one. Would the use of smart pointers a cleaner way to go ? And if one does not want to use smart pointers, would this use of vector be considered clean ? Minimum code: #include <vector> using namespace std; class Foo { public: Foo(); }; Foo::Foo(){} void

How to free memory of dynamic struct array

假装没事ソ 提交于 2019-12-23 05:39:04
问题 As someone who never dealt with freeing memory and so on, I got the task to create a dynamic array of struct and create functions to add or delete array elements. When deleting I have to free the memory which is no longer necessary. when deleting the 2nd element of an array of the size of 3, I move the 3rd element to the 2nd position and then delete the last one. When deleting the last one, I always get an error... Is there anyone who can find an solution for me? struct myFriend { myFriend()

Destructor of class with pointer array C++

蓝咒 提交于 2019-12-22 08:26:07
问题 If I have a class with an array of pointers to another class Vehicle : class List { public: //stuff goes here private: Vehicle ** vehicles; } If I now write the destructor of the class List , do I manually iterate over the array (I know how many items are in the array) and delete every pointer to a vehicle, or will C++ automatically call the destructors of all the Vehicles in the array? (Like it does if there's a private string/... in the class or if it would be a STL container of Vehicle

How to free memory in try-catch blocks?

邮差的信 提交于 2019-12-20 09:29:38
问题 I have a simple question hopefully - how does one free memory which was allocated in the try block when the exception occurs? Consider the following code: try { char *heap = new char [50]; //let exception occur here delete[] heap; } catch (...) { cout << "Error, leaving function now"; //delete[] heap; doesn't work of course, heap is unknown to compiler return 1; } How can I free memory after the heap was allocated and exception occurred before calling delete[] heap ? Is there a rule not to

Delete a pointer to pointer (as array of arrays)

落爺英雄遲暮 提交于 2019-12-20 08:41:50
问题 I have this in my code: double** desc = new double* [size_out]; for (int i = 0; i < size_out; i++) desc[i] = new double [size_in]; How do I delete this desc ? Should I do: delete [] desc; or for (int i=0; i<size_out; i++) delete [] desc[i]; delete [] desc; or for (int i=0; i<size_out; i++) delete [] desc[i]; delete desc; ? 回答1: Simple rules to follow: for each allocation, there has to be a deallocation (ex1 is therefore wrong) what was allocated using new should be freed using delete , using

C++ Array: why is delete[] not working? [duplicate]

佐手、 提交于 2019-12-20 07:52:41
问题 This question already has answers here : C++ delete - It deletes my objects but I can still access the data? (13 answers) Closed 5 years ago . When I run the following code: #include <iostream> using namespace std; main(){ //declare: char* TestArray = new char[16]; for(char i=0; i<16; i++){ TestArray[i]=rand() % 10; } //output Array: for(char i=0; i<16; i++){ cout << int(TestArray[i]) << " "; } //delete it: delete[] TestArray; //output result: for(char i=0; i<16; i++){ cout << int(TestArray[i

Deleting an Array of Pointers - Am I doing it right?

坚强是说给别人听的谎言 提交于 2019-12-19 10:39:09
问题 I feel a little stupid for making a question about the deletion of pointers but I need to make sure I'm deleting in the correct way as I'm currently going through the debugging process of my program. Basically I have a few arrays of pointers which are defined in my header file as follows: AsteroidView *_asteroidView[16]; In a for loop I then initialise them: for(int i = 0; i < 16; i++) { _asteroidView[i] = new AsteroidView(); } Ok, so far so good, everything works fine. When I eventually need