Recurring C++ and Qt anti-patterns
-
I just saw the following again:
auto *p = new ...; // ... if (p != NULL) { delete p; p = NULL; }
which can be shortened to:
delete p; p = NULL; // p = nullptr in C++11 and upwards
because
delete
does the check anyway. -
@aha_1980 said in Recurring C++ and Qt anti-patterns:
because delete does the check anyway
Wait, what? Since when does C++ check if the pointer is nullptr before delete? I didn't know this was a thing now.
Also, is deleteLater not a way to delete Qt objects? Its morning and I haven't had my coffee.
-
Okay, I am just confusing myself. If you delete a pointer you must immediately set it to null. Otherwise you risk double delete, which is bad. But its okay to delete something set to null. Got it.
Edit:
Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself. -
@fcarney said in Recurring C++ and Qt anti-patterns:
Why doesn't delete set the pointer to null then? That seems like it may be an antipattern in and of itself.
I have indeed asked that myself. If someone has the correct answer for that, I'm all ears.
-
@aha_1980
Apparently the standard allows for it:
https://stackoverflow.com/questions/704466/why-doesnt-delete-set-the-pointer-to-nullThe creator himself wonders why it isn't so. Its like C++ is this beautiful, amazing, and now, WILD animal roaming free in cyberspace... Yeah, maybe the analogy isn't all that great, but it does conjure up a cool picture.
-
@LeLev
likely because it would bring more problems than solutions
That would mean, that this pointer shows to an invalid memory region after the delete. Can you think of an example where you still want to use that pointer afterwards? (That is a real question - because for now I have no idea).
-
@LeLev said in Recurring C++ and Qt anti-patterns:
likely because it would bring more problems than solutions
I could see a case where a program is deleting thousands of pointers and there might actually be overhead in a mov instruction for each delete. I have no idea if this overhead would be significant over the delete operation, but it would still be overhead. It would not be that hard to test such a scenario. I should try it!
-
@fcarney said in Recurring C++ and Qt anti-patterns:
deleting thousands of pointers and there might actually be overhead
I cannot actually tell if the overhead in this code is the indexing of the array, or if the movement of data is significant. I tried doing a dummy no op index, but I am guessing it is being optimized out:
#include <QCoreApplication> #include <QElapsedTimer> #include <QDebug> #define MEM_SEG_LEN 8 #define MEM_SEGS 100000000 char** createMemoryList(){ char** list = new char*[MEM_SEGS]; for(int index=0; index<MEM_SEGS; index++){ list[index]=new char[MEM_SEG_LEN]; } return list; } void deleteMemoryList(char** list){ for(int index=0; index<MEM_SEGS; index++){ delete list[index]; list[index]; // can you force an index to occur? } delete list; } void deleteMemoryListNull(char** list){ for(int index=0; index<MEM_SEGS; index++){ delete list[index]; list[index] = nullptr; } delete list; list = nullptr; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QElapsedTimer timer1; char** list1 = createMemoryList(); timer1.start(); deleteMemoryList(list1); qInfo() << timer1.elapsed(); QElapsedTimer timer2; char** list2 = createMemoryList(); timer2.start(); deleteMemoryListNull(list2); qInfo() << timer2.elapsed(); return a.exec(); }
I get the following output:
813 1301
I doubt that is the overhead of the movement of null into the pointer. My guess is the the index overhead is in there too.
-
I eliminated the extra index (probably compiler already did this before):
void deleteMemoryList(char** list){ for(int index=0; index<MEM_SEGS; index++){ char* tmp = list[index]; delete tmp; } delete list; } void deleteMemoryListNull(char** list){ for(int index=0; index<MEM_SEGS; index++){ char* tmp = list[index]; delete tmp; tmp = nullptr; } delete list; list = nullptr; }
Results:
877 1369
Edit: Real world usage? I really highly doubt it. That is a LOT of iterations of delete. So I would say the extra cycles are negligible.
Edit2:
Pointer math:void deleteMemoryList(char** list){ for(int index=0; index<MEM_SEGS; index++){ char** tmp = &(list[index]); delete *tmp; } delete list; } void deleteMemoryListNull(char** list){ for(int index=0; index<MEM_SEGS; index++){ char** tmp = &(list[index]); delete *tmp; *tmp = nullptr; } delete list; list = nullptr; }
Results:
853 1307
Sometimes apples and apples is hard.
-
@fcarney
Since this is the lounge... Surprised by your findings (in earlier examples). What exactly is the difference in the assembly between the two versions? What is being generated for yourtmp = nullptr;
? (Not the later*tmp = nullptr;
, that's different.) -
@JonB said in Recurring C++ and Qt anti-patterns:
tmp = nullptr;
I changed it to not update a local variable. char* tmp is local, so setting it to null is just setting a local variable to null. So it was setting the wrong area of memory to null. That is why I took the address of where that pointer is stored.
The assembler for tmp = nullptr in previous incarnation:
movq $0x0,-0x8(%rbp)
The assembler for *tmp = nullptr in latest incarnation:
mov -0x8(%rbp),%rax movq $0x0,(%rax)
But you are right, the tmp = nullptr is more representative.
The timing is not much different.