Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chigaimasu

Pointers- - Pointless?

This topic is 5529 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

That''s certainly how I feel right now. I may be proven wrong soon, but, personally, I just can''t see a practical application for pointers. I understand the syntax, I understand some of the more ''practical'' algoritims; I just don''t see how _I_ could use them. Anyone who could tell me otherwise (with examples!) would definetly be a help. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
There are many points to pointers... (har har har..)

The big one is using pointers to change values passed to a function. When a variable is passed normally, a copy of the variable is made and the data is copied into it. With a pointer, a reference to the variable is created. Becuase passing by value creates a copy, changing the new variable will not change the one that was passed. With pointers, that variable can be changed. The other advantage to a reference as opposed to byvalue passing is that if you have a 12mb data structure, you don''t have to copy 12mb every time you call the function... you can just access it. Pointers are also useful for getting directly at the bits.. like if you want to access certain system information (such as the video buffer).. you''ll need a pointer to store the address to access it.. though far pointers used like that are more or less obsolete..

Share this post


Link to post
Share on other sites
I disagree that the big use of pointers is to change a value of a variable passed as a pointer. This can be done with a reference.

The 2 big uses of pointers is:

1) dynamic memory allocation . Allocate memory at runtime returns a pointer to that memory location.

2) polymorphism . This is the ability for a pointer to point to an object of a subtype at runtime.

Regards,
Jeff

Edit: Added #2.

[edited by - rypyr on April 29, 2003 11:10:02 AM]

Share this post


Link to post
Share on other sites
Without pointers (in C/C++ at least), you''d have no way of making some of the fundamental constructs of computer science. For example, linked lists or binary trees. However, there are those who agree with you that pointers are pointless. That was one of the principles used in designing Java, the elimination of pointers.

Share this post


Link to post
Share on other sites
how about dynamic memory allocation, that would be one example of where pointers will cease to be pointless. I also wouldn''t really want to create a linked list without pointers, even Java has pointers in the language of sorts(this, for example), the programmer just cannot actually create pointers of their own. so that you don''t ever have to deal with pointers. In C, before C++ existed, I think pointers were the only way to pass variables into a function and be able to change the values, for say a swap() function, that will actually swap the two variable''s values.Although I think C++''s introduction of references got rid of a lot of the use for pointers in the language, they still are very useful constructs.

Share this post


Link to post
Share on other sites
Example of dynamic memory allocation:


    
class StrBuf {
char* m_pBuf;
public:
StrBuf(int len) { m_pBuf = new char[len]; }
~StrBuf() { delete[] m_pBuf; }
};


Here is the typical example of polymorphism that people often show:


  
class IShape {
public:
virtual void draw() = 0;
};

class Circle : public IShape {
public:
void draw() { draw circle here }
};

class Rectangle : public IShape {
public:
void draw() { draw rectangle here }
};

class Polygon : public IShape {
public:
void draw() { draw rectangle here }
};

void main() {
IShape* vecShapes[3];

vecShapes[0] = new Circle();
vecShapes[1] = new Rectangle();
vecShapes[2] = new Polygon();

// draw all shapes

for(int loop = 0; loop < 3; ++loop) {
vecShapes[loop]->draw();
}

for(int loop = 0; loop < 3; ++loop) {
delete vecShapes[loop];
}
}


[edited by - rypyr on April 29, 2003 11:21:53 AM]

Share this post


Link to post
Share on other sites
with a reference you cant change the address you assign to it.
So its a use once and if your done with it then you cant use it again or bad stuff happens ref = address1 you cant change it to ref = address2 doesnt work.

With a pointer you can reassign address to that variable and use it over and over.

That is the difference between them.

Pointers make your coding life a lot easier and you will end up using them more and more. Its very normal for people to ask what is the use for them, but soon enough you will see why pointers and regerences are so important.

Share this post


Link to post
Share on other sites
rypyr: Actually that sort of polymorphism can be achieved with references too. You can reference a subtype just like you can with a pointer. The only constraint being that you can''t re-assign a reference like you can a pointer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The biggest use for pointeres in my opinion is dynamic memory allocation. In a game, for example, a newbie might do a CEnemyMonster aMonster[30]; for the monsters, a CGunShot aProjectile[50]; for the bullets, or something similar. When the best way to do this would be to dynamicly allocate the objects within the array, so you can have any number of them, without using memory for useless objects.

Trust me you WILL you it alot. (not to mention all the functions which demand a pointer as a parameter)

Share this post


Link to post
Share on other sites
You don''t need pointers at all; even for dynamic memory allocation! Just create a huge, massive honkin'' array of, say, 10000 entries! :D Problem solved, right? :D

(yes, this is sarcastic. ;P)

Share this post


Link to post
Share on other sites

  • 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!