C plus plus allows you to have direct access to memory throughout 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 has provides an extra source of errors so some some can be fatal for your applications
or even worse, because poor use of a memory buffers can open security holes in your code that can allow
malware to take over the machine.
So clearly pointers are important aspect of a C plus plus.
So in this lecture you will see how to declare a pointers and initialize them to memory locations.
How to allocate memory on the stack C plus plus restore, and how to use C plus plus arrays.
So C plus plus uses the same syntax.
As see here because in C and C plus plus to declare pointers are the same and assign them to a memory.
And it has like like pointers, arithmetic.
So like c c plus plus allows you to allocate memory on the stack.
So there is an automatic memory cleanup when the stack frame is destroyed.
So and the dynamic allocation on the C plus plus free store where the programmer has a responsibility
to release memory.
So this section will be covered.
These aspects here so so the syntax access memory in C plus plus.
Is straightforward.
So the this operator.
The and operator returns the address of an object.
So the object can be a variable, a boolean type, or the instance of a custom type or even a function.
A which function pointers will be covered in next lectures.
So the address is assigned to a tight 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 a type of this ternary symbol.
So for example, let's declare a pointer here, integer A or let's first let's declare it just an usual
integer, for example, 92 and then integer P here this ternary operator and operator here, here.
So in this code, the variable A actually the variable A is an integer.
And the compiler and 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 later
section.
So actually, let me open our diagram here.
Okay.
Here.
So.
At runtime.
Uh, this 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 92in 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 code is illustrated the memory usage of this code here.
So the pointer holds a value of eight.
See here this memory address.
Notice that the lowest byte 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 92.
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 allocated on the stack like this, you should make no assumptions about where in
memory the variables are allocated so nor their location in relation to the other variables.
So this could assume a 32 bit operating system.
And so the pointer.
A P occupies 32 bits and contains 32 bit address.
If the operating system is 64 bits, then the pointer will be 64 bits wide.
But the integer may still be in 3032 bits.
Just one note here.
So in this lecture, in this course, we will use 32 bit pointers for the simple convenience that 32
bit addresses take less typing than 64 bit addresses.
So the typed pointer is declared with the 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 this 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 a real practicing here.
Here.
And let me.
Close this diagram.
Okay.
So however here, it's important, as I said earlier, to use the syntax if you declare more than one
pointer.
So, for example, we can declare BP 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 goes.
As I said earlier, in C spaces, in most cases cplusplus doesn't matter.
So it's it's because the visual.
No problems here.
So it's not it 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 lines in this case here, CP here CPI.
So when you apply the size of operator to a pointer here, you will get the size of the pointer, not
what it points to.
So this on 32 bit machine here, size of here, size of a size of this integer pointer will return.
Will return a return.
Will return for in 32 bit machine.
Will return.
Eight return.
Eight.
In 64.
Bit machine here.
So.
This is an important observation, especially when we discuss the C plus plus 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.
Now let's write an example code here.
As you can see, we have.
Let's actually let's delete these quotes because we don't need it anymore.
And add this here.
So let's write an integer here and we'll take the pointer.
P So use like this on the right hand side of an assignment.
The 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 the this symbol is also used but has a different
meaning.
So the dereference 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 const keyword which you will learn about later.
So you can dereference the pointer to write to a memory location to for example.
Um.
Okay.
C out.
See out here a.
Or we have to use steady.
See out a.
Here.
Listed.
And then.
Now let's declare this point here and change this.
Change this variable.
To.
A.
And then integer or Yeah P equals 99.
Here.
Or let's, for example, make it 600.
And see out.
See out.
A we have to STD out a here and end line STD and line.
So let's run this code here.
Yeah.
So.
In this code the pointer.
P.
Points to the location in a memory of the variable named A here.
So in this case, using this braces syntax here actually let's make spaces to make it more clear.
So assigning the the references 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.