delete-operator

Deleting a pointer in C++

巧了我就是萌 提交于 2019-11-27 06:03:41
Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience. I've seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a 'simple' pointer (or whatever the proper term might be), here's the code I'm trying to run: #include <iostream>; using namespace std; int main() { int myVar, *myPointer; myVar = 8; myPointer = &myVar; cout << "delete-ing

Behaviour of malloc with delete in C++

筅森魡賤 提交于 2019-11-27 04:46:36
int *p=(int * )malloc(sizeof(int)); delete p; When we allocate memory using malloc then we should release it using free and when we allocate using new in C++ then we should release it using delete. But if we allocate memory using malloc and then use delete, then there should be some error. But in the above code there's no error or warning coming in C++. Also if we reverse and allocate using new and release using free, then also there's no error or warning. Why is it so? This is undefined behaviour, as there's no way to reliably prove that memory behind the pointer was allocated correctly (i.e.

Delete pointer to multidimensional array in class through another pointer - how?

徘徊边缘 提交于 2019-11-27 04:07:55
问题 I have a pointer to a class, that have a pointer to a multidimensional array but I can't seem to delete it from memory when I need to or set it to NULL. #define X 10 #define Y 10 struct TestClass { public: int *pArray[X][Y]; }; // different tries, none working: delete Pointer_To_TestClass->pArray[0][0]; delete[] Pointer_To_TestClass->pArray[0][0] // or by simply: Pointer_To_TestClass->pArray[0][0] = NULL; I know the array has data because I can see the results on screen. Also check if it's

Object-Oriented Suicide or delete this;

萝らか妹 提交于 2019-11-27 03:23:37
问题 The following code compiled with MSVC9.0 runs and outputs Destructor four times, which is logical. #include <iostream> class SomeClass { public: void CommitSuicide() { delete this; } void Reincarnate() { this->~SomeClass(); new (this) SomeClass; } ~SomeClass() { std::cout << "Destructor\n"; } }; int main() { SomeClass* p = new SomeClass; p->CommitSuicide(); p = new SomeClass; p->Reincarnate(); p->~SomeClass(); //line 5 p->CommitSuicide(); } I think the first 4 lines of code in main do not

C++ and when to use delete

戏子无情 提交于 2019-11-27 01:58:10
问题 I am re-reading some code from a while ago on C++ (I am learning Java in school right now), and I am a little confused as to when I must use delete. For example: When declaring two objects: Fraction* f1; Fraction* f2; And create f1 and f2 like this: f1 = new Fraction(user_input1, user_input2); f2 = new Fraction(user_input3, user_input4); The next time I want to use new operator to create a new object, do I have to delete first? I am confused because I am used to having the garbage collector

How to control memory allocation strategy in third party library code?

匆匆过客 提交于 2019-11-27 01:45:38
问题 Previous header: "Must I replace global operators new and delete to change memory allocation strategy in third party code?" Short story: We need to replace memory allocation technique in third-party library without changing its source code. Long story: Consider memory-bound application that makes huge dynamic allocations (perhaps, almost all available system memory). We use specialized allocators, and use them everywhere ( shared_ptr 's, containers etc.). We have total control and power over

delete[] an array of objects

丶灬走出姿态 提交于 2019-11-27 01:07:51
I have allocated and array of Objects Objects *array = new Objects[N]; How should I delete this array? Just delete[] array; or with iterating over the array's elements? for(int i=0;i<N;i++) delete array[i]; delete[]; Thanks UPDATE: I changed loop body as delete &array[i]; to force the code to compile. Every use of new should be balanced by a delete , and every use of new[] should be balanced by delete[] . for(int i=0;i<N;i++) delete array[i]; delete[] array; That would be appropriate only if you initialized the array as: Objects **array = new Objects*[N]; for (int i = 0; i < N; i++) { array[i]

C++ Array of pointers: delete or delete []?

半腔热情 提交于 2019-11-27 00:51:34
问题 Cosider the following code: class Foo { Monster* monsters[6]; Foo() { for (int i = 0; i < 6; i++) { monsters[i] = new Monster(); } } virtual ~Foo(); } What is the correct destructor? this: Foo::~Foo() { delete [] monsters; } or this: Foo::~Foo() { for (int i = 0; i < 6; i++) { delete monsters[i]; } } I currently have the uppermost constructor and everything is working okey, but of course I cannot see if it happens to be leaking... Personally, I think the second version is much more logical

Delete objects of incomplete type

这一生的挚爱 提交于 2019-11-26 23:26:31
问题 This one made me think: class X; void foo(X* p) { delete p; } How can we possibly delete p if we do not even know whether X has visible destructor? g++ 4.5.1 gives three warnings: warning: possible problem detected in invocation of delete operator: warning: 'p' has incomplete type warning: forward declaration of 'struct X' And then it says: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. Wow... are

Using operator new and operator delete with a custom memory pool/allocator

微笑、不失礼 提交于 2019-11-26 22:05:00
问题 I'm working on a memory pool/memory allocator implementation and I am setting it up in a manor where only a special "Client" object type can draw from the pool.The client can either be constructed directly onto the pool, or it can use the pool for dynamic memory calls or it could in theory do both. I would like to be able to overload operator new and operator delete in a way that would call my pools "alloc()" and "free()" functions in order to get the memory needed for the object to construct