以下のコードを実行したら、以下のようなエラーが出ました。ただ、エラー前にやりたい処理が出来ているんですが、どうやらあyのdeleteに失敗しているみたいです。
原因および対処法が知りたいです。
ヘッダーファイル
#ifndef __VECTOR__ #define __VECTOR__ class Vector{ private: int Size; public: double *Element; int size(void)const; Vector(void); Vector(int size); void del(void); std::ostream& print(std::ostream &lhs) const; Vector(const Vector &arg); }; std::ostream& operator<<(std::ostream &lhs,const Vector &rhs); #endif
関数ファイル
#include<iostream> #include<cstdlib> #include<new> #include"1.h" Vector::Vector(void){} Vector::Vector(int size)try:Size(size),Element(new double[Size]){} catch(std::bad_alloc){ std::cerr << "Vector::Vector(int n):Out of Memory!" << std::endl; throw; } Vector::Vector(const Vector &arg)try:Size(arg.Size),Element(new double(Size)){ for(int i=0;i<Size;i++) Element[i]=arg.Element[i]; } catch(std::bad_alloc){ std::cerr << "Vector::Vector(int n):Out of Memory!" << std::endl; throw; } int Vector::size(void)const{ return Size; } //この関数でdeleteを行うのですが、コピーコンストラクタで生成したyのdeleteでエラーがおきているみたいです。> 引用テキスト void Vector::del(void){ std::cout<<"test :: call del"<<std::endl; delete [] Element; std::cout<<"test :: called del"<<std::endl;//yをdeleteするとき、この出力がされません return ; } std::ostream& Vector::print(std::ostream &lhs) const{ int i; lhs << "("; for(i=0;i<Size;i++){ lhs<<Element[i]<<","; } lhs << ")"; return lhs; } std::ostream& operator<<(std::ostream &lhs,const Vector &rhs){ return rhs.print(lhs); }
メインファイル
#include <iostream> #include <cstdlib> #include"1.h" int main(void){ int i; try{ Vector x(5); for(i=0;i<x.size();i++){ x.Element[i]=(double)(i+5); } std::cout << "x:" << x << std::endl;; Vector y(x); std::cout << "y:" << y << std::endl;; x.del(); y.del(); return 0; } catch(std::exception &e){ std::cerr << "main:" << e.what() << std::endl; exit(1); } }
実行結果
x:(5,6,7,8,9,) y:(5,6,7,8,9,) test :: call del test :: called del test :: call del *** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0000000000602040 *** ======= Backtrace: ========= /lib64/libc.so.6[0x7f1381755a42] /lib64/libc.so.6(cfree+0x8c)[0x7f138175924c] ./a.out[0x400ff8] ./a.out[0x400de8] /lib64/libc.so.6(__libc_start_main+0xf4)[0x7f13816ff304] ./a.out[0x400c29] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 00:1f 766973348 /home/gbu/af17006/dist/homework/info4/8/a.out 00601000-00602000 rw-p 00001000 00:1f 766973348 /home/gbu/af17006/dist/homework/info4/8/a.out 00602000-00623000 rw-p 00602000 00:00 0 [heap] 7f137c000000-7f137c021000 rw-p 7f137c000000 00:00 0 7f137c021000-7f1380000000 ---p 7f137c021000 00:00 0 7f13816e1000-7f1381831000 r-xp 00000000 08:01 538571 /lib64/libc-2.8.so 7f1381831000-7f1381a30000 ---p 00150000 08:01 538571 /lib64/libc-2.8.so 7f1381a30000-7f1381a34000 r--p 0014f000 08:01 538571 /lib64/libc-2.8.so 7f1381a34000-7f1381a35000 rw-p 00153000 08:01 538571 /lib64/libc-2.8.so 7f1381a35000-7f1381a3a000 rw-p 7f1381a35000 00:00 0 7f1381a3a000-7f1381a47000 r-xp 00000000 08:01 538752 /lib64/libgcc_s-4.1.2-20080708.so.1 7f1381a47000-7f1381c46000 ---p 0000d000 08:01 538752 /lib64/libgcc_s-4.1.2-20080708.so.1 7f1381c46000-7f1381c47000 rw-p 0000c000 08:01 538752 /lib64/libgcc_s-4.1.2-20080708.so.1 7f1381c47000-7f1381cc8000 r-xp 00000000 08:01 538792 /lib64/libm-2.8.so 7f1381cc8000-7f1381ec7000 ---p 00081000 08:01 538792 /lib64/libm-2.8.so 7f1381ec7000-7f1381ec8000 r--p 00080000 08:01 538792 /lib64/libm-2.8.so 7f1381ec8000-7f1381ec9000 rw-p 00081000 08:01 538792 /lib64/libm-2.8.so 7f1381ec9000-7f1381faf000 r-xp 00000000 08:01 346276 /usr/lib64/libstdc++.so.6.0.8 7f1381faf000-7f13821af000 ---p 000e6000 08:01 346276 /usr/lib64/libstdc++.so.6.0.8 7f13821af000-7f13821b6000 r--p 000e6000 08:01 346276 /usr/lib64/libstdc++.so.6.0.8 7f13821b6000-7f13821b8000 rw-p 000ed000 08:01 346276 /usr/lib64/libstdc++.so.6.0.8 7f13821b8000-7f13821ca000 rw-p 7f13821b8000 00:00 0 7f13821ca000-7f13821e5000 r-xp 00000000 08:01 538581 /lib64/ld-2.8.so 7f13823cb000-7f13823ce000 rw-p 7f13823cb000 00:00 0 7f13823e1000-7f13823e4000 rw-p 7f13823e1000 00:00 0 7f13823e4000-7f13823e5000 r--p 0001a000 08:01 538581 /lib64/ld-2.8.so 7f13823e5000-7f13823e6000 rw-p 0001b000 08:01 538581 /lib64/ld-2.8.so 7fffcacf5000-7fffcad0a000 rw-p 7ffffffea000 00:00 0 [stack] 7fffcadff000-7fffcae00000 r-xp 7fffcadff000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] アボート
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/09 06:04