delete-operator

Double free or corruption after queue::push

不打扰是莪最后的温柔 提交于 2019-12-17 04:22:43
问题 #include <queue> using namespace std; class Test{ int *myArray; public: Test(){ myArray = new int[10]; } ~Test(){ delete[] myArray; } }; int main(){ queue<Test> q Test t; q.push(t); } After I run this, I get a runtime error "double free or corruption". If I get rid of the destructor content (the delete ) it works fine. What's wrong? 回答1: Let's talk about copying objects in C++. Test t; , calls the default constructor, which allocates a new array of integers. This is fine, and your expected

C++ delete vector, objects, free memory

為{幸葍}努か 提交于 2019-12-17 03:51:43
问题 I am totally confused with regards to deleting things in C++. If I declare an array of objects and if I use the clear() member function. Can I be sure that the memory was released? For example : tempObject obj1; tempObject obj2; vector<tempObject> tempVector; tempVector.pushback(obj1); tempVector.pushback(obj2); Can I safely call clear to free up all the memory? Or do I need to iterate through to delete one by one? tempVector.clear(); If this scenario is changed to a pointer of objects, will

Is there any reason to check for a NULL pointer before deleting?

廉价感情. 提交于 2019-12-17 03:04:16
问题 I often see legacy code checking for NULL before deleting a pointer, similar to, if (NULL != pSomeObject) { delete pSomeObject; pSomeObject = NULL; } Is there any reason to checking for a NULL pointer before deleting it? What is the reason for setting the pointer to NULL afterwards? 回答1: It's perfectly "safe" to delete a null pointer; it effectively amounts to a no-op. The reason you might want to check for null before you delete is that trying to delete a null pointer could indicate a bug in

delete vs delete[] [duplicate]

ぐ巨炮叔叔 提交于 2019-12-16 22:12:31
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: ( POD )freeing memory : is delete[] equal to delete ? When I was taught C++, this was a long time ago. I was told to never use delete but delete[] as performing delete[] on a single object will be equivalent to delete . Knowing not to trust teachers too much I wonder, Is this true? Is there ever a reason to call delete instead of delete[] ? I've scanned the possibly related questions in SO, but haven't found any

How do I know if a pointer has been assigned data via 'new'?

丶灬走出姿态 提交于 2019-12-14 03:15:25
问题 Say I have a pointer like this: int *thingy; At some point, this code may or may not be called: thingy=new int; How do I know if I can do this: delete thingy; I could use a bool for every pointer and mark the bool as true whenever the I use new , but I have many pointers and that would get very unwieldy. If I have not called new on thingy , calling delete on it would likely cause a crash, right? I searched around quite a bit but could find no answer that clearly fit my situation. EDIT: I need

New and delete command of c++ in obj-c

穿精又带淫゛_ 提交于 2019-12-13 22:37:34
问题 I have an NSMutablearray of objects. the number of objects is set by user. in c++ I would use a for cycle and the 'new' command.something like this: int fromuser, a; for(a=0;a<fromuser;a++){ array addobject:(new class obj) } what do I need to do in obj c since there is no new? 回答1: You would utilize the alloc and init (or more specialized initializer) provided by NSObject. For example, something like the following should work: int fromuser, a; NSMutableArray objectArray = [[NSMutableArray

“delete” pointer without destroying data

天大地大妈咪最大 提交于 2019-12-13 22:02:21
问题 I'm relatively new to C++. I'm allocating a buffer: uint8 *buffer = new uint8[len]; Using a 3rd party library, I use a method of an "img" object (it's a picture) to "take over" the buffer as raw image data: img->SetBuffer((uint8*)data); I suspect that "taking over" in practice means that the "img" object has its own pointer which after "SetBuffer" points to the data in "buffer". It all works fine, but my compiler complains (it's a warning, not an error) about a memory leak. If I add this line

Delete an object from a vector

此生再无相见时 提交于 2019-12-13 09:06:51
问题 Step 1. Create an instance of a class Step 2. Push this instance to a vector Step 3. Call delete this; in a member method of an instance Step 4. Everything is Ok Step 5. Push something to the vector and get this *** glibc detected *** ./app: double free or corruption (fasttop): 0x0000000001017930 *** ======= Backtrace: ========= /lib/libc.so.6(+0x71bd6)[0x7f607d60cbd6] /lib/libc.so.6(cfree+0x6c)[0x7f607d61194c] ./app[0x40231c] ./app[0x402290] ./app[0x4053c0] ./app[0x4048fe] ./app[0x404246] .

C++: Remove element from dynamic struct array and shift other elements

一笑奈何 提交于 2019-12-13 08:28:15
问题 I have an array of structs. I am trying to delete a list of elements from that array and shift other elements to the left. After shifting the elements I am trying to delete/free the memory at the end of the array which we don't require anymore. I have the following code: #include <iostream> #include<stdio.h> #include<stdlib.h> void removeelement(int*); void displayelements(); typedef struct { int n; }element; element** array; int numofelements=5; int main() { array = (element**)malloc(5

How does delete[] know how much memory to delete? [duplicate]

☆樱花仙子☆ 提交于 2019-12-13 04:35:42
问题 This question already has answers here : How does delete[] “know” the size of the operand array? (9 answers) How does delete[] know it's an array? (16 answers) Closed 5 years ago . int* i = new int[4]; delete[] i; While we call delete[], how does the program know "i" is 4 byte-length. Is 4 be stored in somewhere in memory? The implementation of delete[] depend on System or Compilers? Is there some System API to get the length of i? As HadeS said, which will hold the information how much