- [Instructor] The size of operator is used to determine
the size of an object.
This is working.cpp from chapter four of the exercise files.
So I'm want to come in here and declare an integer.
And then another value of type size t.
And I'm want to use the size of operator
to get the size of x.
And then I'm want to print it with a printf.
So,
size t,
is actually a type (indistinct).
And it's a type that's used specifically
for the return value of size (indistinct).
It's used for the size of things.
It means different things on different systems.
It's a type (indistinct),
but it's always used for this purpose.
It's typically something like a const, unsigned long int.
And printf has a special format specifiers zd.
Which is specifically used for the size t type.
So I'm want to go ahead and I'm going to save this and compile
and run and you see the size of x is four.
And that size is always in bytes.
So if I want to get in bits, I have to multiply by eight,
for eight bits to a byte.
And when I do that, you see that the size of x
which is an integer on this system is a 32 bit integer
it's four bytes.
You'll often see size of used with parentheses
for its operand.
So you'll see it like this as if it were a function.
And, you know, again,
I'll build and run this and works fine.
The parentheses, you know, in this case,
it's just putting that operand in parentheses.
But it can also be used for a type.
If I say size of int,
I'll get a warning that x is no longer used.
And that's fine I build and run this
you see I get the same value.
But in this case the parentheses are required.
If I try to do this without parentheses
you'll notice I get an error because it's a type name
and it's not a variable.
And so it requires those parentheses
which are effectively a cast.
But if I use it with the value x
that does not require parentheses
because x is actually a variable.
If I go ahead and declare a structure
and call it capital X.
And I'll put a semi colon here and give it some, say,
couple of integers
couple of cares
And some long integers
We'll call that capital X now.
And I'll say size of and I need to use parentheses now
because x is a type.
It's a class not an object.
It's a structure.
And so if I build and run this, you see,
this takes up 32 bytes, which is 128 bits.
So the size of operator can be useful
when you need to find out how much space is required
for an object or a type.