I just came across a piece of code written by my ex-colleague few years ago. Honestly, I'm not an C++ expert, so I am seeking help.
The code looks like this:
std::vector<OBJ> objects;
void initobjs()
{
for (int i=0; i<10; i++)
{
OBJ obj;
obj.type=i;
obj.len=16;
objects.push_back(obj);
}
}
My question is: after function initobjs()
returns, aren't all obj
instances already out of scope and automatically freed by the C++ runtime system? If they are, will any reference to contents of any of the objects added to the vector cause memory fault or unexpected result?