| Nausea (27) | |
|
I´m going insane here. I´m passing a pointer into a function and somehow the address that arrives into the function is wrong. How is this possible? Driving me crazy. http://i.imgur.com/SaWNt.jpg http://i.imgur.com/p5JGl.jpg Thanks for any help. | |
|
|
|
| Zhuge (2880) | |
| 'Wrong'? How so? | |
|
|
|
| Nausea (27) | |
|
Yes it´s getting the wrong address of the pointer passed. So when my variables try to get values from the pointer, they get nothing. And when i print the address of the pointer before i pass it, it´s one address and inside the function it shows a totally different address. | |
|
|
|
| closed account (DSLq5Di1) | |
|
I tried setting a breakpoint and my debugger spat out at an error message about it being a screenshot or some such nonsense. Could we have some code please? a small test case that reproduces the problem? | |
|
|
|
| garanon (23) | |
|
Since you haven't shown any code, it's hard to know exactly the problem. However you are referring to the variable as shade1, which I'm assuming is what it was called before you passed it. However, when you pass the variable as C_Entity* Target, the variable name is now Target and not shade1 Again, it's hard to know since you haven't shown all of your code, I'm just assuming this is the problem from the two lines you've given | |
|
|
|
| Nausea (27) | |
|
Well basically, the pointer passed is the address of a Player, C_Player inherits C_Entity. Shade1, Shade2, Shade3 will die before Player at the end of the program. The pointer passed in is only used to get values from Player, not to modify them. example: X = Target->X; This will then give me the wrong result, as it turns out, Target is not pointing to the same place as the memory of the object passed into the function. Why this is, and has never happened to me before, I don´t know. So if someone knows why this might be, please do tell. As I said, the function is not even altering anything in the passed object, only reading values. | |
|
|
|
| Moschops (5961) | |
|
I know; there must be a mistake in your code. Perhaps if we could see the code we could help more. Alternatively, turn on the debugger, set a watch on Target, and see for yourself when the value changes. | |
|
Last edited on
|
|
| coder777 (2378) | |
|
@sloppy9 & garanon What? screenshots of 2 lines of code are not enough to pinpoint a complex problem? losers @Nausea That happens when the stack is corrupt (maybe due to writing out of bounds) or try 42 http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29 | |
|
Last edited on
|
|
| Nausea (27) | |
| Thank you coder777, I will try to add a watch to target and see what I get. I guess that is all I can do really. | |
|
|
|
| MikeyBoy (175) | ||
No, it's not all you can do. You can show us your actual code, so that we might have a chance of being able to see what's wrong. | ||
|
|
||
| Nausea (27) | |||||||||
|
Okej, giving you an example: C_Application.h
C_Application.cpp
C_Player inherits C_Entity, C_PlayerShade inherits C_Entity. C_PlayerShade.h
C_PlayerShade.cpp
Better? | |||||||||
|
Last edited on
|
|||||||||
| MikeyBoy (175) | |
|
Not much. So, you're creating an object of type C_Player as a data member of C_Application. You're then creating another object of type C_PlayerShade dynamically. Why would you expect the value of a data member of the first object to have the same value as a data member of the second object? You've shown us nothing of how these objects are constructed, nor have you shown us what C:Player::OnLoad does to the data members. We have no way of knowing what you expect the values to be. You've also given us no idea what type X is, what the expected behaviour is, and what the observed behaviour is. "Gives wrong value" isn't very helpful - which variable is it that has the wrong value, and how are you observing this? We can't know what's wrong with your code if you won't show us that code. | |
|
Last edited on
|
|
| Nausea (27) | |
|
I´m trying to give the C_PlayerShade objects X variable (float) the value of the players X variable (float). This does not occur, should it not? I think it´s pretty clear that I´m trying to do that? right? C_Player::OnLoad just loads the object with a surface for the image, and width/height. Adding frames for animation. Nothing fancy. I also have a C_PlayerShade::OnLoop() method that updates its coordinates to the parentptr's X and Y coordinates. This does not work. Is that clear enough? If player.X = 500 for example, I want the C_PlayerShade object to get that value and have its X set to that value. | |
|
|
|
| MikeyBoy (175) | |
|
So what results are you seeing? What are you setting C_Application::parent.X to before calling shadeptr->OnLoad(&player, 0.5) ? What value are you seeing for parentptr->X inside the C_PlayerShade::OnLoad() method? What value is this->X being set to inside the C_PlayerShade::OnLoad() method?Edit: If it's the actual pointer value you're worried about, rather than the member variables - don't. The argument to C_PlayerShade::OnLoad() is of type C_Entity*. It will not be pointing to the address of the start of the C_Player object, but the address of the start of the C_Entity part of it. That's how most compilers do it. If the values of the data members are OK, then it's working fine. | |
|
Last edited on
|
|
| Nausea (27) | |
|
I can set parent.X to any number before calling shadeptr->onload. then parentptr->X shows 0 inside of C_PlayerShade::OnLoad(). this->X is set to parentptr->X and thus ends up as 0 instead of the actual C_Player objects X. Since the players X var will be updated every loop, and the playershades X will be updated every loop to reflect the players X var it should work? But it doesnt, it just ends up as 0 every loop. | |
|
Last edited on
|
|
| L B (3327) | |||
| |||
|
Last edited on
|
|||
| MikeyBoy (175) | |
|
How are you declaring X? Presumably, you have it as a protected or public data member of C_Entity? Is it being initialised to 0? If we could see the complete header file declarations for C_Entity, C_Player and C_PlayerShade, it might help. | |
|
Last edited on
|
|
| Nausea (27) | |||||||||||||||
Ok I will show you the important parts of the classes then:
That´s good? | |||||||||||||||
|
|
|||||||||||||||