delete-operator

Address held by pointer changes after pointer is deleted

半腔热情 提交于 2019-12-13 02:29:34
问题 In the following code, why is the address held by pointer x changing after the delete ? As I understand, the delete call should free up allocated memory from heap, but it shouldn't change the pointer address. using namespace std; #include <iostream> #include <cstdlib> int main() { int* x = new int; *x = 2; cout << x << endl << *x << endl ; delete x; cout << x << endl; system("Pause"); return 0; } OUTPUT: 01103ED8 2 00008123 Observations: I'm using Visual Studio 2013 and Windows 8. Reportedly

Delete an array of queue objects

倾然丶 夕夏残阳落幕 提交于 2019-12-12 19:17:53
问题 I am working on an object that contains an array of queues with an array length that isn't decided until the constructor is called. Basically it looks something like the following #include <queue> class myClass{ public: //public functions private: //private functions and variables queue<int>* myQueue; }; it is initialized like so: myClass::myClass(int numOfQueues){ myQueue = new queue<int>[numOfQueues]; } This all works beautifully, it seems. it functions exactly like I was hoping it would,

Calling delete on variable allocated on the stack

狂风中的少年 提交于 2019-12-12 16:12:27
问题 Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; } 回答1: No, it is not safe to call delete on a stack-allocated variable. You should only call delete on things created by new . For each malloc or calloc , there should be exactly one free . For each new there should be exactly one delete . For each new[] there should be

Is it practically OK to delete object not constructed using the new expression?

荒凉一梦 提交于 2019-12-12 10:39:09
问题 Pedantically, this may not be OK. As per cppref: If expression is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined. Putting that aside, is the following code OK in practice ( T is non-array, and assuming that new is not replaced)? auto p = (T*)operator new(sizeof(T)); new(p) T{}; delete p; It is said that in cppref that When calling the allocation function, the new-expression passes the number of bytes requested as the first

C++ array delete operator syntax

时间秒杀一切 提交于 2019-12-12 10:38:25
问题 After I do, say Foo* array = new Foo[N]; I've always deleted it this way delete[] array; However, sometimes I've seen it this way: delete[N] array; As it seems to compile and work (at least in msvc2005), I wonder: What is the right way to do it? Why does it compile the other way, then? 回答1: You can check this MSDN link: delete[N] operator. The value is ignored. EDIT I tried this sample code on VC9: int test() { std::cout<<"Test!!\n"; return 10; } int main() { int* p = new int[10]; delete[test

Why compilers do not assign NULL to pointer variable automatically after deleting dynamic allocated memory? [duplicate]

China☆狼群 提交于 2019-12-12 09:29:47
问题 This question already has answers here : Why doesn't delete set the pointer to NULL? (12 answers) Why doesn't free(p) set p to NULL? (9 answers) Closed 2 years ago . I have small piece of code: #include <iostream> using namespace std; int main() { int *p = new int(10); if(p != NULL) { cout<<"Deleted dynamic allocated memory"<<endl; delete p; } if(p == NULL) { cout<<"NULL"<<endl; } else { cout<<"Not NULL"<<endl; } return 0; } After deleting dynamic allocated memory using delete operator, Why

Delete an object securely from a multi-threaded program

孤者浪人 提交于 2019-12-12 09:27:53
问题 DISCLAIMER: neither Boost, nor C++11 allowed. I've a program, in which I create an instance of Foo and I operate with it in a number of threads. Then I want to delete it securely so those threads do not fall into a segmentation fault. I've added a mutex member to Foo and lock it each time a thread function runs. In order different threads do not conflict with each other. class Foo { public: pthread_mutex_t mutex; }; void* thread ( void* fooPtr ) { Foo* fooPointer = (Foo*) fooPtr; while ( 1 )

Delete expression

被刻印的时光 ゝ 提交于 2019-12-12 08:55:59
问题 Reference here That destructor will also implicitly call the destructor of the auto_ptr object. And that will delete the pointer it holds, that points to the C object - without knowing the definition of C! That appeared in the .cpp file where struct A's constructor is defined. This was curious and then 5.3.5/5 states - "If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is

Why is there a delete[] in C++?

不想你离开。 提交于 2019-12-12 08:35:13
问题 Why is there a delete[] ? From my understanding its to behave differently for arrays. However, why does it really exist? There's only free in C and no free_array. Also in syntax the only difference between delete var and delete []var is the [] which has no params (I'm not telling the length of the array). So why does delete[] really exist? I know someone will say you can overload delete and delete[] (at least i think that is possible) but lets say we are not overloading it. Why does it exist?

Is delete[] equal to delete?

无人久伴 提交于 2019-12-12 02:57:51
问题 IP_ADAPTER_INFO *ptr=new IP_ADAPTER_INFO[100]; if I free using delete ptr; will it lead to memory leak, if not then why ? This is disassembly code generated by VS2005 ; delete ptr; 0041351D mov eax,dword ptr [ptr] 00413520 mov dword ptr [ebp-0ECh],eax 00413526 mov ecx,dword ptr [ebp-0ECh] 0041352C push ecx 0041352D call operator delete (4111DBh) 00413532 add esp,4 ; delete []ptr; 00413535 mov eax,dword ptr [ptr] 00413538 mov dword ptr [ebp-0E0h],eax 0041353E mov ecx,dword ptr [ebp-0E0h]