String Management in C ++

I decided to return to C ++ after some time spent in Java, and now I'm completely confused about how strings work in C ++.

First, suppose we have a function:

void fun() { int a = 1; Point b(1,2); char c[] = "c-string"; } 

As I understand it, a and b are allocated on the stack. c (pointer) is also allocated on the stack, but the content ("c-string") lives happily on the heap.

Q1: Is content c automatically released after the fun function completes?

Secondly, let us have a C ++ string :

 void fun2() { (1) string s = "c++ string"; (2) s += "append"; (3) s = "new contents"; (4) s = "a" + s + "c"; } 

String documentation is not too specific as to how strings work, so here are the questions:

Q2: Is the contents of s automatically freed upon completion of fun2 ?

Q3: What happens when we combine two lines? Should I care about memory usage? (line 2)

Q4: What happens when we overwrite the contents of a line (line 3) - but what about memory, should I worry? Is the originally allocated space reused?

Q5: What if I build such a line (line 4). It is expensive? Are string literals ( "a" , "c" ) a pool (for example, in Java) or are they repeated throughout the executable?

Ultimately, I'm trying to learn how to use strings correctly in C ++.

Thanks for reading this,
Queequeg

+4
source share
4 answers

As I understand it, a and b are allocated on the stack. c (pointer) is also allocated on the stack, but the content ("c-string") lives happily on the heap.

This is wrong, they all live in automatic memory (stack). Even a char array. In C ++, a string is an object of type std::string .

Q1: Content c is automatically freed when fun function ends?

Yes.

Q2: Is the contents of s automatically freed upon completion of fun2?

Yes.

Q3: What happens when we combine two lines? Should I care about memory usage? (line 2)

They are combined and memory is automatically controlled. (assuming we're talking about std::string , not char[] or char* .

Q4: What happens when we overwrite the contents of a line (line 3) - but what about memory, should I worry? Is the originally allocated space reused?

Detail implementation. It can be reused, it can be reallocated if the previous memory cannot contain new content.

Q5: What if I build such a line (line 4). It is expensive? Are string literals ("a", "c") combined (for example, in Java) or repeated throughout the executable?

String literals can be combined, but this is not required. For large concatenation, std::stringstream usually used instead (similarly to Java). But first profile, do not do premature optimizations. Not that neither of these are string literals.

 char* pStr = "this is a string literal"; 

It is in read only memory and cannot be changed.

Ultimately, I'm trying to learn how to use strings correctly in C ++.

Use std::string .

+8
source

c not a pointer. This is an array. You can say that it is an array because it has square brackets, while pointers have an asterisk. Since c is an automatic variable, it does not require any manual lifetime or memory management.

The Q2: s declaration is also an automatic variable, and since it has a well-designed class type, this means that you don’t have to worry about anything.

Ad Q3: Local string objects are appropriately modified to contain a new string; in this process, temporary string objects may or may not exist for the duration of the concatenation expression. (This only applies to line 4; line 2 does not have a temporary number.)

Q4 Announcement: Everything is in order and works as expected; see Q2. The source memory may or may not be used, depending on the destination details. In your example, the original memory is likely to be overwritten; in case like s = std::string("hello"); The buffers of two lines are likely to be replaced.

Ad Q5: String literals are read-only global constants that the compiler can implement in any way that it likes. Details are not that important; you will definitely get the desired string object in s . See Temporary objects Q3.

To "learn how to use strings in C ++", just use and use them. Treat them as integers. It will be right. The beauty of the standard library is that you really don't need to know how everything works; when you use standard library classes in C ++ idiomatic mode, all resource management is done automatically and efficiently for you.

+8
source

Q1: Yes, it is canceled. An array of characters is on the function stack.

Q2: Yes, std::string takes care of releasing all its resources upon destruction, which happens when leaving the area, as for all automatically assigned variables.

Q3: No, do not worry if profiling does not tell you what you should.

Q4: You do not have to worry, and the original space may or may not be reused. In any case, all the space used by the strings is canceled when the function exits.

Q5: Given the optimization, the compiler is allowed to do the only way to determine if X is more expensive than Y in order to profile both of them.

+1
source

Q1 It should be noted that the literals "c++ string","append","new contents","a","c" are in static memory

0
source

Source: https://habr.com/ru/post/1432202/


All Articles