5

Well, sorry for the question, is more like a general culture one (haven't found precise answers).

If I have something like

char * Field

or

void * Field

or

double pointers
  • The size of the pointer is the same? (as far I remember from college it was 4 bytes but ...)
  • Is the size of the pointer the same depending of the architecture of the CPU?
  • If I point to a data structure, the size of the the pointer itself is the same, isn't it?

Assume the examples in C (I would be prone to believe that it will be the same for other languages that does not handle pointers directly)

4 Answers4

3

The size of the pointer is the same? (as far I remember from college it was 4 bytes but ...)

Not necessarily the same and not necessarily 4 bytes: Are all data pointers the same size in one platform for all data types?

Is the size of the pointer the same depending of the architecture of the CPU?

It varies from archtecture to architecture. Even on the same hardware it can vary from operating system to operating system (e.g. 32-bit vs 64-bit).

If I point to a data structure, the size of the the pointer itself is the same, isn't it?

Again, not necessarily: Are all data pointers the same size in one platform for all data types?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Interesting and confusing. I will research more, specially the size differences. Thank you. –  Feb 08 '12 at 16:48
1

A 32-bit system usually has pointers of size 4 bytes and a 64-bit machine, usually has pointers of 8 bytes size. The keyword here is of course - usually, it is entirely possible that the device you maybe using is based on Harvard architecture(Or some other bus architecture scheme), which has separate memories for data and code regions. Hence separate buses with different widths, therefore it can be a possibility that the size of variable pointers (int*, double*, long int* etc.) is 8-bit but the size of function pointer is 16-bit, in the very same architecture.

Ajay Rajan
  • 31
  • 4
  • 1
    It's not necessarily related to Harvard architecture either. If we look at small microcontrollers of 8 or 16 bit CPU (Harvard or von Neumann) they might wish to use data and/or code addressing beyond 64kb and then you need a special kind of extended pointers for that, of 24 or 32 bit. – Lundin Jan 04 '22 at 10:28
  • Yes, I used Harvard architecture just as an example to make OP understand the possibilities. However, I have edited the answer to make it sound more clearer. Thanks for the input. – Ajay Rajan Jan 04 '22 at 11:39
1

In most systems, the size of the pointers is same, but C don't guarantee that. It's just promise you that void* is wide enough to contain every pointer type (except of pointer to function). and yes - it depends of the CPU. (In 64bit systems, pointer is usually 8 bytes)

asaelr
  • 5,438
  • 1
  • 16
  • 22
-1

1) The size of a pointer is the same for all pointer types.

2) Generally on a 32 bit architecture it will be 4 bytes, and on a 64 bit architecture it will be 8 bytes.

3) The size of the pointer will be the same no matter what you point it to.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 1
    `The size of a pointer is the same for all pointer types.` this isn't true. [There are platforms where `sizeof(char*) > sizeof(int*)`](https://stackoverflow.com/q/916051/995714). And nowadays you can almost always see function pointers have different size than data pointers on Harvard architectures – phuclv Jan 04 '22 at 09:49