C++程序设计语言练习10.5 直方图的C++表示

来源:互联网 发布:淘宝屈臣氏海外旗舰店 编辑:程序博客网 时间:2024/05/10 00:13

答案上的代码如下:

#include <iostream>#include <stddef.h>#include <cassert>struct Histogram{Histogram(ptrdiff_t minval, size_t gap, size_t n_bins);~Histogram(){ delete[] bin_; }void record(ptrdiff_t);void output_to(std::ostream&);private:ptrdiff_t const minval_, maxval_;size_t const gap_;size_t * const bin_;size_t n_too_small_, n_too_large_;};Histogram::Histogram(ptrdiff_t m, size_t g, size_t n):minval_(m), maxval_(m + n*g - 1), gap_(g),bin_(new size_t[n]), n_too_small_(0), n_too_large_(0){assert(g != 0 && n != 0);}void Histogram::record(ptrdiff_t datapoint){if (datapoint < minval_){++n_too_small_;}else if (datapoint > maxval_){++n_too_large_;}else++bin_[(datapoint - minval_) / gap_];}void Histogram::output_to(std::ostream &output){output << "< " << minval_ << ": " << n_too_small_ << '\n';for (ptrdiff_t left = minval_; left < maxval_; left += gap_){output << left << ".." << left + gap_ - 1 << ": "<< bin_[(left - minval_) / gap_] << "\n";output << "> " << maxval_ << ": " << n_too_large_ << '\n';}}int main(){Histogram h = Histogram(1, 5, 10);h.output_to(std::cout);h.~Histogram();return 0;}

莫名的析构崩溃。

编译运行环境:Centos7 x64, gcc 6.1

[chaos@localhost cpp]$ ./10.5< 1: 01..5: 0> 50: 06..10: 0> 50: 011..15: 0> 50: 016..20: 0> 50: 021..25: 0> 50: 026..30: 0> 50: 031..35: 0> 50: 036..40: 0> 50: 041..45: 0> 50: 046..50: 0> 50: 0*** Error in `./10.5': double free or corruption (fasttop): 0x00000000020b8c20 ***======= Backtrace: =========/lib64/libc.so.6(+0x7d023)[0x7f297e715023]./10.5[0x400f35]./10.5[0x400e93]/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f297e6b9b15]./10.5[0x400a89]======= Memory map: ========00400000-00402000 r-xp 00000000 fd:00 3124212                            /home/chaos/文档/proc/cpp/10.500601000-00602000 r--p 00001000 fd:00 3124212                            /home/chaos/文档/proc/cpp/10.500602000-00603000 rw-p 00002000 fd:00 3124212                            /home/chaos/文档/proc/cpp/10.5020a7000-020d9000 rw-p 00000000 00:00 0                                  [heap]7f2978000000-7f2978021000 rw-p 00000000 00:00 0 7f2978021000-7f297c000000 ---p 00000000 00:00 0 7f297e698000-7f297e84e000 r-xp 00000000 fd:00 134378031                  /usr/lib64/libc-2.17.so7f297e84e000-7f297ea4e000 ---p 001b6000 fd:00 134378031                  /usr/lib64/libc-2.17.so7f297ea4e000-7f297ea52000 r--p 001b6000 fd:00 134378031                  /usr/lib64/libc-2.17.so7f297ea52000-7f297ea54000 rw-p 001ba000 fd:00 134378031                  /usr/lib64/libc-2.17.so7f297ea54000-7f297ea59000 rw-p 00000000 00:00 0 7f297ea59000-7f297ea6e000 r-xp 00000000 fd:00 142624879                  /usr/lib64/libgcc_s-4.8.5-20150702.so.17f297ea6e000-7f297ec6d000 ---p 00015000 fd:00 142624879                  /usr/lib64/libgcc_s-4.8.5-20150702.so.17f297ec6d000-7f297ec6e000 r--p 00014000 fd:00 142624879                  /usr/lib64/libgcc_s-4.8.5-20150702.so.17f297ec6e000-7f297ec6f000 rw-p 00015000 fd:00 142624879                  /usr/lib64/libgcc_s-4.8.5-20150702.so.17f297ec6f000-7f297ed70000 r-xp 00000000 fd:00 134378041                  /usr/lib64/libm-2.17.so7f297ed70000-7f297ef6f000 ---p 00101000 fd:00 134378041                  /usr/lib64/libm-2.17.so7f297ef6f000-7f297ef70000 r--p 00100000 fd:00 134378041                  /usr/lib64/libm-2.17.so7f297ef70000-7f297ef71000 rw-p 00101000 fd:00 134378041                  /usr/lib64/libm-2.17.so7f297ef71000-7f297f0e2000 r-xp 00000000 fd:00 141313580                  /usr/lib64/libstdc++.so.6.0.227f297f0e2000-7f297f2e2000 ---p 00171000 fd:00 141313580                  /usr/lib64/libstdc++.so.6.0.227f297f2e2000-7f297f2ec000 r--p 00171000 fd:00 141313580                  /usr/lib64/libstdc++.so.6.0.227f297f2ec000-7f297f2ee000 rw-p 0017b000 fd:00 141313580                  /usr/lib64/libstdc++.so.6.0.227f297f2ee000-7f297f2f2000 rw-p 00000000 00:00 0 7f297f2f2000-7f297f313000 r-xp 00000000 fd:00 134294926                  /usr/lib64/ld-2.17.so7f297f4f9000-7f297f4fe000 rw-p 00000000 00:00 0 7f297f510000-7f297f513000 rw-p 00000000 00:00 0 7f297f513000-7f297f514000 r--p 00021000 fd:00 134294926                  /usr/lib64/ld-2.17.so7f297f514000-7f297f515000 rw-p 00022000 fd:00 134294926                  /usr/lib64/ld-2.17.so7f297f515000-7f297f516000 rw-p 00000000 00:00 0 7ffe2c645000-7ffe2c666000 rw-p 00000000 00:00 0                          [stack]7ffe2c744000-7ffe2c746000 r-xp 00000000 00:00 0                          [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]已放弃(吐核)


0 0
原创粉丝点击