From 240fc8f634e9526aaeecd807e9c0d2d2806e4ce0 Mon Sep 17 00:00:00 2001 From: lensferno Date: Fri, 8 Apr 2022 21:19:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E9=AA=8C=E4=B8=89=EF=BC=9A=E9=81=97?= =?UTF-8?q?=E6=BC=8F=20=E4=BF=AE=E5=A4=8Dvs=E4=B8=8B=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=BA=A2=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work20220407/string/String.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/work20220407/string/String.cpp b/work20220407/string/String.cpp index 402c027..6e4a58c 100644 --- a/work20220407/string/String.cpp +++ b/work20220407/string/String.cpp @@ -112,10 +112,12 @@ void String::copyData(const char *source, int sourceLength) { this->data = nullptr; length = 0; allocSize = 0; + + return; } // 如果源字符串长度超过了本对象已分配的字符数组大小则重新分配长度合适的字符数组 - if (allocSize < sourceLength) { + if (allocSize <= sourceLength) { allocSize = sourceLength + 1; delete[] data; @@ -155,7 +157,7 @@ void String::addString(const char *source, int sourceLength) { } // 如果拼接后字符长度超过已分配的内存大小,则重新分配data内存大小 - if (allocSize < (length + sourceLength + 1)) { + if (allocSize <= (length + sourceLength + 1)) { // 保存拼接前的原数据,以便重新分配足够的内存 char *old = new char[length + 1](); strcpy(old, data);