C++ 在正常块crt检测到应用程序 \define\u CRT\u SECURE\u NO\u警告 #包括 #包括 使用名称空间std; 类MyString { char*str; 整数大小; 公众: MyString() { str='\0'; 尺寸=1; } MyString(常量字符*常量s) :str(新字符[字符串]) { 尺寸=strlen(s)+1; strcpy(str,s); } MyString(constmystring&other) :str(新字符[另一个.size]) { 尺寸=另一个尺寸; 对于(int i=0;i
一个问题是默认构造函数中成员的初始化:C++ 在正常块crt检测到应用程序 \define\u CRT\u SECURE\u NO\u警告 #包括 #包括 使用名称空间std; 类MyString { char*str; 整数大小; 公众: MyString() { str='\0'; 尺寸=1; } MyString(常量字符*常量s) :str(新字符[字符串]) { 尺寸=strlen(s)+1; strcpy(str,s); } MyString(constmystring&other) :str(新字符[另一个.size]) { 尺寸=另一个尺寸; 对于(int i=0;i,c++,C++,一个问题是默认构造函数中成员的初始化: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> using namespace std; class MyString { char* str; int size; public: MyString() { str = '\0'; size = 1; } My
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
char* str;
int size;
public:
MyString()
{
str = '\0';
size = 1;
}
MyString(const char* const s)
: str(new char[strlen(s)])
{
size = strlen(s) + 1;
strcpy(str, s);
}
MyString(const MyString& another)
: str(new char[another.size])
{
size = another.size;
for (int i = 0; i < size; i++)
str[i] = another.str[i];
}
~MyString()
{
delete[] str;
}
void set(const char* st)
{
size = (int)strlen(st) + 1;
str = new char[size];
for (int i = 0; i < size; i++)
str[i] = st[i];
}
bool isEqual(const MyString& other) const
{
if (size != other.size)
return false;
if (strcmp(str, other.str) == 0)
return true;
else
return false;
}
void print() const
{
for (unsigned int i = 0; i < size; i++)
cout << str[i];
cout << endl;
}
};
int main() {
MyString strs[] = {
MyString("C"),
MyString(),
MyString("Java")
};
strs[1].set("C++");
const int arraySize = sizeof(strs) / sizeof(MyString);
const MyString target("Java");
for (int i = 0; i < arraySize; i++) {
const MyString str(strs[i]); // copy constructor
if (str.isEqual(target)) {
cout << "[" << i << "]: ";
str.print();
break;
}
}
for (const MyString& str : strs) {
str.print();
}
}
字符文字“\0
等于零,这等于空指针。该赋值等于:
str = '\0';
size = 1;
但是你仍然说字符串的大小是1
,这是错误的,因为没有字符串,大小应该是零
另一个(可能是您的问题的罪魁祸首)问题是,在构造函数
MyString(const char*const s)
中,您使用strlen
获取分配的C样式空终止字符串的长度,但您忘记了strlen
不计算空终止符
这意味着分配太小,当您使用strcpy
复制字符串时,它会将空终止符写入分配内存的范围之外
此外,您在其他地方使用null结束符计数,实际上设置大小包括空终止符(即使在<代码> MyString(const char * const s)构造函数中)。这违背了字符串的普通C++语义,其中大小不包括终止符。
这可能会导致进一步的问题,比如在
print
函数中实际打印空终止符。默认构造函数不应使用最新的GCC编译器编译,只能将null
或nullptr
分配给指针
MyString(){
str=nullptr;
尺寸=0;
}
这个构造函数需要额外的一个1字节来分配内存,这样就可以避免缓冲区溢出
MyString(常量字符*常量s):str(新字符[strlen(s)+1]){
尺寸=strlen(s)+1;
strncpy(str,s,size);
}
strncpy
是strcpy的更安全的替代品,我建议您使用这个
您还需要为操作符=
添加实现,否则一旦使用操作符=
复制对象,编译器生成的实现将导致内存泄漏
MyString&operator=(const MyString&other){
if(&other==this)返回*this;
删除[]str;
str=新字符[other.size];
尺寸=另一个尺寸;
//最好在这里使用strncpy
对于(inti=0;i
本文讨论了一个类似的字符串实现,您也可以参考其中的代码
ahhhhhhhyyyyyyy非常感谢。我用初始值设定项str(new char[strlen(s)+1])更正了我在MyString(const char*const s)构造函数中的代码。这是一个简单的错误……非常感谢,祝你过得愉快!-来自韩国学生:)@iamianpark很高兴听到这个消息!非常感谢!我还在构造函数MyString中更正了我的代码(const char*const s):str(new char[strlen(s)+1])像这样!我认为这个初始值设定项是主要的问题。谢谢你的好评论,祝你愉快!-来自韩国
str = nullptr;