new [size_t] + 是什么意思? 1 次退货

发布于 12-20 12:14 字数 1972 浏览 6 评论 0原文

以下代码示例来自“C++ von A bis Z”(第二版,翻译:C++ from A to Z)一书,第 364 页。该示例是错误的。

// overload operator +=
#include <iostream>
#include <cstring>
using namespace std;

class String {
private:
    char* buffer;
    unsigned int len;

public:
    String(const char* s="") {
        // cout << "Constructor: " << s << "\n";
        len = strlen(s);
        buffer = new char [len+1];
        strcpy(buffer, s);
    }
    ~String() {
        // cout << "Destructor: " << buffer << "\n";
        delete [] buffer;
    }
    String(const String& s) {
        // cout << "Copy_Constructor: " << s.get_buffer() << "\n";
        len = s.len;
        buffer = new char [len+1];
        strcpy(buffer, s.buffer);
    }
    char* get_buffer() const {
        return buffer;
    }

    // returning a reference is more efficent
    // String& operater+=(const String& str1)
    String operator+=(const String& str1) {
        // cout << "Assignment_Operator +=: " << str1.get_buffer() << "\n";
        String tmp(*this);
        delete [] buffer;
        len = tmp.len + str1.len;
        // invalid pointer
        // buffer = new char[len+1];
        buffer = new char [len]+1;
        strcpy(buffer, tmp.buffer);
        strcat(buffer, str1.buffer);
        // wrong return_type
        // return *this;
        return buffer;
    }
};

int main(void) {
    String string1("Adam");
    String string2("Eva");
    string1+=" und ";
    string1.operator+=(string2);
    cout << string1.get_buffer() << "\n";
    return 0;
}

带评论的行是我的“修复”。现在我想知道“new char [len]+1”的作用是什么?我认为如下:

  • 它从堆中分配 sizeof(char)*len 内存
  • 并将错误的地址返回到指针 *buffer
  • 但错误的地址是什么:“堆上新内存的首地址 + 1”或“堆上的新内存 + sizeof(char)*1)

会发生什么? 谢谢

//编辑 谢谢大家!你帮了我! 我只是想知道,这个语句会返回什么。

new char [len]+1;

当然,这句话本身是本书作者的拼写错误。

The following sample of code if from a book "C++ von A bis Z" (second edition, translation: C++ from A to Z) at page 364. The sample is wrong.

// overload operator +=
#include <iostream>
#include <cstring>
using namespace std;

class String {
private:
    char* buffer;
    unsigned int len;

public:
    String(const char* s="") {
        // cout << "Constructor: " << s << "\n";
        len = strlen(s);
        buffer = new char [len+1];
        strcpy(buffer, s);
    }
    ~String() {
        // cout << "Destructor: " << buffer << "\n";
        delete [] buffer;
    }
    String(const String& s) {
        // cout << "Copy_Constructor: " << s.get_buffer() << "\n";
        len = s.len;
        buffer = new char [len+1];
        strcpy(buffer, s.buffer);
    }
    char* get_buffer() const {
        return buffer;
    }

    // returning a reference is more efficent
    // String& operater+=(const String& str1)
    String operator+=(const String& str1) {
        // cout << "Assignment_Operator +=: " << str1.get_buffer() << "\n";
        String tmp(*this);
        delete [] buffer;
        len = tmp.len + str1.len;
        // invalid pointer
        // buffer = new char[len+1];
        buffer = new char [len]+1;
        strcpy(buffer, tmp.buffer);
        strcat(buffer, str1.buffer);
        // wrong return_type
        // return *this;
        return buffer;
    }
};

int main(void) {
    String string1("Adam");
    String string2("Eva");
    string1+=" und ";
    string1.operator+=(string2);
    cout << string1.get_buffer() << "\n";
    return 0;
}

The lines with the comments are my "fixes". Now I want to know what "new char [len]+1" does? I think the following:

  • it allocates sizeof(char)*len memory from heap
  • and returns the WRONG address to the pointer *buffer
  • but what is the wrong address: "first address of the new memory on heap + 1" or "first address of the new memory on heap + sizeof(char)*1)?

What happens?
Thanks

// edit
Thank you all! You helped me!
I just wanted to know, what this statement will return.

new char [len]+1;

The line itself is, of course, a typo from the author of the book.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

红尘作伴2024-12-27 12:14:55

让我们分解一下:

new char[len];

返回一个指向 char 数组的指针。

new char[len] + 1;

返回内存中的下一个地址。

它基本上切断了第一个字符。

编辑:正如其他人提到的,这很可能是一个拼写错误,它应该是 new char[len+1]。我只是解释代码的作用,但是如果您确实知道自己在做什么,则应该只使用指针算术。正如 cHao 所指出的,尝试删除返回的指针将是 UB。如果 len == 1 并尝试使用返回的指针,您还将获得 UB。

Let's break it down:

new char[len];

returns a pointer to an array of char.

new char[len] + 1;

returns the next address in memory.

It's basically cutting off the first character.

EDIT: As others have mentioned, this is most probably a typo, it should be new char[len+1]. I'm just explaining what the code does, but you should only use pointer arithmetics if you really know what you're doing. Trying to delete the returned pointer would be UB, as cHao pointed out. You'll also get UB if len == 1 and attempt to work with the returned pointer.

中二柚2024-12-27 12:14:55

如果将整数 i 添加到 T*,则会将 sizeof(T) * i 添加到指针。所以在这种情况下,由于 new char[len] 返回一个 char*+ 1 确实会添加 sizeof(char) * 1 到它。

If you add an integer i to a T*, this will add sizeof(T) * i to the pointer. So in this case, since new char[len] returns a char*, + 1 will indeed add sizeof(char) * 1 to it.

源来凯始玺欢你2024-12-27 12:14:55

new Type[size] + 1 将分配一个大小为 size 的数组,并生成索引为 1 的元素的地址 - 即第二个元素。没什么特别的,只是指针算术。 new[] 将产生索引为 0 且大小为 +1 的元素的地址,在该地址上完成它会产生索引为 1 的元素的地址>。

new Type[size] + 1 will allocate an array of size size and yield the address of the element with index 1 - that the second element. Nothing special, just pointer arithmetic. new[] would yield the address of the element with index 0 and size +1 is done on that address it yields address of element with index 1.

春夜浅2024-12-27 12:14:55

它只是返回指向第二数组项的指针 =)
阅读有关 C 指针的内容;)

It simply returns the pointer to the second array item =)
Read about C pointers ;)

囍笑2024-12-27 12:14:55

+sizeof(char)*1,但我不明白你为什么这样做。

+sizeof(char)*1, but I failed to see why did you do it.

背叛残局2024-12-27 12:14:55

我认为这是一个拼写错误,应该是new char [len+1]+1 是必须存在的字符串终止符。

I think it's a typo, and it should be new char [len+1]. The +1 is for the string terminator character that must exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文