When you run ./a.out, it produces a monstrous message:

*** glibc detected *** ./a.out: free(): invalid pointer: 0x08049ff4 *** ======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x42e501]
/lib/libc.so.6(+0x6dd70)[0x42fd70]
/lib/libc.so.6(cfree+0x6d)[0x432e5d]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xc36441]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xc3649d]
./a.out[0x8048801]
./a.out[0x804864d]
/lib/libc.so.6(__libc_start_main+0xe7)[0x3d8ce7]
./a.out[0x8048501]
======= Memory map: ========
003c2000-00519000 r-xp 00000000 08:05 261168 /lib/libc-2.12.1.so
00519000-0051a000 ---p 00157000 08:05 261168 /lib/libc-2.12.1.so
0051a000-0051c000 r--p 00157000 08:05 261168 /lib/libc-2.12.1.so
0051c000-0051d000 rw-p 00159000 08:05 261168 /lib/libc-2.12.1.so
0051d000-00520000 rw-p 00000000 00:00 0
005a6000-005c2000 r-xp 00000000 08:05 261144 /lib/ld-2.12.1.so
005c2000-005c3000 r--p 0001b000 08:05 261144 /lib/ld-2.12.1.so
v005c3000-005c4000 rw-p 0001c000 08:05 261144 /lib/ld-2.12.1.so 006a4000-006a5000 r-xp 00000000 00:00 0 [vdso] v0087d000-00897000 r-xp 00000000 08:05 261202 /lib/libgcc_s.so.1
00897000-00898000 r--p 00019000 08:05 261202 /lib/libgcc_s.so.1
00898000-00899000 rw-p 0001a000 08:05 261202 /lib/libgcc_s.so.1
00b8c000-00c6b000 r-xp 00000000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c6b000-00c6f000 r--p 000de000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c6f000-00c70000 rw-p 000e2000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c70000-00c77000 rw-p 00000000 00:00 0
00f84000-00fa8000 r-xp 00000000 08:05 261217 /lib/libm-2.12.1.so
00fa8000-00fa9000 r--p 00023000 08:05 261217 /lib/libm-2.12.1.so
00fa9000-00faa000 rw-p 00024000 08:05 261217 /lib/libm-2.12.1.so
08048000-08049000 r-xp 00000000 08:05 131889 /home/mr_bunch/a.out
08049000-0804a000 r--p 00000000 08:05 131889 /home/mr_bunch/a.out
0804a000-0804b000 rw-p 00001000 08:05 131889 /home/mr_bunch/a.out
0832d000-0834e000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b7835000-b7838000 rw-p 00000000 00:00 0
b7845000-b7847000 rw-p 00000000 00:00 0 bfee1000-bff02000 rw-p 00000000 00:00 0 [stack]
006a4000-006a5000 r-xp 00000000 00:00 0 [vdso] v0087d000-00897000 r-xp 00000000 08:05 261202 /lib/libgcc_s.so.1
00897000-00898000 r--p 00019000 08:05 261202 /lib/libgcc_s.so.1
00898000-00899000 rw-p 0001a000 08:05 261202 /lib/libgcc_s.so.1
00b8c000-00c6b000 r-xp 00000000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c6b000-00c6f000 r--p 000de000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c6f000-00c70000 rw-p 000e2000 08:05 396181 /usr/lib/libstdc++.so.6.0.14
00c70000-00c77000 rw-p 00000000 00:00 0
00f84000-00fa8000 r-xp 00000000 08:05 261217 /lib/libm-2.12.1.so
00fa8000-00fa9000 r--p 00023000 08:05 261217 /lib/libm-2.12.1.so
00fa9000-00faa000 rw-p 00024000 08:05 261217 /lib/libm-2.12.1.so
08048000-08049000 r-xp 00000000 08:05 131889 /home/mr_bunch/a.out
08049000-0804a000 r--p 00000000 08:05 131889 /home/mr_bunch/a.out
0804a000-0804b000 rw-p 00001000 08:05 131889 /home/mr_bunch/a.out
0832d000-0834e000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b7835000-b7838000 rw-p 00000000 00:00 0
b7845000-b7847000 rw-p 00000000 00:00 0 bfee1000-bff02000 rw-p 00000000 00:00 0 [stack]

Аварийный останов. Вот код: #include <stdio.h> #include <math.h> #include <cstring> class Vectorn { protected: int dim; double *X; public: Vectorn() { dim = 0; X = NULL; }; Vectorn(int _dim) { dim = _dim; X = new double[dim]; }; Vectorn(int _dim, double *_X) { dim = _dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _X[i]; }; Vectorn(const Vectorn & _C) { dim = _C.dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _C.X[i]; }; ~Vectorn() { dim = 0; if (X) delete[]X; X = NULL; }; int operator ==(Vectorn C) { if (dim != C.dim) return 0; for (int i = 0; i < dim; i++) if (X[i] != CX[i]) return 0; return 1; } Vectorn operator =(Vectorn C) { if (C == *(this)) { return *(this); }; int i; if (dim < C.dim) { for (i = 0; i < dim; i++) { X[i] = CX[i]; }; } else { for (i = 0; i < C.dim; i++) { X[i] = CX[i]; }; }; }; Vectorn operator +=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] += CX[i]; return *this; }; Vectorn operator -=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] -= CX[i]; return *this; }; Vectorn operator *=(double n) { for (int i = 0; i < dim; i++) X[i] *= n; return *this; }; void Print() { printf("Vector("); for (int i = 0; i < dim; i++) printf("%+lf_", X[i]); printf(")\n"); }; }; int main() { double g[4] = { 7., 5., 8., 0. }; Vectorn C(4); Vectorn D(4, g); C = D; C.Print(); D.Print(); }

