pointer question

Started by
1 comment, last by ToohrVyk 16 years, 1 month ago
hi all This is the problem Im having at the moment. I have 2 pointers, p1 and p2, And I have class. P2 is stored within a std::vector. Inside a loop p2 points to the class, then p1 points to p2 and p1 is stored into the vector. On each update p2 points to a different struct. Heres the problem, each p1 in the vector = the NEW value p2 is pointing at, not its original value that it was stored at.(I hope this is understandable :) ) So in short, every p2 in the vector is equal to the last p2 stored in the vector. I want to keep it all pointers for speed and memory sake. Any Ideas on how to fix it? Thanks.
Advertisement
Never mind. All fixed :)
Quote:I want to keep it all pointers for speed and memory sake. Any Ideas on how to fix it?


Rule number one of optimization: make it run, then make it run fast.

Your priority is to have a program that works, because if you optimize a program that doesn't work then all your optimization efforts will be lost when you have to change the code to make it work. Besides, if your program didn't work in the first place, how did you determine that it needed to be optimized in that particular area?

Quote:Original post by Helo7777
hi all
This is the problem Im having at the moment.
I have 2 pointers, p1 and p2, And I have class. P2 is stored within a std::vector. Inside a loop p2 points to the class, then p1 points to p2 and p1 is stored into the vector. On each update p2 points to a different struct. Heres the problem, each p1 in the vector = the NEW value p2 is pointing at, not its original value that it was stored at.(I hope this is understandable :) ) So in short, every p2 in the vector is equal to the last p2 stored in the vector.


No, it's not understandable. You should either explain what you want to do (something which is generally possible in plain English), or you should provide code or pseudocode to explain how you are doing it (something which is quite difficult in plain English). Besides, your terminology is weird: you can't point to a class (only instances of a class) and you can't have 'every p2' since you have only one p2, the value of which was stored in the vector.

However, by attempting to decipher your explanation, it seems that there is a problem with your code. I suspect the following, though I'm not sure:

while (condition){  T object_on_the_stack;  T* pointer = &object_on_the_stack;  vector.push_back(pointer);}

This topic is closed to new replies.

Advertisement