tailieunhanh - C++ Timesaving Techniques for Dummies phần 4

Để xem mã theo dõi phân bổ hoạt động như thế nào, nó đơn giản nhất để tạo ra một trình điều khiển thử nghiệm đơn giản để minh họa những phần khác nhau của hệ thống. Hãy tạo ra một chương trình thử nghiệm đơn giản để sử dụng các bộ xử lý mới và xóa chúng tôi đã tạo ra. Các bước sau đây chỉ cho bạn cách: | Testing the Memory Allocation Tracker All production code should be tested with a memory-leak tool or run through code like this to see whether memory is being allocated and freed properly not freed correctly or freed more than once. Testing the Memory Allocation Tracker In order to see how the allocation tracking code works it is easiest to create a simple test driver that illustrates the various pieces of the system. Let s create a simple test program to use the new and delete handlers we have created. The following steps show you how 1. In the code editor of your choice open the existing file to hold the code for your test program. In this example I named the test program CH 24. 2. Type the code from Listing 24-4 into your file. Better yet copy the code from the source file on this book s companion Web site. 3. Save the source code and close your code editor. Listing 24-4 Memory Allocator Test Driver int main int argc char argv DumpUnfreed char c new char 200 DumpUnfreed char c2 new char 256 - 3 DumpUnfreed delete c delete c DumpUnfreed i nt x new i nt 20 delete x DumpUnfreed Foo f new Foo delete f Foo af new Foo 5 delete af Foo af1 new Foo 3 delete af1 4. Compile the source file using your favorite compiler on your favorite operating system. If you run the resulting executable the program should give you the output shown in Listing 24-5. Listing 24-5 Output from the Memory Tracking Program . ------------------------ Allocations ----------------------- Total Unfreed 0 bytes Array operator new called ------------------------ Allocations ---------------------- 0 ADDRESS a050648 Size 200 unfreed Total Unfreed 200 bytes Array operator new called Allocations --------------------- 0 ADDRESS a050648----------------------------------------Size 200 unfreed 1 ADDRESS a050770 Size 256 unfreed continued Technique 24 Defining Your Own new and delete Handlers Listing 24-5 continued Total Unfreed 456 bytes Basic operator delete called Basic operator delete called Unable

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.