By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Clarification for std::vector and std::map

P: n/a
Can someone explain what is happening in the following code?

#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
vector<int> v;
map<char*,int*> m;

v.push_back(99);
m["TEST"]=&v[0];

cout<<"Vec size: "<<v.size()<<" : "<<v[0]<<endl;

v.erase(&v[0]);

cout<<"Map: "<<*m["TEST"]<<endl;

cout<<"Vec size: "<<v.size()<<endl;

return 0;
}

I don't understand why I am getting "99" from this line of code

cout<<"Map: "<<*m["TEST"]<<endl;

I erased the vector element and the map only contained the address to
that vector element...yet I am still getting the value I pushed_back.
What is happening behind the scenes? Is this code safe?

Any help would be appreciated
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Sir_Ciph3r" <Si********@nospam.com> wrote...
Can someone explain what is happening in the following code?
Undefined behaviour.
#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
vector<int> v;
map<char*,int*> m;

v.push_back(99);
m["TEST"]=&v[0];
You're saving a pointer to an object that may vanish into thin
air at any point and the map won't know about it. That's called
"asking for trouble".
cout<<"Vec size: "<<v.size()<<" : "<<v[0]<<endl;

v.erase(&v[0]);
There is no 'erase' member that takes a pointer to int. If your
implementation _happens_ to have vector::iterator the same as
a pointer to the contained type, it doesn't mean it will work
on any other computers/compilers/platforms. You have to pass
an iterator to 'erase'.

cout<<"Map: "<<*m["TEST"]<<endl;
After the 'erase' (which probably worked by some weird chance)
you're trying to dereference a pointer that doesn't exist any
longer. The behaviour is undefined. You get your 99, but you
could as well get flying nasal demons.

cout<<"Vec size: "<<v.size()<<endl;

return 0;
}

I don't understand why I am getting "99" from this line of code

cout<<"Map: "<<*m["TEST"]<<endl;

I erased the vector element and the map only contained the address to
that vector element...yet I am still getting the value I pushed_back.
What is happening behind the scenes?
Impossible to say. Your code causes undefined behaviour.
Is this code safe?


Absolutely not.

Victor
Jul 19 '05 #2

P: n/a
"Sir_Ciph3r" <Si********@nospam.com> wrote in message
news:ro********************************@4ax.com...
Can someone explain what is happening in the following code?

#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
vector<int> v;
map<char*,int*> m;

v.push_back(99);
m["TEST"]=&v[0];

cout<<"Vec size: "<<v.size()<<" : "<<v[0]<<endl;

v.erase(&v[0]);

cout<<"Map: "<<*m["TEST"]<<endl;

cout<<"Vec size: "<<v.size()<<endl;

return 0;
}

I don't understand why I am getting "99" from this line of code

cout<<"Map: "<<*m["TEST"]<<endl;

I erased the vector element and the map only contained the address to
that vector element...yet I am still getting the value I pushed_back.
What is happening behind the scenes? Is this code safe?

Any help would be appreciated


A container of raw pointers is usually not a good idea.
Use string and iterator instead of char* and int*.

int main()
{
vector<int> v;
map<string, vector<int>::iterator> m;

v.push_back(99);
m["TEST"]=v.begin();

cout<<"Vec size: "<<v.size()<<" : "<<v[0]<<endl;
cout<<"Map: "<<*m["TEST"]<<endl;

v.erase(v.begin());

// cout<<"Map: "<<*m["TEST"]<<endl;
// v.erase() invalidates m["TEST"]

cout<<"Vec size: "<<v.size()<<endl;

return 0;
}

--
ES Kim

Jul 19 '05 #3

P: n/a


Sir_Ciph3r wrote:
I erased the vector element and the map only contained the address to
that vector element...yet I am still getting the value I pushed_back.
What is happening behind the scenes? Is this code safe?

You did all kinds of undefined behavior. Iterators and pointers into
vectors are invalidated as soon as you do any kind of operation on the
vector that make cause it to resize memory. Erase() certainly could have
done that. What probably happened with that the vector saw that you were
erasing the last element, so it just backed its valid end pointer by
one, and decreased the size so that the element you are pointing to will
be overwritten the next time.

What are you trying to accomplish?


Brian Rodenborn
Jul 19 '05 #4

P: n/a
On Wed, 30 Jul 2003 18:13:48 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:

Thanks for the help guys, I am slowing learning.

With this advice ->

There is no 'erase' member that takes a pointer to int. If your
implementation _happens_ to have vector::iterator the same as
a pointer to the contained type, it doesn't mean it will work
on any other computers/compilers/platforms. You have to pass
an iterator to 'erase'.

Would this be a safe way to erase an element from a vector?

std::vector<CWindowKeeperWindow*>::iterator IVec;

// for(UINT i=0;i<uiSize;i++)
for(IVec=m_vecInstalledTimers.begin();
IVec!=m_vecInstalledTimers.end();)
if((*IVec)->get_window_id()==hTreeItem)
{
//
m_vecInstalledTimers.erase(&m_vecInstalledTimers[i]);
IVec=m_vecInstalledTimers.erase(IVec);

#ifdef __CWINDOWKEEPER_DEBUG__
ConPrintf(_T("OnDeleteWindow() - erased timer
element\n"));
#endif
}
else
++IVec;
}
Jul 19 '05 #5

P: n/a
On Wed, 30 Jul 2003 18:13:48 -0400, "Victor Bazarov"
<v.********@attAbi.com> wrote:
"Sir_Ciph3r" <Si********@nospam.com> wrote...
Can someone explain what is happening in the following code?


Undefined behaviour.
#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
vector<int> v;
map<char*,int*> m;

v.push_back(99);
m["TEST"]=&v[0];


You're saving a pointer to an object that may vanish into thin
air at any point and the map won't know about it. That's called
"asking for trouble".


I was just wondering if

map<char*, int> m;

m["TEST"] = 4;
m["TEST"] = 6;

is guaranteed to work, knowing that "TEST" is a pointer to a c-style
string. To me, it seems that a compiler is allowed to have two
different "TEST" strings and to map the values to the pointer address,
no?
Jonathan

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.