"Data structure, algorithm and application of C + + language implementation--ss" a heap of code implementation Bug__java

Source: Internet
Author: User
The discovery of the problem

When I knock on the heap code in this book, found completely in accordance with the book after the completion of the code, the compilation succeeds, the results are correct, but the program did not end normally, but in the last line of return 0 when the crash, after a step-by-step debug, first determine the problem appears in the destructor, That is, delete []heap, there is a problem, and the root of the problem is

Asserte (_block_type_is_valid (Phead->nblockuse))

But debug a lot of times did not find any problems, when looking at the code again, only to discover the problem. solution to the problem

Looking at the code again, we find that the first sentence of the Initialize (T *theheap,int thesize) function is delete []heap and then assign Theheap to heap, when I think of the key word delete must and keyword new corresponding to each other, then the problem may be out here; sure enough, because in the main method, the array theheap is not dynamically defined, but the form of int theheap[10], statically defined, the dynamically defined array heap in the constructor no longer exists, Instead, it is replaced by statically defined Theheap, where a block_type_is_valid error occurs when the Delete keyword that corresponds to the new in the destructor and the constructor is used. So in the Initialize (T *theheap,int thesize) function, you can't delete the heap, but instead use a loop to copy the contents of the theheap to the heap so that you don't break the correspondence between new and delete in the constructor and destructor. Summary

Through this experience, understand C + + with new and delete two key words must be cautious. According to the code in the book, the parameter of the Initialize function must be a dynamically defined array, otherwise there would be a problem, so that the function that supports only dynamically defined arrays is definitely a bug. and the code given in this book's Web site The parameters of the Initialize function in the main method are statically defined arrays, which is certainly wrong.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.