Why is a pointer 8 byte while the int value is 4 byte?


#1

While using the sizeof() function, I’ve learned that the size of the pointer is 8 bytes, while the size of the int value is 4 bytes.

This doesn’t make sense to me.

I’m thinking that the pointer places to a memory address, while the int is a variable which holds the value (at that point in time, depending on what part of the stack we are in.) Shouldn’t the value of the variable held in memory be larger, while the address should be a constant, depending on the memory size available in the computer?

What am I missing?


#2

sizeof () is not a function; it is unary operator.

[quote] I’m thinking that the pointer places to a memory address, while the int is a variable which holds the value (at that point in time, depending on what part of the stack we are in.) Shouldn’t the value of the variable held in memory be larger, while the address should be a constant, depending on the memory size available in the computer?
[/quote]
Sizes of objects are constant, independent of the stack.

You should always trust what the sizeof operator says. If it says the size of an int is 4 bytes, then the size of an int is 4 bytes on the platform you are working on.


#3

sizeof () is not a function; it is unary operator.[/quote]

Looks like a function to me. Its’ name precedes left and right parentheses, just like a function ought to. (See, Hillegass text at pg.31) And at pg. 58, in connection with the specific lesson that prompted the original post to this thread, the author specifically states that sizeof() is a function. (“The sizeof() function returns a value …”)


#4

[quote]…specifically states that sizeof() is a function. (“The sizeof() function returns a value …”)
[/quote]
The book says that but doesn’t really mean it, because the book is not teaching the C Programming Language in depth.

If something is a function, you can take its address:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSLog (@"%p", main);
    NSLog (@"%p", printf);
    NSLog (@"%p", &main);
    NSLog (@"%p", &printf);
    NSLog (@"%p", sizeof);   // Error
    NSLog (@"%p", &sizeof);  // Error

    return 0;
}