I’ve been trying to figure this out for a while so any help would be great. I’m writing an engine for a school project and the data in one of my pointers appears to be changing on its own. I declare an object pointer Actor* actor; in Engine.h and initialize it with
void Engine::initActor(Actor* newActor)
{
actor = newActor;
std::cout << "Actor imported." << std::endl;
}
by sending initActor() a new actor pointer with its own Position pointer of a generic Position class as seen here:
class Position
{
public:
//vars
GLfloat x,y,z;
GLfloat rotx, roty, rotz;
//constructors
Position();
Position(int newx, int newy, int newz);
Position(int newx, int newy, int newz, int newRotx, int newRoty, int newRotz);
GLfloat distanceTo(Position pos1);
};
Then, i go to access the Position object pointer of the Actor class when drawing the world, and even though the Position pointer is pointing to the same address to which it was initialized, position->x has changed from 0 to which it was originally set and is now read as -1.07374e+008. The only thing i can think is that the memory the Position pointer is pointing to has changed but i don’t know why or how to avoid this. Here is the actor.h class if it helps:
class Actor{
public:
//constructors
Actor();
//functions
Position* position;
void posCamera();
};
Whenever you see crazy big +/- numbers like that pop up its a sure sign that:
A) the value you passed in directly or via pointer was never initialized to begin with, or
B) the value has gone out-of-scope.
You indicate it’s originally correct so it’s probably going out of scope somehow. Was it allocated to the heap originally, versus the stack? How are you creating the original pointer? A common beginner mistake is improper use of the & address reference. Here’s a quick example if it helps; it also reproduces the error you indicate, first printed value for me was
1606416707, the second was the correct 100 value.
Hope that helps!