使用"new"创建字符数组会生成比我指定的更多的字符.C++
Creating a character array with `new` generates more characters than I specify. C++
所以我的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终止。我以为我的电脑确实出了问题,但没有,只是调试器出了问题。感谢所有关于这方面的额外投入;它真的帮我弄清楚了一些事情!
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存