Why can the new way create an array of strings?

I saw code that uses string arrays as follows.

string *pointer = new string[runtimeAmmount]; 

I also saw individual characters in a string accessible as follows.

 string aString = "this"; char bString[] = "that"; bString[3] = aString[3]; 

The above will result in a bString equal to "thas". This assumes that the string is actually a pointer to the location of the first character. However, the string still has member functions that are accessed as "string.c_str ()", which means that it itself does not comply with the rules of the pointer. How does it all work?

Note. My initial question should be different, but I figure it out by typing it. If someone could answer my initial question just for verification, I would appreciate it. My initial question is this: How can an array of strings be new'd if each row can vary in length throughout its entire life cycle? Won't lines interact with each other?

Answer: I came up with: Strings contain pointers to C-style arrays in some way, so objects occupy a given number of spaces.

OR

Lines are something like an STL template template, which I have not yet found the time to look at.

+4
source share
3 answers

I will talk about what happens in each of the four lines of code in your question, but first I have to say that your conclusion is not accurate. You are "tricked" by overloading the statement built into the string class. Although it is likely that internally, the string class supports an array of C-style characters, it is encapsulated and string is and should be treated as an opaque object other than a C-style string .

Now for each of your lines:

 string *pointer = new string[runtimeAmmount]; 

In this line, pointer set to point to the newly allocated array of (empty) string objects. runtimeAmmount - the number of lines in the array, not the number of characters in a C-style string

 string aString = "this"; 

This line builds a new empty line using the constructor of the (implicit) conversion from the class string : string(const char *) . (Note that in the context of a non-construct, for example aString = "this"; operator=(const char *) overload of the string class will be used.)

 char bString[] = "that"; 

This is a typical C string, treated as an array of characters.

 bString[3] = aString[3]; 

This uses the overloaded operator[] of the string class to return the character (reference), and then assigns it to the third character spot in the C-style character array.

Hope this helps.

+8
source

Your intuition is correct.

A string in C ++ is an object and may contain a pointer to another store. (They don’t need Google’s “small string optimization” if you want to understand why not — but this is a leak.)

If you think of the string as a structure that looks like this:

 struct str { int len; // number of bytes allocated char *data; // pointer to the data }; 

then you can see how the string can work. Note that std :: string is actually a lot more complicated than that; but that should understand the idea.

Regarding templates, std::string is an instance of std::basic_string specialized for char (unlike std::wstring which is specialized for wchar_t )

+3
source

When you do this:

 string *pointer = new string[runtimeAmmount]; 

an array of string objects is created. Each of them is built with the constructor string::string , which can do everything that it needs. In this case, string has a pointer to memory, which is initialized in the constructor. So your array is like an array of char * pointers, each of which is separated separately.

+3
source

All Articles