0

I am studying pointers and found I get incorrect size of a pointer.

What I learned is that since p_num is a pointer to an int, it should show 4 bytes but is shows 8.

What could the possible reason for it?

#include <stdio.h>

int main()
{
    int num = 0;
    int * p_num = NULL;

    num = 10;

    printf("num address: %p\n", &num);
    printf("num size: %zd\n", sizeof(num));
    printf("num value: %d\n\n", num);

    p_num = &num;

    printf("p_num address: %p\n", (void*)&p_num);
    printf("p_num size: %zd\n", sizeof(p_num));
    printf("p_num value: %p\n", p_num);
    printf("p_num value pointed: %d\n", *p_num);

    return 0;
}

Output:

num address: 0x7fffcb0e8ffc
num size: 4
num value: 10

p_num address: 0x7fffcb0e8ff0
p_num size: 8
p_num value: 0x7fffcb0e8ffc
p_num value pointed: 10
Alan
  • 91
  • 6
  • 7
    Are you perhaps on a 64-bit system? Then pointers are usually 64 bits (i.e. 8 bytes). Why do you think that the pointers should be 4 bytes (32 bits)? – Some programmer dude Mar 02 '20 at 10:07
  • 2
    The type of object that a pointer points to is not relevant to the pointer's size; the pointer has to be big enough to hold any address that the system may use - in your case, (presumably a 64-bit system) that size is 8 bytes. – Adrian Mole Mar 02 '20 at 10:08
  • 1
    Yes, I am testing on a 64-bit machine. – Alan Mar 02 '20 at 10:09
  • 2
    *What I learned is that since `p_num` is a pointer to an `int`, it should show 4 bytes* Where did you learn that? Because that's just plain wrong. – Andrew Henle Mar 02 '20 at 10:10
  • Different types have different sizes. And "pointer to `int`" (`int *`) is a different type from `int`. – Some programmer dude Mar 02 '20 at 10:10
  • The address of the num value should also give you a clue. The value 0x7fffcb0e8ffc does not fit in a 32 bit variable. So if the pointer was only 32 bits (4 bytes) long it could only store the 0xcb0e8ffc part of the address and thus be pointing to the wrong address. – Roel Balink Mar 02 '20 at 10:39
  • @AndrewHenle I would not go as far as to say it is plain wrong, since pointers usually occupy 4 byte on 32-bit architectures, but at least incorrect in its actual form. It happens when you learn from a vague source, like they can be found all over the place in the web, just teaching something but not really clarifying the models and this later leads to misunderstandings and false mindsets. – RobertS supports Monica Cellio Mar 02 '20 at 11:34
  • @Someprogrammerdude "*Why do you think that the pointers should be 4 bytes (32 bits)?*" I guess because he has learned from vague sources, not considering the difference between architectures - probably from a time where 64-bit architectures wasn´t common or simply not existant. – RobertS supports Monica Cellio Mar 02 '20 at 11:36

5 Answers5

2

What I learned is that since p_num is a pointer to an int, it should show 4 bytes but is shows 8.

Your assumption is incorrect.

The int is one variable.

The int pointer is another variable.

So two different variables and these two variables do not need to have the same size.

In other words:

printf("p_num size: %zu\n", sizeof(p_num));   // Gives you the size of the pointer

printf("p_num size: %zu\n", sizeof(*p_num));  // Gives you the size of the pointed to object
                                   ^
                                   notice the *

In your case you see that size of int is 4 while size of int pointer is 8. This is perfectly legal.

Most likely, you could have made the size difference even bigger by looking at a char and a char pointer. Then you would most likely see 1 for char and still 8 for char pointer.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
2

What could the possible reason for it?

The size of a pointer is dependent upon the architecture. On 32-bit architectures, a pointer usually occupies 4 byte in memory, on 64-bit architectures commonly 8 byte.

So there is nothing wrong with the program or your system itself, if you use a 64-bit architecture as you said in the comments. You just had an incorrect assumption of:

What I learned is that ... a pointer to an int ... should show 4 bytes.

Furthermore, this is usually valid for all pointer types, not only pointers to int. They usually have the same size in memory, but the standard does not require that as you can see in the quote from ISO/IEC 9899:2011 (C11) in this answer regarding to the exact question for that.

The size of a pointer may be also different from the size of the object the pointer point to. So differ between the size of a pointer object and the size of an object the pointer point to.

1

sizeof(p_num) is the size of the pointer, not the size of the thing it points to.

sizeof(*p_num) is the size of the thing it points to.

Also, the parentheses are unnecessary. sizeof p_num and sizeof *p_num work fine. Parentheses are needed only to take the sizes of types (rather than expressions) or of complicated expressions.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

In order to address more than 4gb in memory the pointers should be 8 bytes of size. Addresses of Memory more than 4gb cannot be represented using 32 bits i.e. 4 bytes.

0

In the 64-bit machines, the size of pointer is 8 bytes, because a pointer is a memory address. The pointer p_num pointes to the varibale num that is just one of several integer types. In the 64-bits system, sizeof(int) = 4.

Hitokiri
  • 3,607
  • 1
  • 9
  • 29