delete-operator

When does std::unique_ptr<A> need a special deleter if A has a destructor?

限于喜欢 提交于 2020-01-03 09:04:15
问题 If the class A in unique_ptr<A> it's own destructor, is it necessary to declare a deleter to ensure that the unique pointer uses that destructor? The example I am thinking of is that A has a member mx of type user_matrix (a name I just made up) which needs to call a function free(...) to release its memory, one would define ~A(){ user_matrix::free(mx); /*etc*/} Since default_deleter<> will call delete , it is my understanding that that should use ~A() . However, the example with the opening

What happens to an address after delete operator has been applied to it in C++?

半腔热情 提交于 2019-12-29 07:45:11
问题 If I delete a pointer as follows for example: delete myPointer; And, after that did not assign 0 to the pointer as follows: myPointer = 0; //skipped this Will myPointer be pointing to another memory address? 回答1: No, in most implementations it will store the same address as previously - delete usually doesn't change the address and unless you assign a new address value it remains unchanged. However this is not always guaranteed. Don't forget, that doing anything except assigning a null

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

偶尔善良 提交于 2019-12-26 06:49:13
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

こ雲淡風輕ζ 提交于 2019-12-26 06:49:07
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

_CrtMemDumpAllObjectsSince() function is not able to detect leaks if delete array called instead of delete []array

有些话、适合烂在心里 提交于 2019-12-25 00:42:39
问题 Here is some sample code: #include <crtdbg.h> #include <windows.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif int main() { int* arr = new int[10]; delete arr; //not using delete [] arr _CrtMemDumpAllObjectsSince(NULL); // dumping leak logs return 0; } As you can see that I have not used delete [] arr and still I did not get any leaks. Can anyone please correct it and explain me why _CrtMemDumpAllObjectsSince() is not dumping leaks in above code. 回答1: The official answer is that

class overloaded new and delete vs placement new with a bespoke memory class

我怕爱的太早我们不能终老 提交于 2019-12-25 00:27:19
问题 I am investigating the pros and cons between using class overloaded news and deletes vs placement news. By this I mean, either declaring every class I may wish to new and delete with their own operator overloads, or by using the memory manager to give me the memory I need via placement new. I have a memory manager that allows me to allocate memory from a number of pools: enum MemPool { kPool1, kPool2, } class MemoryManager { public: template <typename T> void* Allocate(MemPool pool); void

Global overload operator new/delete in MinGW

孤人 提交于 2019-12-24 21:28:57
问题 I want to overload new/delete operators in my application to catch all memory leaks. It works on Linux fine. But I got a problems on Windows. New/delete overloading works only for .exe but not for calls from .dll files. Furthemore if some object is created in my code but is deleting from .dll file it leads to app crash. Cppreference here says Versions (1-8) are replaceable: a user-provided non-member function with the same signature defined anywhere in the program, in any source file,

Proper Object Disposal In C++/CLI

自古美人都是妖i 提交于 2019-12-24 10:00:18
问题 Consider the following class: public ref class Workspace { protected: Form^ WorkspaceUI; SplitContainer^ WorkspaceSplitter; AvalonEditTextEditor^ TextEditor; ScriptOffsetViewer^ OffsetViewer; SimpleTextViewer^ PreprocessedTextViewer; ListView^ MessageList; ListView^ FindList; ListView^ BookmarkList; ListView^ VariableIndexList; TextBox^ VariableIndexEditBox; Label^ SpoilerText; ToolStrip^ WorkspaceMainToolBar; ToolStripButton^ ToolBarNewScript; ToolStripButton^ ToolBarOpenScript;

Why delete pointer to stack is error?

与世无争的帅哥 提交于 2019-12-24 09:27:48
问题 I have read topic Calling delete on variable allocated on the stack And i know when use operator delete in stack, see error. But i want to know more and deep information . Why error ? 回答1: Stack created objects are automatic whereas non-stack created objects (using the keyword new ) are dynamic and need to be reclaimed using the keyword delete . Objects on the stack are reclaimed by the system automatically using a different mechanism to objects allocated dynamically using keyword new . So

new[] / delete[] and throwing constructors / destructors in C++

眉间皱痕 提交于 2019-12-24 08:01:50
问题 What happens, in the following code, if construction / destruction of some array element throws? X* x = new X[10]; // (1) delete[] x; // (2) I know that memory leaks are prevented, but additionally: Ad (1), are the previously constructed elements destructed? If yes, what happens if destructor throws in such a case? Ad (2), are the not-yet-destructed elements destructed? If yes, what happens if destructor throws again? 回答1: Yes, if the constructor of x[5] throws, then the five array elements x