delete-operator

new-expression and delete-expression on const reference and const pointer

怎甘沉沦 提交于 2019-12-11 22:19:50
问题 C++ Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees. Then, why can they be delete d? const int& cirDynamic = *( new int(5) ); // ^ 'const int& cirDynamic = *( &( *( new int(5) ) ) );' gives same output below cout << cirDynamic << endl; // 5 delete &cirDynamic; cout << cirDynamic << endl; // garbage value I know the trailing const in T* const only prevents the pointer from being reseated, but below I use

What does the use of new require you to also call delete?

一曲冷凌霜 提交于 2019-12-11 19:39:13
问题 I am here stuck with a question in my C++ book with the following: "What does the use of new require you to also call delete?" Maybe you have an answer for that? 回答1: Because that is the way C++ is designed & that is the intended behavior. The intention was to provide a memory allocation which you demand and own till you reliquish it explicitly. new gives you a dynamic memory allocation(on heap) which will continue to exist and you own it untill you explicitly deallocate it by calling delete

Free/delete strtok_r pointer before processing complete string?

狂风中的少年 提交于 2019-12-11 11:46:51
问题 When trying to delete/free character ptr without being processed completely by strtok_r , its giving me stack trace error. I know that one cannot free/delete a strtok_r char ptr in a regular way, without completing the whole strings separation process by strtok_r func. Can anyone tell me how to free a char ptr, when its under process by strtok_r ? char *data = new char[temp->size()+1];//temp is of type string copy(temp->begin(),temp->end(),data); data[temp->size()]='\0'; count = 0; while

Can't delete dynamically allocated multidimensional array

安稳与你 提交于 2019-12-11 07:44:51
问题 I can't delete the dynamically generated arrays. There is how I create them: template <typename T> T **AllocateDynamic2DArray(int nRows, int nCols){ T **dynamicArray; dynamicArray = new T*[nRows]; for( int i = 0 ; i < nRows ; i++ ){ dynamicArray[i] = new T[nCols]; for ( int j=0; j<nCols;j++){ dynamicArray[i][j]= 0; } } return dynamicArray; } And I initialize a 2D array using: long double** h = AllocateDynamic2DArray<long double>(KrylovDim+1,KrylovDim); I can't delete it. Here are the

C++ singleton with private constructor

徘徊边缘 提交于 2019-12-11 07:23:53
问题 I need singleton with a application lifetime, guaranteed creation/destruction and static access to it. #include <iostream> #include <cstdlib> #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) #define M() C::sM() #define M2() C::sM2() using namespace std; class C { private: static C* s; ~C() { cout << "~C()" << endl; } static C* instance() { if (s==NULL) { s=new C(); } cout << "instance()=" << s << endl; return s; } static void cleanUp()

overload delete[] operator with specific arguments

倖福魔咒の 提交于 2019-12-11 03:21:50
问题 We're trying to overload the delete[] operator with specific arguments. Which is the right way to call it? We use the GNU compiler and obtain compiler errors with all of these samples: #include<memory> using namespace std; typedef unsigned int P; struct A{ static allocator<A>m; P p; void*operator new[](size_t s){return m.allocate(s);} void operator delete[](void*p,int s){m.deallocate((A*)p,s);} void operator delete[](void*p,size_t t,int s){m.deallocate((A*)p,s);} }; int main(){ A*a=new A[10];

Putting restrictions in overloading new and delete

倖福魔咒の 提交于 2019-12-11 02:28:27
问题 Is it possible to put some restrictions in overloading operators new and delete? My overloaded new is linked in a different file to my test program. The scenario is: if(condition is satisfied) call overloaded new else call the actual new defined in new.h 回答1: There are three ways to provide an operator new. replacing one or more of the four non placement default operators new, providing overload to the default operator new (thus with an additional parameter, those may be called with the

What C++17 standard say about calling delete on nullptr?

牧云@^-^@ 提交于 2019-12-11 02:25:40
问题 C++03 Standard say's: 5.3.5 Delete [...] In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.[...] char *p = nullptr; delete p; //no effect It means, it is valid to delete null pointer in c++. What C++17 standard say about calling delete on nullptr pointer? 回答1: Yes it is valid, and it results in a noop. reference If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called. 回答2

Copy/move elision versus explicitly deleted copy/move constructors

青春壹個敷衍的年華 提交于 2019-12-11 00:29:56
问题 I want to know when copy/move elision applies (or is allowed to apply) to explicitly delete d copy/move constructors and to non- delete d copy/move constructors. Here are the specifics: Can an explicitly delete d copy ctor or move ctor get elided? Is an attempt to construct an object from another same-type object or temporary object ever allowed to succeed by skipping over the delete d copy ctor and/or delete d move ctor? Here’s what happens in VC12 (with which I’m not sure if there is an

C++ new & delete and functions

一世执手 提交于 2019-12-10 19:17:38
问题 This is a bit unclear to me... So, if I have a function: char *test(int ran){ char *ret = new char[ran]; // process... return ret; } and then call it multiple times: for(int i = 0; i < 100000000; i++){ char *str = test(rand()%10000000+10000000); // process... // delete[] str; // do i have to delete it here? } So the question is, do I have to use delete[] for each new[] call? 回答1: You don't have to. But if you don't delete memory you reserved with 'new' you will start running out of memory