C++ allows you to have direct access to memory for pointers.
So this gives you a lot of flexibility and potentially it allows you to improve the performance of your
code by eliminating some unnecessary copying of data.
So, however, it also provides an extra source of errors so some can be fatal for your locations,
or even worse, because of poor use of a memory.
Buffers can open security holes in your code that can allow malware to take over the machine.
So clearly the pointers are important aspect of C++.
So in this lecture you will see how to declare a pointers and initialize them to memory locations.
How to allocate memory in the Stack C++ three store and how to use C++ arrays.
So C++ uses the same syntax.
As a C here because in C and C++ to declare pointers are the same and assign them to a memory.
And it has like, like pointers arithmetic.
So like C, C++ allows you to allocate memory on the stack.
So there's an automatic memory cleanup when the stack frame is destroyed.
So and the dynamic location on the C++ free store where the programmer has a responsibility to release
memory.
So this section will cover these aspects here.
So, so the syntax access memory in C++.
He's straightforward.
So this operator.
The end operator returns the address of an object, so the object can be variable.
It will tintype or the instance of a custom type or even a function.
Which function pointers will be covered in next lectures.
So the address is assigned to a tied pointer or a void pointer here.
Void pointer.
So the void pointer should be treated as merely storage for the memory address because you cannot access
data and you cannot perform pointer arithmetic.
This means that manipulate the pointer values using arithmetic operators.
So you can't do an on these actions on a void pointer.
So the pointer variables are usually declared using the type of this you ternary symbol.
So for example, if we had a pointer here integer A or the first list is just an usual integer, for
example, 92.
And then integer key here.
This term operator and operator here.
Here.
So in this code, the variable A actually the variable A is an integer.
And the compiler and the linker will determine where this variable will be located at.
So usually a variable in a function will be on a stack frame as described as we will describe in a later
section.
So actually, let me open our diagram here.
Okay.
Here.
So.
At a runtime.
The stack will be created.
Essentially a chunk of memory will be allocated and the space will be.
Reversed reserved here in the stack memory for the variable.
So then the program puts the variable 9 to 2 in that memory.
Next, the address of the memory allocated for the variable A is placed in the variable P here.
The memory usage in this court is I illustrated the memory of this court here.
So the pointer holds value of eight.
See here this memory address.
Notice that the lowest part is stored in the lowest byte in memory.
So this is for example, this code illustrates the 32 bit machine.
So the memory location here has a value of two A and six zero.
And that is a value of 90 to the value of variable A here signs P is also variable.
It also occupies space in the memory, and in this case the compiler has put the pointer lower in memory
than the data it points to.
And in this case, the two variables are not contiguous.
So with the variable I at in the stack like this, you should make no assumptions about where in memory
the variables are allocated.
So now the real question in relation to the other variables.
So this could assumes 32 bit operating system and so the pointer.
P occupies 32 bits and contains 32 bits.
Address.
If the operating system is 64 bits, then the pointer will be 64 bits wide, but the integer may still
be into the 32 bits.
Just one note here.
So in this lecture, in this course we will use 32 bit pointers for the simple components that 32 bit
addresses take less typing than 64 bit others.
So the typed pointer is declared within this symbol here.
So and we will refer to this as an integer pointer here because the pointer points to memory that holds
an integer when declaring a pointer.
The convention is to put the ternary operator next to the variable name rather than next to the type.
So the syntax emphasizes that the type pointer to is an integer.
So however, it is important to use this syntax if you declare more than one variable in a single statement
here.
So actually, let's let me show this in real practicing here.
Here.
Let me.
Close this diagram.
So however here it's important, as I said earlier, to use this syntax.
If you declare more than one pointer.
So, for example, we can declare b p here and.
Oops.
Here we can declare the.
B.
P.
And.
See here.
Um.
Here.
We declared more than one variable in a single statement.
It's clear that the first variable is an integer pointer and the second is an integer.
But the following year is not so clear.
As you can see here.
You might interrupt this to mean that the type of both variables is an integer, but this is not the
case here as this declares a pointer and integer.
So this is the pointer and this is the integer.
So if you want to declare two pointers, then apply this on each variable.
So I want to say that this doesn't matter where it's false.
As I said earlier, in spaces, in most cases, C++ doesn't matter.
So it's it's because the visual.
No problems here.
So it's not it's you.
If you put this ternary operator here, it will be nice and more explanatory for your code.
So it's probably better to just declare two pointers in separate lives in this case.
Here.
P.
Here.
C.
P.
So when you apply the size of operator to a pointer here, you will get the size of the pointer, not
what it points.
So this is on 32 bit machine here, size of here, size of size of this integer pointer will return.
Will return.
Let's turn.
Will return for Inter to beat machine.
Will return.
Eight.
Return.
Eight.
In 64.
Bit.
Machine.
Here.
So.
This is an important observation, especially when when we discussed the C++ built in arrays in later
lectures.
So to access the data pointed to by a pointer, you must reference it using the using this operator
here.
So.
And let's write an example quote here.
As you can see, we have.
What's the actual distillate this?
Because we don't need it anymore.
And this here.
So let's write integer G here and we'll take the pointer P.
So use like this on the right hand side of an assignment.
The reference pointer gives access to the value pointed to by the pointer.
So g here is initialized to 92.
So compare this to the declaration of pointer where that this symbol is also used but has a different
meaning so that the reference operator does more than give a read access to the data at the memory location.
As long as the pointer does not restrict it using the keyword which you will learn about later.
So you can the reference the pointer to right to a memory location to, for example, see out.
See out here a.
Or we have to use a steady.
See out a.
There.
It's TV.
And then.
Let's declare this point here and change this.
Change this variable.
To.
A.
And then integer or Yeah, P equals 99.
Here.
Oh, let's, for example, make it 600.
And see.
Out.
See out.
A we have digested the out A here and and line steady and line.
So let's run this quote here.
Yeah.
So.
In this quote the pointer.
P.
Points to the location in a memory of the variable named A.
Here.
So in this case, using this precise syntax, you'll actually make spaces to make it more clear.
So assigning the reference at pointers assigns the value to the location that the pointer points to.
So the result is that on the last line here, the variable A will have a value of 600, not 92.