You need to understand the difference between compile-time and runtime with respect to C++ types. This is a basic fundamental of the C++ and 'C' language.
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
std::string s;
cout << sizeof(s) << "\n";
s = "abc123456789123456789xyz";
cout << sizeof(s) << "\n";
}
What results do you get? You see that sizeof(s) is the same, regardless of what you do at runtime to the string? The sizeof(string) doesn't magically change from 0 to 24 just becase it now contains 24 characters.
AH HA MOMENT!
I can see now that when the vector of structs is instantiated, space for the string is not allocated "within" the struct. Just a pointer to the string is the actual member in the struct itself. The string is actually stored elsewhere in memory. So the size of the struct remains constant and the compiler knows exactly how many bytes make up the struct which is always a constant value.
Then all the compiler has to do is:
++ iterator
interator * jumpValue
to get to the next struct.
You're the man Paul. Thanks for helping me understand it! (Hope that's right anyway )
This technique can actually be used in programs I write in the future.
Thanks,
Raptor
Last edited by raptor88; December 2nd, 2012 at 06:17 PM.
Bookmarks