views:

463

answers:

6

I know that pointers store the address of the value that they point to, but if you display the value of a pointer directly to the screen, you get a hexadecimal number. If the number is exactly what the pointer stores, then when saying

pA = pB; //both are pointers

you're copying the address. Then wouldn't there be a bigger overhead to using pointers when working with very small items like ints and bools?

+12  A: 

A pointer is essentially just a number. It stores the address in RAM where the data is. The pointer itself is pretty small (probably the same size as an int on 32 bit architectures, long on 64 bit).

You are correct though that an int * would not save any space when working with ints. But that is not the point (no pun intended). Pointers are there so you can have references to things, not just use the things themselves.

Zifre
In C++ there is a clear reference concept. The reason pointers exist is to be able to manage dynamically allocated memory, not references.
David Rodríguez - dribeas
@dribeas: I didn't mean C++ references, I meant the theoretical concept of having something that just tells you where to find something, so you can share the data.
Zifre
+3  A: 

Memory addresses.

That is the locations in memory where other stuff is.

Pointers are generally the word size of the processor, so they can generally be moved around in a single instruction cycle. In short, they are fast.

dmckee
+1  A: 

The number refers to its address in memory. The size of a pointer is typically the native size of the computer's architecture so there is no additional overhead compared to any other primitive type.

Niki Yoshiuchi
+2  A: 

As others have said, a pointer stores a memory address which is "just a number' but that is an abstraction. Depending on processor architecture it may be more than one number, for instance a base and offset that must be added to dereference the pointer. In this case the overhead is slightly higher than if the address is a single number.

Yes, there is overhead in accessing an int or a bool via a pointer vs. directly, where the processor can put the variable in a register. Pointers are usually used where the value of the indirection outweighs any overhead, i.e. traversing an array.

I've been referring to time overhead. Not sure if OP was more concerned space or time overhead.

Steve Fallows
+1  A: 

On some architectures there is an additional overhead of pointers to characters because the architecture only supports addressing words (32- or 64-bit values). A pointer to a character is therefore stored as a word address and an offset of the character within that word. De-referencing the pointer involves fetching the word and then shifting and masking it's value to extract the character.

Nat
A: 

An address in memory. Points to somewhere! :-)

mduvall