实验三:修补

main
lensfrex 2 years ago
parent f01a0d08e6
commit b36a65b23f
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 11
      work20220407/string/String.cpp

@ -15,9 +15,9 @@ String::String(int size) : length(0), allocSize(size) {
}
/**
* 64
* 128
*/
String::String() : String(64) {}
String::String() : String(128) {}
/**
*
@ -160,18 +160,17 @@ void String::addString(const char *source, int sourceLength) {
char *old = new char[length + 1]();
strcpy(old, data);
// 重新分配内存,防止拼接后溢出,2为两个横杠大小
length += sourceLength;
allocSize = length + 1;
// 重新分配内存,防止拼接后溢出
delete[] data;
allocSize = length + sourceLength + 1;
this->data = new char[allocSize]();
strcpy(data, old);
}
strcat(data, source);
length += sourceLength;
length = allocSize - 1;
}
/**

Loading…
Cancel
Save