-2

I am just wondering how memory handling is done for a string object in c++.

I have below code:

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    string str = new char[30];
    str = new char[60];
    
    delete[] str;
    return 0;
}

This piece of code will definitely throw an error, but can someone explain me in detail why this is an error ? And will the above declaration assign a total of 90 bytes to str ?

Thanks in advance

  • 1
    it will not compile. `delete[] str;` does not make sense. `std::string` is not just an array of `char`s – 463035818_is_not_an_ai Jun 09 '22 at 12:56
  • 3
    The error is because the whole purpose of using `std::string` is for it to handle all memory management, so how did you come to a conclusion that you still need to `new` anything? What, specifically, in your C++ textbook suggested that? – Sam Varshavchik Jun 09 '22 at 12:57
  • 1
    `std::string` handles its memory internally, so not only is `string str = new char[30];` wrong (since the newly created array is not a null-terminated string) it also leaks the memory you just allocated. – UnholySheep Jun 09 '22 at 12:57
  • `string str = new char[30];` does compile but it is undefined behavior. It calls constructor (5) (https://en.cppreference.com/w/cpp/string/basic_string/basic_string) but the char array is not null terminated. – 463035818_is_not_an_ai Jun 09 '22 at 12:58
  • 1
    don't try to learn c++ by guessing. Seeing your code it is somewhat obvious that you are in need of a good book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Jun 09 '22 at 12:59
  • *I am just wondering how memory handling is done for a string object in c++.* -- You find out by looking at the `` header and look at the code for the various `basic_string` functions. You don't find out by erroneously calling `new[]`. – PaulMcKenzie Jun 09 '22 at 13:02
  • 1
    If you are learning modern C++ properly, for example with one of the books suggested above, you will find that you almost never need to use `new`/`delete` for anything (and you shouldn't). When you see a C++ type like `string` your first assumption should be that it handles all the memory it needs by itself. Other cases are the exception and often indicate bad (or out-dated) design. [C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) – user17732522 Jun 09 '22 at 13:22

1 Answers1

2

std::string allocates it's memory internally. That's the whole point, it does the memory handling for you. So if you want to allocate a string of thirty characters you do it like this

string str(30, ' ');

This creates a string of 30 spaces. The memory allocated will automatically be deallocated when the string is no longer being used.

The string constructor your code used is actually meant to create C++ strings from C strings. E.g.

string str("hello");

This creates a string of length 5 and initialises it to 'hello'. Again any memory allocated will automatically be deallocated when the string is no longer used.

This is the point of the std::string class, to make things easy. Easier than you were prepared to believe apparently.

john
  • 85,011
  • 4
  • 57
  • 81