Hello! When writing a program, debugging is successful, but during operation, an error occurs and writes that the heap is damaged. VS 2010 SP 1.
Code: usestring.cpp
#include <iostream> #include "String.h" using namespace std; int main() { String a("Hello, world!"); cout << "String a: \""; a.Print(); cout << "\", Len = " << a.Len() << endl; //=============================================== String b=a; cout << "String b: \""; b.Print(); cout << "\", Len = " << b.Len() << endl; //===================================================== b=a; //b.operator=(a); cout << "String b: \""; b.Print(); cout << "\", Len = " << b.Len() << endl; //===================================================== b=b; //ΡΠ°ΠΌΠΎΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΠ΅ //b.operator=(b); cout << "String b: \""; b.Print(); cout << "\", Len = " << b.Len() << endl; //================================================ a += b; cout << "String a: \""; a.Print(); cout << "\", Len = " << a.Len() << endl; // a = a + b; //================================================== String *p = new String ("Good Bye!"); cout << "String in: \""; p->Print(); cout << "\", Len = " << p->Len() << endl; delete p; system("pause"); } String.cpp:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include "String.h" using namespace std; String & String :: operator+=(const String &str) { char *t = new char[n+str.n+1]; //Π·Π΄Π΅ΡΡ Π΄.Π±. ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΎΡΠΈΠ±ΠΊΠ° strcpy(s,s); strcat(s,str.s); delete[] s; s = t; n += str.n; return *this; } String & String :: operator = (const String &str) { if(this == &str) //Π·Π°ΡΠΈΡΠ° ΠΎΡ ΡΠ°ΠΌΠΎΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ { return *this; } else { delete[] s; } n = str.n; s = new char[n+1]; //Π·Π΄Π΅ΡΡ Π΄.Π±. ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΎΡΠΈΠ±ΠΊΠ° strcpy(s,str.s); return *this; } //====================================================== String ::String(const String &str) { n = str.n; s = new char[n+1]; //Π·Π΄Π΅ΡΡ Π΄.Π±. ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΎΡΠΈΠ±ΠΊΠ° strcpy(s,str.s); } String ::String(const char *str) { n = strlen(str); s = new char[n+1]; //Π·Π΄Π΅ΡΡ Π΄.Π±. ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΎΡΠΈΠ±ΠΊΠ° strcpy(s,str); } String :: ~String() { delete[] s; } void String :: Print() const { cout<<s; } String.h:
#ifndef STRING_H #define STRING_H class String { public: String & operator+=(const String &); String & operator = (const String &); String(const String &); String(const char *str=""); ~String(); int Len() const {return n;} void Print() const ; private: char *s; int n; }; #endif Error (no problem when debugging):
"String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Users\Vladislav\Documents\Visual Studio 2010\Projects\String\Debug\String.exe", Π‘ΠΈΠΌΠ²ΠΎΠ»Ρ Π·Π°Π³ΡΡΠΆΠ΅Π½Ρ. "String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Windows\SysWOW64\ntdll.dll", ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π½Π°ΠΉΡΠΈ ΠΈΠ»ΠΈ ΠΎΡΠΊΡΡΡΡ ΡΠ°ΠΉΠ» PDB "String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Windows\SysWOW64\kernel32.dll", ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π½Π°ΠΉΡΠΈ ΠΈΠ»ΠΈ ΠΎΡΠΊΡΡΡΡ ΡΠ°ΠΉΠ» PDB "String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Windows\SysWOW64\KernelBase.dll", ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π½Π°ΠΉΡΠΈ ΠΈΠ»ΠΈ ΠΎΡΠΊΡΡΡΡ ΡΠ°ΠΉΠ» PDB "String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Windows\SysWOW64\msvcp100d.dll", Π‘ΠΈΠΌΠ²ΠΎΠ»Ρ Π·Π°Π³ΡΡΠΆΠ΅Π½Ρ. "String.exe": ΠΠ°Π³ΡΡΠΆΠ΅Π½ΠΎ: "C:\Windows\SysWOW64\msvcr100d.dll", Π‘ΠΈΠΌΠ²ΠΎΠ»Ρ Π·Π°Π³ΡΡΠΆΠ΅Π½Ρ. HEAP[String.exe]: Heap block at 00294910 modified at 0029494A past requested size of 32 ΠΠ‘ Windows ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°Π»Π° ΡΠΎΡΠΊΡ ΠΎΡΡΠ°Π½ΠΎΠ²Π° Π² String.exe. ΠΡΠΎ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π²ΡΠ·Π²Π°Π½ΠΎ ΠΏΠΎΠ²ΡΠ΅ΠΆΠ΄Π΅Π½ΠΈΠ΅ΠΌ ΠΊΡΡΠΈ ΠΈ ΡΠΊΠ°Π·ΡΠ²Π°Π΅Ρ Π½Π° ΠΎΡΠΈΠ±ΠΊΡ Π² String.exe ΠΈΠ»ΠΈ Π² ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΡ
ΠΈΠΌ DLL. ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΡΠΈΡΠΈΠ½ΠΎΠΉ ΡΠ°ΠΊ ΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π½Π°ΠΆΠ°ΡΠΈΠ΅ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΌ ΠΊΠ»Π°Π²ΠΈΡΠΈ F12, ΠΊΠΎΠ³Π΄Π° ΡΠΎΠΊΡΡ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ String.exe Can you please tell me how the heap was damaged :)? And how to fix it?