Pointers: Lay the question to rest?

Started by
11 comments, last by tyfius 16 years ago
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.
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.
Moved to For Beginners.
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.'
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.
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.
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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.
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
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 changedPassByReference(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.

This topic is closed to new replies.

Advertisement