FAQ Database Discussion Community


The destructor for the class member `B`, why is it invoked in the snippet below?

c++,language-lawyer,c++14,delete-operator
From ยง5.3.5[expr.delete]/1, I can understand that the destructor for the object *a is not invoked in the snippet below. But I didn't understand why is the destructor for the class member B invoked in this case, as can be seen in this live example. #include <iostream> class A { public:...

Is calling delete operator on a memory allocated by new[] operator undefined behavior?

c++,standards,c++14,delete-operator
I am pretty much sure it is but if I am interpreting correctly the standard (Section 18.6.1.2 new.delete.array) mentions that: void operator delete[](void* ptr) noexcept; pointer. . 13 Default behavior: Calls operator delete(ptr) Since in its default behavior delete[] just calls its delete(ptr) equivalent why should it matter which version...

Deleting a dynamically allocated 2D array [duplicate]

c++,memory-management,delete-operator
This question already has an answer here: delete vs delete[] [duplicate] 4 answers So I'm used to memory management in C where free(pointer) will free up all space pointed to by pointer. Now I'm confusing myself when attempting to do something simple in C++. If I have a 2D...

How do I stop my resize function from causing _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)?

c++,arrays,pointers,delete-operator
I've been tasked with implementing a stack using arrays However, when I run the program it says _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse). The debugger directed me to the delete statement in the resize function, but I'm unsure how to fix the problem. Note: I'm trying to do so without using vectors. void...

What is the exact behaviour of delete and delete[]?

c++,arrays,new-operator,delete-operator
Why is this code wrong? Am I missing something regarding the behaviour of delete and delete[]? void remove_stopwords(char** strings, int* length) { char** strings_new = new char*[*length]; int length_new = 0; for(int i=0; i<*length; i++) { if(is_common_keyword(strings[i]) == 0) { strings_new[length_new] = strings[i]; length_new++; } else { delete strings[i]; strings[i]...

Should I use a pointer to an object or an object when the object represents an hardware component?

c++,new-operator,delete-operator,v4l2
I have a class called Camera which opens a camera with v4l2_open, etc., in the constructor. The destructor does some cleaning and closes the file descriptor with v4l2_close. When the camera crashes, what I do is to delete the object and then create a new one: Camera *camera = new...

How to delete new'd arrays in JavaScript?

javascript,arrays,new-operator,unityscript,delete-operator
I'm using Unity, so this is technically UnityScript but I assume the built-in arrays work the same way. I have an array I create thusly: var room:roomInfo[,,]; room=new roomInfo[5,5,5]; What's the deleting syntax? Simply doing delete room; or delete room[,,]; doesn't seem to work. After deleting it I need to...