Understanding Pointer sizes

http://pw1.netcom.com/~tjensen/ptr/ch2x.htm
"Also we have learned that on different systems the size of a pointer can vary. As it turns out it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble attempting to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types."

So based on this text pointer sizes can vary, but when I use
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    int number = 10;
    int* pNumber = &number;
    void* pVoid = &number;
    cout << "*pNumber: "       << *pNumber       << "\tsizeof(pNumber): " << sizeof(pNumber)       << "\n";
    cout << "*((int *) pVoid): " << *((int *) pVoid) << "\tsizeof(pVoid): "   << sizeof(sizeof(pVoid)) << "\n";
    return 0;
}

It it always says the size of the pointer is 4 bytes, no matter if I try finding the size of an int, void, char pointer, so this is really confusing me, can the size of a pointer vary?

-edit- 32 bit computer btw
Last edited on
It can, but that doesn't mean it has to.
There's usually no reason for their sizes to be different (although pointers to member functions often have varying sizes).
@Athar Why though? if generally the definition of a pointer is simply a variable that holds a memory address, aren't all memory addresses the same size? (32bits)
No, they aren't. The world of computers isn't limited to your desktop.
32 bits offers 2^32, or 4,294,967,296 different possibilities. If you have more than 4 gigs of RAM, you will need more than 32 bytes to address it.
Anyone here remember the days of far and near pointers?
There are four categories of pointers; each may have a different size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct A { int i ; void foo() const {} } ;

int main()
{
    void* pv = 0 ; // contains an address
    std::cout << "size of pointer to (possibly cv-qualified) object: " << sizeof(pv) << '\n' ;

    void (*pfn)() = 0 ; // almost always contains a location in code
    std::cout << "size of pointer to free function: " << sizeof(pfn) << '\n' ;

    int A::*pmv = 0 ; // typically contains an offset (often offset+1)
    std::cout << "size of pointer to member variable: " << sizeof(pmv) << '\n' ;

    void (A::*pmf)() const = 0 ; // typically a union;
    // either a location in code (non-virtual) or an offset into to the vtbl (virtual)
    std::cout << "size of pointer to member function: " << sizeof(pmf) << '\n' ;
}


On my implementation:
size of pointer to (possibly cv-qualified) object: 4
size of pointer to free function: 4
size of pointer to member variable: 4
size of pointer to member function: 8
Topic archived. No new replies allowed.