This is a core concept in C. It's your breakthrough moment when you get it things start making sense.
A pointer is an address. I can have the address of your house written on a piece of paper and the paper doesn't need to be the size of your house.
I'm not being silly the way we use 'address' in C is closely associated to that real-life concept. But C pointers (addresses) are places in memory. They're represented by the value of some fixed number of bytes.
OK, so the postal system identifies your house by block of characters.
In computers we identify locations in memory be a sequence of bytes. Yes, we use bytes to store the location of other bytes.
malloc(sizeof(Person))
return the address of (a pointer to) a block of memory large enough to hold a Person
structure.
It didn't return the block. That's 'somewhere' identified by, can be 'found', accessed, written to and read from using the address.
An pointer/address isn't the thing it's a reference to a thing. Here using reference
in the broad computing sense not the slightly narrower sense it's used in C++.
Your machine clearly uses 64-bit addressing. There are 8-bits in a standard byte and and a size of 8 amounts to 64 bits.
The C Standard in some parts talks about pointers functionally. It tells you applying &
to suitable expression returns a pointer that can be de-referenced by *
or ->
to get to the thing or part of it respectively. But it also talks about the 'address-of' operator (being &
) and I believe why it uses ampersand which is a stylised 'a'.
To answer the questions.
sizeof(person)
is 8 bytes because that's the address of 16 bytes that malloc()
has set aside for you to store 16 bytes as requested (the size of a Person
object).
The answer regarding person->name
is similar. It's the address of 16 bytes not itself 16 bytes.
Add printf("person: %p\n",(void*)person);
it will print out the address.
It's implementation defined how it comes out but it's almost certainly going to be like person: 0x7ffca93ce784
(but a different hex value).
That won't change if you change the values in it (e.g. person->age=50;
).
Do take heed of that advice that you need to use %zu
to output a size.
Also get into the habit of calling free(person);
when you've finished with memory you allocated. Render unto free()
anything malloc()
rendered unto thee.
If not that memory you were allocated can't be reused. That won't affect this little program. But it will become an issue later.
I'd say formalising the distinction between a reference and its referant is a key Computing notion. The something that identifies a thing and the thing. No one has an issue that their name refers to them but isn't them. But somehow when it all gets abstracted into computing you need to consciously understand the distinction more clearly.
All programming languages have this and useful ones have some notion of pointer-like things. In Java it's an Object Variable. That's a reference to an object. Not the object. It even lets slip and if you abuse want gives you a NullPointerException
. But I thought Java didn't have pointers! Secret.
The question you'll ask soon is why sizeof(Person)
is 16 but sizeof(char*)+sizeof(int)
is 12. What (you will ask) are the other 4 bytes doing? I won't go into it but there may be 'dead' bytes in a structure to make things line up nicely. Many computers need (or work more efficiently) if things like pointers and ints are stored at addresses with some low bits set to 0 - it's called alignment and it's a hardware thing!
person
is 8 bytes cuz it's a pointer and the same goes forperson->name
. – Megen%zu
to matchsizeof
. Again, it's a matter of size,%d
might expect a 32-bit argument, butsizeof
might supply a 64-bit value. – Metanephros%zu
I get a warning:unknown conversion type character 'z' in format [-Wformat=]|
– Johanna%zu
I suggest using%llu
and cast the operand:(unsigned long long)sizeof(buffer)
– Metanephros(int)
and keep the"%d"
(for these guaranteed to be small sizes,int
is definitely fine,long long unsigned
+"%llu"
should be safe in a more context-free fashion). – Megen%zu
– Johanna