简体   繁体   中英

What does new [size_t] + 1 return

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.

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.

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.

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 .

它只是将指针返回到第二个数组item =)了解C指针;)

+sizeof(char)*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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM