Why would you write something like this? (intentio

2019-01-28 00:07发布

I came across this kind of code once in a while - I suspect the creator is/was afraid that table delete would iterate over the table and "cost performance" (which imho will not be done either way)... is there any real benefit one might get/consider/imagine from not using the the table delete here?

myClass** table = new myClass* [size];
... //some code that does not reallocate or change the value of the table pointer ;)
delete table; // no [] intentionally

8条回答
Root(大扎)
2楼-- · 2019-01-28 00:39

Check with the section [16.11] "How do I allocate / unallocate an array of things?" and beyond in C++ FAQ Lite,

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11

They explain that the array delete is a must when an array is created.

The instanced of myClass pointed to by the elements of your array should also be deleted where they are created.

查看更多
放我归山
3楼-- · 2019-01-28 00:40

that statement will leave all of the myClass objects that were pointed to by all the pointers in the array hanging around in memory. It also leaves the array of pointers in memory. There is no way that can be helpful, as it only frees up 32 bits of memory, and the OS still thinks you have (size) myClasses and pointers to each in use. This is just an example of a programmer not cleaning up after themself properly.

查看更多
登录 后发表回答