Jump to content
  • Advertisement
Sign in to follow this  
Aerts

Why pointer

This topic is 4446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a C++ book with sourcecode for a game example. The game's core variables and methods are all inside a class called GameEngine. To access all those, an object has been created: _pGame. But instead of: GameEngine _pgame;... it was declared like this: GameEngine* _pGame; (the small 'p' stands for pointer). My questions are: 1)why make it a pointer when a regular object could access all members just as well? 2)further along the book is implemented class Bitmap (with bmp-specific methods). All objects of class Bitmap are also pointers. Why?

Share this post


Link to post
Share on other sites
Advertisement
Pointers are a very nice feature of programming that you should become accustomed to. Assuming you know what a pointer is, I'll just jump to the reasoning: the reason why pointers are used in programs is because it's more memory-efficient. Most notably, pointers are rather good at saving memory when they are passed to a function. Imagine, for a second, the following function:

void foobar(CData data) {}

The above function, as you may note, does not take a pointer to an object of type CData; instead, it accepts a "new object" in essence of type CData; basically, what happens in the function as shown above, when a variable of type CData is passed it will actually copy all the info from the passed object into our new variable "data" (or whatever the variable is named). If this were instead a pointer, the function would not re-copy the information, but would instead work with the object that is already stored in memory.

I'd explain further, but I'm half asleep at the moment... hope my sentences aren't too scattered or sound too much like jibberish. Anyone care to further explain what I've said? lol

Edit: perhaps this resource could explain things for you.

Share this post


Link to post
Share on other sites
well i have no idea why the gameengine would be a pointer. i'd have to see some code to make that determination. the bitmap is usually a pointer because it makes for faster access, gets passed to alot of functions, and the bitmaps change often and may have to deallocated and/or reallocated.

Share this post


Link to post
Share on other sites
say if you have a function like this


void foo( int num )
{
num = 0;
}



then you do this


int blah = 10;

foo( blah );

cout << blah;



your output would be 10 because when you pass a variable to a function you send a copy not the actual variable.

but if you have a function like this:

void foo( int *num )
{
*num = 0;
}



then you do this


int blah = 10;

foo( &blah );

cout << blah;



the output will be 0.

Why? because you sent the RAM address with a variable you wanted to call foo() on.
Your function knows where it is in RAM and can now alter it.

Managing memory is something to get used to because it makes 0 sense when you start out. Though with the few programs to practice, memory will be your bitch [grin]

Share this post


Link to post
Share on other sites
There is probably no good reason to dynamically allocate the main game object as to put it on the stack. They might want the control to deallocation of the object.

There is nothing restraining them to not be able to use the main game object as a pointer either way.

Share this post


Link to post
Share on other sites
Be carful from some of those answers in your thread because they may misslead you.

I'll give you one simple example when you would like to use a reference to an object. this ofcourse isn't the only reason you willing to do this, but it will help you to understand.

assume a 2d bitmap object. basiclly the object should hold position on screen and the bitmap to draw.

let assume also that there should be 20 other 2d bitmap objects with the same bitmap picture. so basiclly you have two options :

1. load to memory for each 2d bitmap object the same bitmap. this means that you are going to use 2d_bitmap_size * number_of_objects memory space.

2. load one instance of that bitmap to memory and reference from each 2d bitmap object to the bitmap image that stored in memory. basiclly it means we now using 2d-bitmap_size * 1 memory space. a big diffrent.

thats pretty answering your question. using a reference to the bitmap in your book code gives the ability to store the bitmap once in memory.

there are other uses for referencing an object, but this is what you were after here. there is no performence penlty from not using it. ofcourse creating a new object probobly may call a constractor or load anything to memory so it may effect performance, but sometimes you have to create a new object.

cheers,
Nuno1

Share this post


Link to post
Share on other sites
One point about the bitmap example just to clarify things. One good reason for bitmap to be a allocated dynamically is that most of the time you don't actually know the size of the bitmap before you load it. Thus, there is no sense in preallocating a certain amount of memory and hope that all the bitmaps will actually fit into it (probably you'd be wasting huge amounts of memory).

Most of the time I use pointers, because there is more about it such as class interfaces which cannot be static variables.

I think that it is wrong to say "pointers are faster" though. You can pass a non-pointer variable as well to a function.


void foo( int &num )
{
num = 0;
}

//or with a class object

void foo( myClass &obj )
{
obj.num = 0;

//if obj was a pointer this would be obj->num = 0;
}

//this is actually slightly better way than using the pointer approach imho
//and it does the same thing.


Share this post


Link to post
Share on other sites
hey,

I have a question about object oriented programming, which is (I think) also related to pointers and managing memory.

what is the diffence between

log.move();

and

move(log);

if I'm correct the second one will take a lot of memory becase a copy of log has to be made. But now, what is the advantage of oop over using this:

move(plog);

with plog being the pointer to log. And move rewritten to handle pointer input of course.

Maybe it's a stupid question, don't know.

Share this post


Link to post
Share on other sites
The advantage is classes organization. OOP benefits of encapsulation, inheritance and those all other features OO can offer, instead of globally shared functions and data. This reduces mess on code, creates a much enjoyable way of reusing code and you get to choose what will be able to be modified from a structure.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!