Welcome! Log In Create A New Profile Wiibrew Wiki HackMii Blog

Advanced

Problem with Pointers

Posted by Dreloc 
Problem with Pointers
April 19, 2012 07:24PM
Hello again,

I'm quite sure i got again a basic c++ principle problem. Anyways here is what i got:

I got a Test.cpp class which should just draw me a integer value i passed by reference:
Test.h
class Test : public Drawable{
private:
	int* pCounter;
	int debugHeight;
public:
	Test(int debugHeight, int* pCounter);
	virtual void draw();

};

Test.cpp
#include "Test.h"

Test::Test(int debugHeight_, int * pCounter_) {
	pCounter = pCounter_;
	debugHeight = debugHeight_;
}
void Test::draw() {
	++*pCounter;
	GRRLIB_Printf(30, debugHeight, tex_BMfont5, GRRLIB_WHITE, 1, "pCounter : %i",  pCounter);
	GRRLIB_Printf(30, debugHeight + 20, tex_BMfont5, GRRLIB_WHITE, 1, "*pCounter : %i",  *pCounter);

}

As you can see i just want to print out the pointer as well as the dereferenced value:

My data structure is as Following:
-Screen.cpp || A screen contains a map of Layer with their z-index
-Layer.cpp || A layer contains a map of Drawable with their z-index
-Drawable.cpp || Drawable contains a virtual function draw which draws using the grrlib and has a few derived classes in my case Test.h

in my main.cpp i got a function which draws me a screen by iterating all layers, iterating all drawables and invoke the draw function

void drawScreen(Screen screen) {
	std::map lMap = screen.getData();
	std::map::iterator layerIt;
	for (layerIt = lMap.begin(); layerIt != lMap.end(); layerIt++) {
		Layer lp = layerIt->second;

		std::map::iterator drawIt;
		std::map dMap = lp.getData();

		for (drawIt = dMap.begin(); drawIt != dMap.end(); drawIt++) {
			Drawable* dp = drawIt->second;
			dp->invokeAction();
			dp->draw();
		}
	}
}

What i do now is i create a Screen, create a Layer and a Drawable (Test.h). Then i add the drawable to the layer, the layer to the screen


	Screen screen2;
	Layer layer2;
	Drawable* test2 = new Test(400, pX);
	layer2.addDrawable(1, test2);
	screen2.addLayer(1, layer2);

afterwards I invoke the drawScreen() function and the integer i pass by pointer increments nicely.

So far so good.

No i for the purpose of a cleaner code i created an other class to produce all my different screens i gonna need:

ScreenFact.h

class ScreenFact{
public:
	ScreenFact();
	Screen getScreen();
};
ScreenFact.cpp
ScreenFact::ScreenFact() {

}

Screen ScreenFact::getScreen() {
	int x = 10;
	int* pX = &x;

	Screen screen;
	Layer l;
	Drawable * t = new Test(300, pX);

	l.addDrawable(1, t);
	screen.addLayer(1, l);

	return screen;
}

BUT! Yeah finally here's the but ^^
In my main.cpp I create a screen using following code:
	ScreenFact sf;
	Screen s = sf.getScreen();

Now if i use the method drawScreen() on this generated screen the dereferenced integer is not incrementing. The pointer is stable but the dereferenced value is not! it looks itself like a pointer and changes randomly.

The drawScreen() method is invoked in the magic while-loop while the Screens are only initialized once.

I guess it is the way I use the ScreenFact to produce a screen....

I tried to make the code as easy as possible and hope someone has a clue

Thx in advance,
Dreloc
Re: Problem with Pointers
April 20, 2012 02:05AM
In ScreenFact::getScreen() the int pointer that you pass to the new Test class points to an int that is local to that function i.e. when getScreen() returns, that int no longer exists and the pointer is no longer valid - it points to some random memory on the stack which is why you see it randomly changing.
Re: Problem with Pointers
April 20, 2012 11:54AM
Works perfectly now... sorry for the whole story and thx for reading through it
Re: Problem with Pointers
April 20, 2012 01:51PM
Good luck with your code! Looks complicated. how many layers do you plan to draw?
Sorry, only registered users may post in this forum.

Click here to login