I'm creating a stack virtual machine and wondering how vector really works underground. This question related to malloc as well.
Consider the following code:
std::vector<int> vec;
vec.push_back(1);
vec.push_back(3);
vec.push_back(7);
for (unsigned long long i = 0; i < vec.size(); i++) {
std::cout << &vec[i] << std::endl;
}
std::cout << &vec[0] << std::endl;
std::cout << &vec[0] + 1 << std::endl;
std::cout << &vec[0] + 2 << std::endl;
with the following output in my computer:
00000292E7CF9D20
00000292E7CF9D24
00000292E7CF9D28
00000292E7CF9D20
00000292E7CF9D24
00000292E7CF9D28
The thing is; by storing all the program instructions to be run by my VM, I use a vector. I have a PC (program counter / instruction pointer):
Should the PC point to the first address of the vector and increment it by 1 each time I move to the next instruction or should I simply access it by the vector accessor (this would be slower of course...)?
Is incrementing 1 to an address a really bad practice?
I'm kinda new to the c / cpp world.
Thanks!