Vector and references
-
These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.
std::vector vec;
vec.push_back(MyClass());MyClass& obj = vec[0];
AddSomeMoreItemsToTheVector(vec);
obj.CallSomeFunction();
The problem is that error may or may not always pose a problem.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.
std::vector vec;
vec.push_back(MyClass());MyClass& obj = vec[0];
AddSomeMoreItemsToTheVector(vec);
obj.CallSomeFunction();
The problem is that error may or may not always pose a problem.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Rama Krishna Vavilala wrote:
AddSomeMoreItemsToTheVector(vec);
Shouldn't that be &vec? [else there's no bug since a copy of the vector is modified :-)]
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog -
Rama Krishna Vavilala wrote:
AddSomeMoreItemsToTheVector(vec);
Shouldn't that be &vec? [else there's no bug since a copy of the vector is modified :-)]
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blogNishant Sivakumar wrote:
AddSomeMoreItemsToTheVector
If
AddSomeMoreItemsToTheVector()
is declared asvoid AddSomeMoreItemsToTheVector(type& vec);
then no.
---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.
std::vector vec;
vec.push_back(MyClass());MyClass& obj = vec[0];
AddSomeMoreItemsToTheVector(vec);
obj.CallSomeFunction();
The problem is that error may or may not always pose a problem.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Rama Krishna Vavilala wrote:
The problem is that error may or may not always pose a problem.
This is another class of error that i've seen so many times in so many different forms it's almost easier to see based on the symptoms than the code itself. Other, similar forms:
CString mystr = myotherstr;
...
DoStuffToString( (LPTSTR)(LPCTSTR)mystr );
if ( mystr != myotherstr)
...const KewlKlass& GetKlass()
{
KewlKlass k;
...
return k;
}class ItemManager
{
public:
void DoStuffForAllItems()
{
std::vector::iterator itr;
for (itr=Items.begin(); itr!=items.end(); ++itr)
DoStuffForItem(*itr);
}
void DoStuffForItem(Item& item)
{
if ( DontLikeItem(item) )
item = ReBuildEntireItemArrayAndReturnNewReference(Items);
item.LetsAllHaveIcecream();
}
private:
std::vector Items;
}---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
Nishant Sivakumar wrote:
AddSomeMoreItemsToTheVector
If
AddSomeMoreItemsToTheVector()
is declared asvoid AddSomeMoreItemsToTheVector(type& vec);
then no.
---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
Shog9 wrote:
void AddSomeMoreItemsToTheVector(type& vec);
Blast - that was a duh moment - I posted it around midnight though :-O
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog -
These are time consuming to figure out on one's own efforts but a code review catches the problem immediately.
std::vector vec;
vec.push_back(MyClass());MyClass& obj = vec[0];
AddSomeMoreItemsToTheVector(vec);
obj.CallSomeFunction();
The problem is that error may or may not always pose a problem.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
There is actually 2 problems. First, vector (and all the STL containers) store copies of objects. This isn't a problem unless something is done to the vector (e.g. sort it) and the object that you were referencing is now moved to another location in the vector. Thus, the memory your reference is now looking at contains different data than what you thought you were operating on. Second, if the vector has to be resized to accomidate your additions, the memory location that obj is at may no longer be allocated for that object, so you would be accessing invalid memory (or possibly accessing memory that is now allocated for something else).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac