使用"new"创建字符数组会生成比我指定的更多的字符.C++

Creating a character array with `new` generates more characters than I specify. C++

本文关键字:字符 quot C++ 创建 new 数组 使用      更新时间:2023-10-16

所以我的CS类有一个作业,这给我带来了一些麻烦。目标是在不使用任何cstring函数的情况下创建自己的非NULL终止的String类。类中的数据必须包含字符串的Length,一个名为"data"的char*,以及一些与我的问题无关的其他内容。

因此,当我为字符串分配内存时,我调用MyStrCopy(MyString* tar, const char* str)函数,该函数使用tar->data = new char[length]来分配内存,"length"是作为str传入的cstring的长度,它按预期工作。然而,当以这种方式分配内存时,数组总是比我指定的要大得多(例如,我要求6个字节,得到的字节数超过了11个),而且我得到的字节数量似乎是随机的,每次运行都不同。我试着写一个函数来剔除不需要的字符,但我认为我只是缺乏如何做到这一点的技能/知识。

这些额外的数据让我的几个函数变得不正常,我一直在思考如何修复它。有什么想法吗?

我已将我的类别定义为低于

#include <iostream>
#pragma once
class MyString {
private:
char* data;
int length;
static bool printAsUppercase;
int getLengnth(const char* str);
void MyStrCopy(MyString* tar, const char* str);
public:
// Constructors
MyString();
MyString(const char* data);
MyString(MyString& data2Copy);
~MyString();
// Operator Overloads
// Assignment Operator
MyString operator=(const MyString& data);
MyString operator=(const char* data);
// Arithmetic Operators 
MyString operator+(const MyString& rightStr);
// Pre/Post decrement
MyString operator--();
MyString operator--(int);
// Boolean Operators
friend bool operator==(const MyString& leftStr, const MyString& rightStr);
friend bool operator>(const MyString& leftStr, const MyString& rightStr);
friend bool operator<(const MyString& leftStr, const MyString& rightStr);
// Streaming Operators
friend std::ostream& operator<<(std::ostream& os, const MyString& str);
friend std::istream& operator>>(std::ostream& is, MyString& str);
// Mutators
MyString& uppercase();
void cull();
// Accessors
int getLengnth();
};

下面是实现。注意:目前大部分都没有达到预期效果。

#include "MyString.h"
// Constructors
MyString::MyString() {
data = NULL;
length = 0;
}
MyString::MyString(const char* data) {
MyStrCopy(this, data);
}
MyString::MyString(MyString& data2Copy) {
MyStrCopy(this, data2Copy.data);
}
MyString::~MyString() {
delete[] data;
}
MyString MyString::operator=(const MyString& data) {
MyString temp;
MyStrCopy(&temp, data.data);
return temp;
}
MyString MyString::operator=(const char* data) {
MyString temp;
MyStrCopy(&temp, data);
return temp;
}
void MyString::MyStrCopy(MyString* tar, const char* str) {
// WIP Something's not right with the NEW line
tar->length = getLengnth(str);
if (data != NULL)
delete data;
tar->data = new char[length];
for (int i = 0; i < tar->length; i++)
tar->data[i] = str[i];
tar->cull();
}
void MyString::cull() {
// WIP currently does effectively nothing
int currLen = getLengnth(data);
while (currLen > length)
data[currLen--];
}
int MyString::getLengnth() {
return length;
}
int MyString::getLengnth(const char* str) {
int len = 0;
while (str[len] != NULL)
len++;
return len;
}

提前感谢!

MyString::MyString(MyString& data2Copy) {
MyStrCopy(this, data2Copy.data);
}

这基本上是默认构造MyString的新实例,在调用MyStrCopy()之前不初始化其任何成员。在MyStrCopy:中

if (data != NULL)
delete data;

由于data和新类的任何其他成员都没有初始化——出于上述原因,这里的data将是随机垃圾,从现在起,它都是未定义的行为。

你的意思是做:

new char[tar->length];

length属性没有初始化,所以您会得到未定义的行为,尽管我会在调试器中进行检查。这是一些混乱的代码,因为MyStrCopy是一个非static函数,您不应该用显式tar参数来调用它,这隐含为this

调用这个函数copy(const char* data)而不是误导性的类名样式MyStrCopy会更有意义。只传递您需要的内容,如data,并直接使用属性,而不是通过参数间接使用。

记住,你可以用另一个构造函数来定义构造函数:

MyString(const char* data);
MyString(const MyString& src) : MyString(src.data) { };

其中copy函数完全消失,因为它只是第一个构造函数的一部分。

这里也有一些打字错误,比如getLengnth,所以一定要仔细检查所有内容。

因此,事实证明,我在调试器中获得的额外字符是调试器试图通过继续打印字符来提供帮助,直到它找到NULL,这可能在任何地方,因为我不允许NULL终止。我以为我的电脑确实出了问题,但没有,只是调试器出了问题。感谢所有关于这方面的额外投入;它真的帮我弄清楚了一些事情!