Coming from a C# world, I'm struggling to make sure I don't introduce memory leaks and errors in a C++ project I've been assigned to. I'm writing code that uses structs to parse information from a buffer of data. Because the number of data structures that appear in the buffer can vary at runtime, an stl vector is used to store the processed data. I came across the following block of code in the existing software, and am struggling to understand why it works:
MyVectorOfObjects.clear();
for (unsigned __int8 i = 0; i < NumberOfObjects; i++)
{
MyParserObject parserObject; // Declaring without 'new'?
parserObject.Decode(buffer, offset, size); // A method on the struct.
MyVectorOfObjects.push_back(parserObject); // Does this keep parserObject in scope?
}
My questions are specifically:
According to this question, wouldn't parserObject
go out of scope each iteration since the new
keyword isn't used? Evidently this code has been working.
In this case, does placing the object in a vector
keep the parserObject
in scope?
According to this question, the parserObject is copied. If this is the case, what are the performance implications (e.g. memory consumption, memory allocation, etc.) of this? Also, do the copied parserObjects then assume the same scope as the vector?
Thanks for any help.
The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope. This means that the object cannot be accessed in a nesting scope (block).
C/C++ use lexical scoping. The lifetime of a variable or object is the time period in which the variable/object has valid memory. Lifetime is also called "allocation method" or "storage duration."
The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution. The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.
Yes, the instance of parserObject
that is declared within the for
loop goes out of scope each time the loop iterates.
No, placing the parserObject
into the vector
doesn't keep that object in scope. The push_back()
method will make a copy of that object that is now owned by the vector
. You need to make sure your objects can be properly copied (copy-constructor and assignment operator may be necessary). Copies contained in the vector in this example are owned by the vector, and will have object lifetimes that are similar to the vector
itself.
The paserObject
is copied, and this may have implications on memory usage and performance. If the parserObject
is not trivial to copy then this can be an expensive operation. This is wholly dependent upon your implementation of the parserObject
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With