In this video, we'll learn about c++ sizeof operator
and about the climits and cfloat include files.
C++ has an operator called sizeof
that returns the number of bytes used to represent any type.
Sizeof can also be used to determine the sizeof compound types like arrays,
structures and objects. We'll learn about compound types in a future section.
In order to determine the sizeof a specific type,
we provide the name of the type inside the parentheses.
For example, sizeof int, sizeof double.
This will tell us the sizeof that type in
bytes on the specific machine and compiler that you're running the program on.
You can also determine the sizeof variables using their names.
In this case, you can use the same syntax
or you can omit the parentheses since they're optional for variables.
The sizeof operator gets its information from
two c++ include files, climits and cfloat.
Climits provides information about integral types
and seefloat about floating point types.
These include files also provide a bunch of handy defined constants
that we can use to determine the precision of our primitive types.
For example,
int max tells us the maximum value we can store in an int on this specific machine.
There are many others, and you can see some in the examples in this slide.
Let's head over to the CodeLite IDE, and we'll see what some of these values are in my machine.
I'm in the CodeLite IDE
and I've written some code that uses the sizeof operator. Let me walk you through it.
First thing we'll see is that we're including iostream
and we're also including climits.
Everything I'm using here will use climits.
You can get the same information in cfloat, and I'll leave that as an exercise for you if you like.
First thing we'll do is we'll output sizeof information for types.
So in this case, I'm going to output char,
followed by sizeof char and bytes,
sizeof int, sizeof unsigned int
short, long and long, long.
Now when I run this,
I'll get information about how
big or how much storage is necessary
to represent a character an integer and so forth
on my specific machine using this specific compiler.
Okay. Now the rest of the code is commented out. And again, I'll uncomment that
as we go through. So what I'll do is
and I've created a project here called sizeof operator project,
and you can run this code on your machine and see what your values look like.
So I'll build and run.
And you can see the sizeof information that's come up.
A character is 1 byte, an integer is 4 bytes,
an unsigned integer is also 4 bytes, that makes sense
because the only difference between an int and an unsigned int
is that signed bit and how much precision you can store in it
a short integer is 2 bytes. A long integer is 4 bytes.
A long, long integer is 8 bytes. And there you go.
Now I'm running a 32-bit compiler on a 64-bit operating system.
If you're running a 64-bit compiler, these numbers will likely be different.
All right. So that takes care of the the basic sizeof for the integral types.
Let's look at some of the floating point types.
So in this case, you can see that I want the sizeof a float,
a sizeof a double and the sizeof the long double.
Okay. So what we can do is we can build and run.
And you can see now that a float on this particular machine again is 4 bytes,
a double is 8 bytes and a long double is 12 bytes.
So that's the amount of storage that it takes to represent
these specific types on this machine.
Okay. So now the climits file
contains some pretty nice defined constants,
and let's use some of them. You can see what it's like. In this case,
I want minimum value. So I want to know what's the minimum value
that I can store in a character, what's the minimum value that I can store in an integer,
in a short integer, in a long integer
and in a long, long integer. And you can see the defines are right here,
and those will default to some integers.
So let's let's run this now. Again, I'll build and run.
And you can see that the minimum value I can store in a character is -128.
That makes sense because a character is 1 byte big.
In an integer, you can see the number there.
In a short -32768. The long and the long,
long your numbers may look different on your machine depending on a lot of factors.
All right. Now we can do the maximum values,
and it's exactly the same except.
We're doing char max, int max instead of char min and int min.
I'll run this again, and I'll press ctrl F5 to run.
And now, our maximum values are for characters 127,
for short 32767 and for long, longs,
you can see that's a pretty big number.
All right. So that's the use of climits and sizeof with types.
We can also use sizeof with variable names. And I'll show you an example here
where what we have here is
here we've got an integer called age,
and we're assigning 21 to it. We're not going to use the 21 at all.
All we're interested in is the sizeof that variable.
So rather than sizeof int, here we're saying sizeof age.
This is one syntactic variant.
We can also just say sizeof age without the parentheses.
So I'm doing that for an integer called age.
And I'm doing the same for a double called wage.
So obviously, we would expect the double to be much bigger.
But again, what I'm doing in here is I'm using the variable name
instead of the type here as well.
All right. So let's run this one more time.
And what we've got now at the bottom here is that
the sizeof using variable names. So age is 4 bytes.
And if you recall here we know that age is an integer right,
and integer up top is 4 bytes if you recall that statement at the top.
So age is 4 bytes.
The other syntax variant gives you exactly the same result.
And wage is a double, which is 8 bytes and you can see our doubles here is 8 bytes.
You may be wondering why in the world would I ever use this stuff.
Well, it's important a to understand
what the size and precision of your
types are on your system.
Later on when we deal with arrays and other data structures,
sometimes it's nice to be able to understand how big something is,
how big each section of it is.
This is one of the big differences between c++ and other languages
like java and python.
But those languages, you're really far removed from the machine.
So you're programming at a much higher level.
C++, we're at a lower level. So we've got to deal with this kind of thing.