Passing pointer, getting wrong addres, how is this possible?

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.
'Wrong'? How so?
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?
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
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.
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
@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
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.
I guess that is all I can do really

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.
Okej, giving you an example:

C_Application.h
1
2
3
4
5
6
7
8
9
10

class C_Application : public C_Event {

         private:
            C_Player player;

                (...)

};


C_Application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13

bool C_Application::OnInit()
{

   player.OnLoad("player.png", 0, 0);
   
   C_PlayerShade* shadeptr = new C_PlayerShade;

   shadeptr->OnLoad(&player, 0.5);

   (...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h
1
2
3
4
5
6
7
8
9
10
11
12
13

class C_PlayerShade : public C_Entity {

              private:
               C_Entity* parentptr;

              public:
               bool  OnLoad(C_Entity* parent, float delay);
   
          (...)

}


C_PlayerShade.cpp
1
2
3
4
5
6
7
8
9
10

bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

     parentptr = parent;

     this->X = parentptr->X;       // Does not work. Gives wrong value.

}


Better?
Last edited on
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
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.
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
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
Nausea wrote:
Since the players X var will be updated every loop, and the playershades X will be updated every loop to reflect the players X
What makes them sync together? You've still not given us your actual code.
Last edited on
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
Ok I will show you the important parts of the classes then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class C_Entity {

    public:
        static std::vector<C_Entity*>   EntityList;

        C_Animation     Anim_Control;

    protected:


        SDL_Surface*    Surf_Entity;

    public:
        float           X;
        float           Y;
(...)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "C_Entity.h"

std::vector<C_Entity*> C_Entity::EntityList;

C_Entity::C_Entity()
{

    Surf_Entity     = NULL;

    X               = 0;
    Y               = 0;

    Width           = 0;
    Height          = 0;

    MoveLeft        = false;
    MoveRight       = false;

    Direction       = ENTITY_DIRECTION_RIGHT;

    Type            = ENTITY_TYPE_GENERIC;

    Dead            = false;
    Flags           = ENTITY_FLAG_NONE;

    SpeedX          = 0;
    SpeedY          = 0;

    AccelY          = 0;

    MaxSpeedX       = 13;
    MaxSpeedY       = 10;

    Col_X           = 0;          
    Col_Y           = 0;

    Col_Width       = 0;            
    Col_Height      = 0;
(...)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class C_Player : public C_Entity {

    public:

        unsigned int    LastSlide;
        bool            Sliding;
        bool            SlidingRight;
        bool            SlidingLeft;
        bool            JumpedOnSlide;

        bool            CanJump;
        bool            Jumping;
        bool            FirstJump;

        bool            Shooting;
        bool            ChargeShot;

(...)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
C_Player::C_Player()
{

    MaxSpeedY       = 10.5;
    X               = 0;
    Y               = 0;

    LastSlide       = 0;
    Sliding         = false;
    SlidingRight    = false;
    SlidingLeft     = false;
    JumpedOnSlide   = false;

    CanJump         = false;
    Jumping         = false;
    FirstJump       = true;

    Shooting        = false;
    ChargeShot      = false;

    Direction       = ENTITY_DIRECTION_RIGHT;
    Type            = ENTITY_TYPE_PLAYER;
    Flags           = ENTITY_FLAG_GRAVITY;

    Dead            = false;

}


1
2
3
4
5
6
7
8
9
10
11
class C_PlayerShade : public C_Entity {

        private:
            C_Entity*       target;

            float           SpeedDelay;

        public:
            C_PlayerShade();
            C_PlayerShade(C_Entity* parent, float delay);
(...)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
C_PlayerShade::C_PlayerShade()
{
    SpeedX = 0;
    SpeedY = 0;
    SpeedDelay = 0;

    AccelY = 0;

    X = 0;
    Y = 0;

    target = NULL;

    Dead = false;

    Direction = ENTITY_DIRECTION_RIGHT;
    Type = ENTITY_TYPE_PLAYER;
    Flags = ENTITY_FLAG_GRAVITY;
}

C_PlayerShade::C_PlayerShade(C_Entity* parent, float delay)
{

    target = parent;

    AccelY = target->AccelY;

    X = target->X;
    Y = target->Y;

    SpeedX = target->SpeedX;
    SpeedY = target->SpeedY;
    SpeedDelay = delay;

    Dead = false;

    Direction = ENTITY_DIRECTION_RIGHT;
    Type = ENTITY_TYPE_GENERIC;
    Flags = ENTITY_FLAG_GRAVITY;

    OnLoad();

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
bool C_PlayerShade::OnLoad()
{


    C_Frameset CharJumpRight( "JUMPINGRIGHT" );
    CharJumpRight.AddFrame(0, 188, 25, 48);
    CharJumpRight.AddFrame(25, 187, 23, 51);
    CharJumpRight.AddFrame(48, 189, 23, 55);
    CharJumpRight.AddFrame(71, 187, 23, 57);
    CharJumpRight.AddFrame(94, 190, 27, 53);
    CharJumpRight.AddFrame(121, 191, 34, 50);

    C_Frameset CharJumpLeft( "JUMPINGLEFT" );
    CharJumpLeft.AddFrame(0, 245, 25, 48);
    CharJumpLeft.AddFrame(25, 244, 23, 51);
    CharJumpLeft.AddFrame(48, 246, 23, 55);
    CharJumpLeft.AddFrame(71, 244, 23, 57);
    CharJumpLeft.AddFrame(94, 247, 27, 53);
    CharJumpLeft.AddFrame(121, 248, 34, 50);

    C_Frameset CharFallRight( "FALLINGRIGHT" );
    CharFallRight.AddFrame(155, 190, 32, 52);
    CharFallRight.AddFrame(187, 189, 29, 54);

    C_Frameset CharFallLeft( "FALLINGLEFT" );
    CharFallLeft.AddFrame(155, 247, 32, 52);
    CharFallLeft.AddFrame(187, 246, 29, 52);

    C_Frameset CharSlideRight( "SLIDINGRIGHT" );
    CharSlideRight.AddFrame(0, 301, 34, 44);
    CharSlideRight.AddFrame(34, 301, 46, 44, 10);
    CharSlideRight.AddFrame(80, 301, 53, 44, 20);
    CharSlideRight.AddFrame(133, 301, 57, 44, 20);
    CharSlideRight.AddFrame(190, 301, 38, 44, 10);
    CharSlideRight.AddFrame(228, 301, 30, 44, 10);
    CharSlideRight.AddFrame(258, 301, 41, 44, 10);
    CharSlideRight.AddFrame(299, 301, 43, 44, 10);

    C_Frameset CharSlideLeft( "SLIDINGLEFT" );
    CharSlideLeft.AddFrame(0, 345, 34, 44);
    CharSlideLeft.AddFrame(34, 345, 46, 44);
    CharSlideLeft.AddFrame(80, 345, 53, 44);
    CharSlideLeft.AddFrame(133, 345, 57, 44);
    CharSlideLeft.AddFrame(190, 345, 38, 44);
    CharSlideLeft.AddFrame(228, 345, 30, 44);
    CharSlideLeft.AddFrame(258, 345, 41, 44);
    CharSlideLeft.AddFrame(299, 345, 43, 44);

    C_Frameset Transparent("TRANSPARENT");
    Transparent.AddFrame(500,0,0,0);

    Anim_Control.AddFrameset(CharJumpRight);
    Anim_Control.AddFrameset(CharJumpLeft);
    Anim_Control.AddFrameset(CharFallRight);
    Anim_Control.AddFrameset(CharFallLeft);
    Anim_Control.AddFrameset(CharSlideRight);
    Anim_Control.AddFrameset(CharSlideLeft);
    Anim_Control.AddFrameset(Transparent);

    Anim_Control.SetAnimation("TRANSPARENT",150);

    if(C_Entity::OnLoad("./gfx/megaman/shade.png") == false)
    {
        fprintf(stderr, "Failed to load shade.png: %s\n", SDL_GetError());
        return false;
    }

    X = target->X;
    Y = target->Y;

    SpeedX = target->SpeedX;
    SpeedY = target->SpeedY;

    AccelY = target->AccelY;

    return true;


}


That´s good?
Topic archived. No new replies allowed.