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?

  • You can comment on the code in main c from the end and collect the Release version until it stops falling. This will indicate exactly where the problem is, from there and dance. - fogbit
  • you have incorrectly implemented operator + = (String &). Be careful when working with pointers, see what you copy / assign to - Yury Shadchnev 2:46 pm
  • Guys, well, you're smart like that :). I understand that in this error, but where exactly? - VladislavMSK
  • one
    ERROR FOUND! In the first function, I assigned s = s and I had to t = s. Thanks to all. - VladislavMSK

1 answer 1

This is not a very pleasant debugging error.

Heap corruption check works like this. When allocating memory, space is allocated slightly more than necessary. When deleting, it is checked whether the contents of an extra piece of allocated memory has changed. Therefore, such an error is detected only when it is deleted, and it is not illuminated where it is located. It should be searched in cycles (error by one, for example i <= n, when i <n is necessary), memcpy (copying outside the block), strcpy and strcat (it’s harder here, you need to know the length of the string).

As you already understood, the error in these lines:

  strcpy(s,s); strcat(s,str.s); 

This is where you write beyond the s buffer.

PS Why do you write your own class of strings? The task is, or decided to do it yourself for training? For practice it is better to use std :: string. It works faster, although this is not the limit. I wrote my line even faster. To improve performance, I advise you to replace strcpy and strcat with memcpy. Just do not forget about adding zero to the end.

  • I study Visual C ++, this is a DZ - you need to make all sorts of options for adding lines and getting their length. - VladislavMSK