J
Jonathan Mcdougall
Sir_Ciph3r said: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