Аварийный останов. Вот код: #include <stdio.h> #include <math.h> #include <cstring> class Vectorn { protected: int dim; double *X; public: Vectorn() { dim = 0; X = NULL; }; Vectorn(int _dim) { dim = _dim; X = new double[dim]; }; Vectorn(int _dim, double *_X) { dim = _dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _X[i]; }; Vectorn(const Vectorn & _C) { dim = _C.dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _C.X[i]; }; ~Vectorn() { dim = 0; if (X) delete[]X; X = NULL; }; int operator ==(Vectorn C) { if (dim != C.dim) return 0; for (int i = 0; i < dim; i++) if (X[i] != CX[i]) return 0; return 1; } Vectorn operator =(Vectorn C) { if (C == *(this)) { return *(this); }; int i; if (dim < C.dim) { for (i = 0; i < dim; i++) { X[i] = CX[i]; }; } else { for (i = 0; i < C.dim; i++) { X[i] = CX[i]; }; }; }; Vectorn operator +=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] += CX[i]; return *this; }; Vectorn operator -=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] -= CX[i]; return *this; }; Vectorn operator *=(double n) { for (int i = 0; i < dim; i++) X[i] *= n; return *this; }; void Print() { printf("Vector("); for (int i = 0; i < dim; i++) printf("%+lf_", X[i]); printf(")\n"); }; }; int main() { double g[4] = { 7., 5., 8., 0. }; Vectorn C(4); Vectorn D(4, g); C = D; C.Print(); D.Print(); }

 Аварийный останов. Вот код: #include <stdio.h> #include <math.h> #include <cstring> class Vectorn { protected: int dim; double *X; public: Vectorn() { dim = 0; X = NULL; }; Vectorn(int _dim) { dim = _dim; X = new double[dim]; }; Vectorn(int _dim, double *_X) { dim = _dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _X[i]; }; Vectorn(const Vectorn & _C) { dim = _C.dim; X = new double[dim]; for (int i = 0; i < dim; i++) X[i] = _C.X[i]; }; ~Vectorn() { dim = 0; if (X) delete[]X; X = NULL; }; int operator ==(Vectorn C) { if (dim != C.dim) return 0; for (int i = 0; i < dim; i++) if (X[i] != CX[i]) return 0; return 1; } Vectorn operator =(Vectorn C) { if (C == *(this)) { return *(this); }; int i; if (dim < C.dim) { for (i = 0; i < dim; i++) { X[i] = CX[i]; }; } else { for (i = 0; i < C.dim; i++) { X[i] = CX[i]; }; }; }; Vectorn operator +=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] += CX[i]; return *this; }; Vectorn operator -=(Vectorn C) { for (int i = 0; i < dim; i++) X[i] -= CX[i]; return *this; }; Vectorn operator *=(double n) { for (int i = 0; i < dim; i++) X[i] *= n; return *this; }; void Print() { printf("Vector("); for (int i = 0; i < dim; i++) printf("%+lf_", X[i]); printf(")\n"); }; }; int main() { double g[4] = { 7., 5., 8., 0. }; Vectorn C(4); Vectorn D(4, g); C = D; C.Print(); D.Print(); } 

Closed due to the fact that off-topic participants ߊߚߤߘ , freim , andreymal , 0xdb , entithat Jun 18 at 12:16 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - ߊߚߤߘ, freim, andreymal, 0xdb, entithat
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Okay, what's the problem? No one will pick your code. - Vladimir Gordeev
  • excellent, it worked, thanks to all - mr_bunch
  • one
    Yes please ... Although it was possible to choose the "correct" answer - so to speak + in the turnip answered ... Still, they spent their time ... Regards, gote - gote

4 answers 4

In my opinion, it is more correct to use type syntax

CVector3 & operator = (CVector3 t) {... return * this; }

that is, to return not an object, but a link to an object .. And, accordingly, to correct other operators

Well, take objects by reference

CVector3 & operator = (const CVector3 & rhs) {... return * this; }

  • 100% - didn’t see :) - gote

At a minimum, an error in the implementation of the operator = You forgot the returnes, respectively, it is not known what returns ...

Judging by the message, and in my tracing, the error is in this method ...

Generally speaking, according to YOUR code, by calling the method operator = a NEW object of class Vectorn is created and when I delete it and an error occurs.

In my opinion, it is more correct to use type syntax

CVector3 & operator = (CVector3 & t) {... return * this; }

that is, to return not an object, but a link to an object .. And, accordingly, to correct other operators.

Update - corrected the transfer of the parameter - "by reference" ...

    Error message

    free (): invalid pointer

    suggests that with high probability you

    1. or delete a memory that has not yet been allocated;
    2. or delete the memory that has already been deleted.

    Apparently the code

     if (X) delete[]X; X = NULL; 

    does not work as you planned. Try to catch the error with seals or with valgrind.

      But it seems to me that the problem is with the libraries, since everything works for me, as a result we get:

       Vector(+7.000000_+5.000000_+8.000000_+0.000000_) Vector(+7.000000_+5.000000_+8.000000_+0.000000_) 

      And I think you missed return in Vectorn operator = (Vectorn C).