Some questions about the lifespan of a string object

Trying to understand the C ++ string a little more. The compiler here is g ++ 4.7.3.

Question 1: In the following code snippet, would the compiler be smart enough to free user data (on the heap, implicitly indicated by s) at the end of the function? I think yes, just want to confirm.

void dummy() {
    string s;
    s.append("hello");
    //more append
}

      

Question 2: In the following snippet, the compiler will not release the user data it points s

to when the function returns. It is right? If so, the user data can be freed in the caller by the compiler (unless the caller function itself returns a string object).

string dummy() {
    string s;
    s.append("hello");
    //more append
    return s;
}
//some caller
string s1 = dummy();

      

+3


source to share


2 answers


In question 1, yes, the memory for the variable s

will be freed when the function ends dummy

, because it was the enclosing scope of the variable s

.



In question 2, the compiler will most likely use return value optimizations to avoid having to copy memory from s

to s1

, as it s

is about to go out of scope.

+3


source


Question 1: In the following code snippet, would the compiler be smart enough to free user data (on the heap, implicitly pointed to s) at the end of the function?

Yes, it is necessary. At the end of the function scope, the string destructor is called, which will release the allocated memory.

An object s

is said to have an "automatic storage duration" and it is described by the standard through §3.7.3 / 1:

Scope variables explicitly declared by case or explicitly declared static or external have automatic storage duration. The storage for these objects continues until the block in which they are created is completed.




Question 2: In the following snippet, the compiler will not release the user data pointed to by s when the function returns. It is right? If so, the user data can be freed in the caller by the compiler (unless the caller function itself returns a string object).

The compiler can delete the copy there using the RVO (Return Value Optimization) function. Specifically, the standard words are as follows, in §12.8 / 31:

This permission for copy / move operations, called copy, is allowed in the following cases (which can be combined to eliminate multiple copies):

  • in a return statement in a function with a return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy / move operation can be omitted by constructing an automatic an object directly into the return value of functions
  • in a throw expression (5.17) when the operand is the name of a non-volatile automatic object (excluding a function or catch-clause parameter) that does not extend beyond the innermost shell, block (if any), copy / move operation from operand to object exceptions (15.1) can be omitted by creating an automatic object directly into the exception object
  • when an object of a temporary class that has not been bound to a reference (12.2) is copied / moved to a class object with the same cv-unqualified type, the copy / move operation can be omitted by directly constructing the temporary object into the skipped copy / move target
  • when an exception declaration of an exception handler (clause 15) declares an object of the same type (except for cv-qualification) as an exception object (15.1), the copy operation can be omitted by handling the exception declaration as an alias for the exception object if the program value does not change, for the exception of the execution of constructors and destructors for the object declared by the exception declaration. [Note: there can be no movement from the exception object as it is always an lvalue. -endnote]
+3


source







All Articles