C++ containers behavior -
My question is simple when I use STL containers, do they copy the value that I have there Do you archive (using copy constructor) or not? What if I give them the alphabet (character *) instead of the letter? How do they behave? What is the guarantee that the information will be stored in the system stack instead of a heap?
Thanks for the reply.
Values are stored in STL containers. If you have a vector like this:
class BigObject {...}; Of vector & lt; BigObject & gt; MyObjs; MyObjs.push_back (obj1); MyObjs.push_back (obj2); ...
The vector will make a copy of the object that is pushing in you. Apart from this, in the case of a vector, it can make new copies later when it is the underlying memory, so keep in mind.
The same thing is true when you have an indicator such as vector & lt; Char * & gt;
- But the difference is that the value of the copied value is, the string does not indicate it. So if you have:
vector & lt; Char * & gt; MyStrings; Char * str = new cell [256]; // SR points remembered place sprintf (str, "Hello, buffer") at position 0x1234; MyStrings.push_back (STR); Remove [] str;
... vector will receive a copy of the indicator. The indicator will have the same value (0x1234), and since you push the pointer to delete
D, your vector has a wild indicator and your code will eventually crash (after In, hopefully)
By the way, could have been unable to avoid, if you used strings instead of using four letters * then:
typingf vector & lt ; String & gt; Wire; Strings myStrings; MyStrings.push_back ("Hello, Buffer");
Comments
Post a Comment