Sign in to follow this  
brandonman

Pointers: Lay the question to rest?

Recommended Posts

brandonman    102
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
rip-off    10976
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
Telastyn    3777
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
ToohrVyk    1595
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
tyfius    122
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
_goat    804
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
brandonman    102
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
dmail    116
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
JimmyDeemo    156
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
Kylotan    9850
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.


The fact that it's a memory location is almost incidental. The main thing that a pointer allows is for multiple places to reference the same thing. Without that, it would be awkward to deal with large objects that you don't want to make copies of, for example.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by tyfius
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.


Aside from the fact that you are incorrect (the standard does not require the size of int to be 32 bits, regardless of the system), even if you were right, I don't understand why what I said is incompatible with your assertion.

Share this post


Link to post
Share on other sites
tyfius    122
@ToohrVyk: I just wanted to clarify that one can indeed make the assumption that the size will be fixed on most modern systems (as explained here and here).

No standard defines a fixed size however, only a minimum size (which should be 16 bits for an int). Most modern compilers have agreed a fixed size to enhance portability between different systems.

Or at least, that's what I've always been told.

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