Jump to content
  • Advertisement
Sign in to follow this  
brandonman

Pointers: Lay the question to rest?

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

Ok, I just want to clarify, a C++ pointer is a quicker way to access a value stored in memory? Or am I understanding this wrong? What I understand is it speeds up the program because int x=y is somewhat slow, while int *x, x=&y is faster (Right Syntax?) Explain this to me if I am wrong.

Share this post


Link to post
Share on other sites
Advertisement
You are wrong. The underlying reason for using a pointer is not speed. We use pointers only when they are necessary (which isn't often in modern C++). Here is an explanation of pointers, along with the surrounding concepts.

Share this post


Link to post
Share on other sites
On most computers, that's the same exact operation an int isn't any bigger/smaller than a pointer. The concept of a pointer isn't as simple as 'do this it's faster.'

Share this post


Link to post
Share on other sites
Quote:
Original post by brandonman
What I understand is it speeds up the program because int x=y is somewhat slow, while int *x, x=&y is faster (Right Syntax?) Explain this to me if I am wrong.


This is a partial understanding. You are wrong for three different reasons.

  1. In C++, a pointer is usually as large as an integer, if not larger. Therefore, manipulating integers will be at least as fast as manipulating pointers. The speed of copy only comes into account when manipulating larger types, or types which have non-trivial copy constructors.
  2. int x = y; makes a copy of y called x. int *x = &y; does not create a copy of y, but instead references the value of y. In the first case, you have two independent values, while in the second case, you have only one. As such, the second option is only usable if you don't need a copy—if you don't need a copy, however, it will indeed be faster for complex types.
  3. A pointer is both a random access iterator and an option type, which makes it a hackish way of representing reference semantics. If you wish to manipulate an object without making copies, you should instead consider references in almost all practical cases.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVykIn C++, a pointer is usually as large as an integer, if not larger. Therefore, manipulating integers will be at least as fast as manipulating pointers. The speed of copy only comes into account when manipulating larger types, or types which have non-trivial copy constructors.
True, but a pointer should always be 4 bytes on a 32-bit system and 8 bytes on a 64-bit system while an integer is only 4 bytes large on both systems. If it has any other size you are using a non-conform compiler (or setting) and that will most likely also cause other things to be incorrect.

Share this post


Link to post
Share on other sites
Quote:
Original post by tyfius
Quote:
Original post by ToohrVykIn C++, a pointer is usually as large as an integer, if not larger. Therefore, manipulating integers will be at least as fast as manipulating pointers. The speed of copy only comes into account when manipulating larger types, or types which have non-trivial copy constructors.
True, but a pointer should always be 4 bytes on a 32-bit system and 8 bytes on a 64-bit system while an integer is only 4 bytes large on both systems. If it has any other size you are using a non-conform compiler (or setting) and that will most likely also cause other things to be incorrect.


No. There aren't those kinds of guarantees.

Share this post


Link to post
Share on other sites
OK, I understand they reference a variable without copying it, but what exactly is the use for them? I never could understand the use of knowing a variable's address location.

Share this post


Link to post
Share on other sites
Quote:
Original post by tyfius
Quote:
Original post by ToohrVykIn C++, a pointer is usually as large as an integer, if not larger. Therefore, manipulating integers will be at least as fast as manipulating pointers. The speed of copy only comes into account when manipulating larger types, or types which have non-trivial copy constructors.
True, but a pointer should always be 4 bytes on a 32-bit system and 8 bytes on a 64-bit system while an integer is only 4 bytes large on both systems. If it has any other size you are using a non-conform compiler (or setting) and that will most likely also cause other things to be incorrect.

Also wrong

Share this post


Link to post
Share on other sites
Quote:
Original post by brandonman
OK, I understand they reference a variable without copying it, but what exactly is the use for them? I never could understand the use of knowing a variable's address location.


One use of pointers and references is for to not having to copy something big, such as a user define class.

void myFunction( CMyClass class )
{
class.doSomething();
}

void myNextFunction( const CMyClass &class )
{
class->doSomething();
}




In the first one, the function recieved a COPY of your class, meaning it calls the copy constructor and has all of is member variables copied.

In the second one you are passing by reference, and giving the function the memory address of the object you want to do things with.

Can you see why the second one might be benefcial for larger classes?

Another use is that you might want to change a variable in a function and have it alter that variable in a higher scope. For example;

void PassByValue(int num)
{
//Anything i do to 'num', is in local scope
}

void PassByValue(int &num)
{
//Anything i do to the reference is changing the variable at the scope where
//this function was called.
}

int g_MyNum;

PassByValue(g_MyNum); //g_MyNum will not be changed
PassByReference(g_MyNum); //g_MyNum could be changed by the function.



This might not look like behaviour you wouldn't desire, but you will learn how this can be useful.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!