The problem here was that the poster was declaring a pointer to the object, but no object was created, therefore he was accessing random values in memory which didn't form a std::vector object which led to a crash. One has to differenciate between a pointer to an object and address of an object. Their values have the same type (object*) but creating a pointer doesn't create an object. I can have several pointers pointing to the same object (and that's the whole point of using pointers).
int var;
int *ptr1 = &var;
int *ptr2 = &var;
int *ptr3 = ptr2;
int var;
int *ptr1 = &var;
int *ptr2 = &var;
int *ptr3 = ptr2;
To copy to clipboard, switch view to plain text mode
Now if I modify var ( var = 7; ) all pointers will still point to the same object, so dereferrencing them will still give me "7". Moreover if I now do:
int var2 = 5;
ptr2 = &var2;
int var2 = 5;
ptr2 = &var2;
To copy to clipboard, switch view to plain text mode
ptr2 will point to var2, but ptr3 will still point to var. The bottom line is that pointers are not tied to any objects, they just point to a place in memory, regardless of its contents.
Bookmarks