c++ - Debug Assertation Failed - Dont find bad code -
I wrote a class to use for C ++ when I try to delete an object, then debugger "Debug Essay Failed" with _BLOCK_TYPE_IS_VALID (pHead-> nBlockUse) throws.
Perhaps someone can correct my mistake in debugging all memory addresses.
Lines. H
#ifndef LINES_H # Define LINES_H #include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; using namespace std; Square lines {public: Lines (fuse): Fulexacon {'#'}, name {"Hans-Peter"}, llegence {_laenge} {}; Lines (int lelegages, four_fuelzechen): Fulezeken {_fuellzeichen}, will bring {_laenge}, name {"Hans-Peter"} {}; Lines (int lalejes, four _Fuillezichen, four * _name): Fuelezeken {_fuellzeichen}, llegence {_laenge}, name {_name} {}; Lines (Constant & amp; _l); ~ Lines (); Lines & amp; Operator = (Constant & amp; _l); Private: Charles Felixchenen; Will bring int; Four names; }; #endif // LINES_H
lines CPP
#include "lines." Lines :: Lines (Constant Lines; _L) {fuellzeichen = _l.fuellzeichen; Langes = _ l. lange; Int len = strlen (_l.name) + 1; Name = new four [lane]; Strncpy (name, _l.name, len); } Lines & amp; Lines :: operator = (Constant & amp; _L) {if (this == & amp; _l) {return * it; } Lines TMP (_l); Swap (Fuelezchen, TMP. Fuelzeken); Swap (name, tmp.name); Swap (llange, tmp. Lagain); Return * This; } Lines :: ~ Lines () {Delete} [] Name; }
main.cpp
#include "lines.h" ent main (int argv, char * args [] ) {Lines A (20, '#'); Lines B (A); }
do not delete delete []
Lines (Intl_Lange): Fualechenne {'#'}, name {"Hans-Peter"}, you new []
!
take {_laenge} {}; Lines () {delete] [] Name; }
Take a look at the above two rows:
- ctor digit
name
string-literal
The easiest and best solution is to use a
std :: string
instead of managing your own memory.
This will also allow you to follow the rule-zeroAnother bug , or at least bug-waiting is to: A-logic-ctor should be
clear
, so it is not considered for a deeper conversion.On the one hand, consider using default-logic to simplify your code. In addition, use the
const
where appropriate:clear lines (int _lange, four _fualezeken = '#', const char * _name = "swan-peter" ): Fulexacon {_fuellzeichen}, will bring {_laenge} {int Len = strlen (_name) +1; Name = new four [lane]; Memcpy (name, _name, lane); };
As you are using copy-and-swap for assignments, consider typing the free-function
swap
and going through the value:in lines & amp; Lines :: operator = (rows _l) {swap (this, & amp; _l); Return * This; } Zero swap (lines and a, lines and b) {std :: swap; Swap (a.fuellzeichen, b.fuellzeichen); Swap (a.name, b.name); Swap (a.laenge, b.laenge); }
(
line
andoperator =
should be defined in the class, and should beswap
Be declared in that header.)
Comments
Post a Comment