Sign in to follow this  
Aerts

Why pointer

Recommended Posts

Aerts    100
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
Omega147    536
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
Alpha_ProgDes    6936
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
Lazy Foo    1113
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
Leadorn    100
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
Nuno1    161
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
Demus79    362
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
twoaterisn    190
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
Kalazart    148
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
Oluseyi    2112
Quote:
Original post by twoaterisn
if I'm correct the second one will take a lot of memory becase a copy of log has to be made.

Not necessarily. C++ has references, which are like pointers managed by the compiler instead of by you. Memory overhead becomes identical.

Stop thinking of everything in terms of theoretical performance. In most cases, you're not writing code that needs absolute optimization. Think first of logical simplicity, ease of comprehension, modularity and maintainability. Think of correctness and readability. Then think of performance.

Object orientation allows you to bundle state and operations together. This makes it easier to carry the logical structure of the code in your head, which facilitates better overall code.

Share this post


Link to post
Share on other sites
Oluseyi    2112
Quote:
Original post by twoaterisn
So there will be no (significant) speed difference between log.move and move(plog)?

Internally, log.move() is converted into move(this), where this is a pointer to the specific instance of the Log class on which it is being called. So, no, there's no difference.

Share this post


Link to post
Share on other sites
Nuno1    161
the only real "performance" issue that you have using C++ (or any other OOP language) is using virtual functions. that is basiclly why most of the C developers saying that C++ is bad for performance.

virtual functions compiled to a dynamic call instead of a static call.
for example in assembly language :

- CALL 0x12345678 demonstrate a non-virtual function call.this is a static call because the 0x12345678 is the address of the function itself. the compiler know that when you call a non-virtual function it will allways be the same code to execute.

- CALL [0x12345678] demonstrate a virtual function call.the 0x12345678 address is a pointer to a function. this cost alittle bit more cycles on your CPU but somthing that for games or applications is very minor.

now by thinking about that, most of the C programmers that claims C++ is bad for performance do want the benefits of an OO Programming so they do use structures with pointers to functions and by that basicly "implements" the VTable itself. so they do have the same penelty as the virtual function have.

ofcourse C++ can be used bad and effect the performance in other areas as well. but this is the same as C Programming. it easyer to do mistakes with C++.

my 2 cents,
Nuno1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this