pointers object variable's getting scrambled?

Alright so what I have is a pointer pointing towards an object. This sets up perfectly. But for some reason when the pointer is used in a function or goes into a smaller scope or another variable is assigned the variables in the object that the pointer is pointing to become all scrambled and NOT what they are supposed to be.
That may not be the reason but that is what it looks like it is doing. The variables are set to specific values but later, for example, an integer turns into something like "-873984738"* and a float does something like "137747e+007"* but not necessarily at the same time, the scrambling happens over time as the code progresses.

*not exact numbers

The only reason I can imagine is that I didn't declare the pointer using the "new" keyword and such.

Sorry if I'm not clear enough but the program is acting so strange I can't describe it very clear. Hopefully you MIGHT have an idea on how I could attempt at solving the problem... use as much technical words as necessary. :) Thanks in advance.

BTW I am using the MS Visual C++ Express.



Last edited on
Sounds like you might be pointing to an object that exists on the stack. The object is going out of scope, and your pointer is going bad. Then when you try to access the pointer you're effectively accessing garbage.

Can you show me some code? How are you getting this pointer? What object are you pointing to? How was that object created?
Here is the "infected" code...

Create the pointer.
Animals * pet;
assign the pointer
pet = makeAnimal(aa); //in this function there is the below
1
2
3
4
5
6
7
8
9
10
11
12
//...I put this in a function to condense the main function's size. P.S. there is more to the //function but it is irrelevant to this cause
Animals * makeAnimal(int aNum)
{
Animals * ppet;
if(aNum == ELEPHANT)
	{
		Animals Elephant(15,1,ELEPHANT);
		ppet = &Elephant;
	}

	return(ppet);
}


Now a little later I want to display to the screen the objects "stats".
 
statistics(pet->strength,pet->speed,pet->species,pet->level); //it actually works here 

All this does is displays stuff to the screen.
But the VERY next line shows scrambled variable values

pet 0x0028f7f8 {level=-858993460 strength=-1.0737418e+008 speed=-1.0737418e+008 ...}
^^straight from the editor.

I hope this is enough info. :)
Last edited on
yes, that is enough. And you're doing exactly what I suspected:

1
2
3
4
5
6
7
8
9
10
Animals * ppet;
if(aNum == ELEPHANT)
	{
		Animals Elephant(15,1,ELEPHANT);  // Elephant is created in this scope
		ppet = &Elephant;   // ppet points to Element
	}  // Elephant goes out of scope and is destroyed here.
	   //  ie... it doesn't exist anymore
	   //  so 'ppet' points to garbage.  It's a bad pointer

	return(ppet);  // so you're returning a bad pointer 


If you want your animal to exist outside of the scope it's created in, you need to dynamically allocate it:

1
2
3
4
if(aNum == ELEPHANT)
  ppet = new Animals(15,1,ELEPHANT);

return ppet;


Of course then you have to remember to delete the pointer from the calling function (because you're passing ownership). You might want to consider returning an auto_ptr instead of a normal pointer.
Ahhhhhh...that makes sense. I forgot to look for going out of scope in the function. I think that is the fix I was looking for. Now that I know the problem I can work on fixing it. Thank you so much!
Topic archived. No new replies allowed.