Dynamic memory allocation;

Hi forum,
I'm making a space invaders clone with SDL, as part of my learning. I want the player to be able to shoot a bullet. I have a bullet class. In short, I want a new instance of the bullet class to be created every time space is pressed, and I want this instance of the class to be public. In more detail:
1
2
3
4
5
6
7
8
9
void bullet::handleInput() {
	if(event.type == SDL_KEYDOWN) {
		if(event.key.keysym.sym == SDLK_space) {
			bullet* asdf = new bullet();
			asdf -> load();
			asdf -> draw();	
		}
	}
}

Here is a function from bullet. When I try to reference "adsf" from say, my main function, with like asdf -> move();, gcc says that asdf was not declared in this scope and it dosn't compile. Could I have any pointers (no pun intended ;D) from you guys please?
Was asdf declared in main?
no, i declare it in this function. where should i declare it for it to still be dynamically allocated but global?
Well, you don't want global. But if you want it used by main, you need it declared in main. It looks like you need to rethink your design here.

This is basic scoping issue.
All scoping aside, is the way i'm doing things now creating a new instance of bullet with a unique memory address in the stack every time I press space?
Yes. However, you are (due to scoping issues) losing your pointer to it, thus creating a memory leak.
Well yea, but it's inaccessible from anything outside that if block. And you're not freeing the memory so it's just gonna be sitting in memory unusable even after your done with it. This happens every time you shoot, and I imagine a lot of shooting in space invaders. As you can imagine, this is gonna lead to a whole lot of of unfreed memory.
Hmmm. I must look at the source of other programs and read some more because currently, (and i'm guessing due to my draw() function), my program does not work the way I want it to. Thanks for the help.
Live long and prosper
Topic archived. No new replies allowed.