1/21
Information About c++
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is a pointer?
• A pointer stores the memory address of another variable.
What must be specified when declaring a pointer?
When declaring a pointer, we must
specify what type of value it will be
pointing to.
A pointer to an integer is different
than a pointer to a double, etc.
WHY?
They point to different data sizes.
They point to different types, int* points to iptr and double* points to dptr
How do you declare a pointer?
To declare a pointer, follow the data
type the pointer will be pointing to
with an asterisk.
Example
int * intPointer; // intPointer will hold
// the address of an
// integer
What must you do where several pointers are declared?
You must precede each
pointer name with an asterisk.
- int* x, y;
// x is a pointer to an
integer, y is an integer
- int w, * z;
// w is a pointer to an
integer, z is a pointer
to an integer
What will the reference operator “&” return?
The reference operator, &, will return
the address of a variable.
What will the dereference operator * do?
The dereference operator,*, will
dereference a pointer. Which means to go to the address and get the value.
Example:
int x = 3;
int *xptr = &x;
*xptr += 5;
cout << x << endl;
Output will be 3 sine the value remains unchanged.
& and *
These operators are on the same
precedence level as the pre-increment
and decrement operators.
• These operators commute from right to left.
Can you assign a pointer of one type to a pointer of a different type? Yes or no, if no why not?
We can not assign a pointer of one
type to a pointer of a different type.
Example:
int *xtpr = &x
double *yptr = &y;
xptr = yptr
//error
What happens when you add to a pointer?
Adding an integer value to a pointer
changes the address that the pointer
is holding, thus changing the memory
location that the pointer is pointing to.
• Ex the ++ operator would move the
pointer to the next memory location.
Why do would we use pointers as
function parameters if we already
have call by reference capabilities?
Pointers are more versatile.

Should the pointer parameter for Func have any const qualifiers?
No const qualifier should be added to the pointer parameter because Func modifies both the values pointed to and the pointer itself.

What could you do to prevent modifying the values from the picture?
If you wanted to prevent modifying the values, you'd use const int* ptr, but that would break the function.
What could you do to prevent modifying the pointer?
If you wanted to prevent modifying the pointer, you'd use int* const ptr, but that would also break the pointer increment.

Is the function call on the second line of the main valid?
The function call Func(x); is valid. When you pass an array to a function, the array decays to a pointer to its first element. So x is implicitly converted to int* pointing to x[0].

What will be the output of this code?
Will grab 5 elements from the array. (100, 84, 64, 97, 0). The elements in the rest of the array are set to 0.
The while loop shows that if the number is greater than 0 then 5 will be added to the value.
So, x[0]= 100+5=105. x[1]= 84+5=89. x[2]=64+5=69. x[3]=97+5=103. x[4]=0.
Loop will stop at index 4. So the final output will be. 105 89 69 103 0 0 0 0 0 0
What can change using variables declared each of the following ways?
type * variable
const type * variable
type * const variable
const type * const variable
The pointers target and the value being pointed to.
The pointers target but cannot change the value being pointed to.
The value being pointed to but cannot change the pointers target.
You cant change anything. (The value being pointed to or the target)
What does “new” stand for in c++?
It is used to allocate memory dynamically at runtime.
What does “delete” stand for in c++?
It is used to free dynamically allocated memory.
*Use “delete” if you use “new”.
When to use “new” and “delete”. With or without [].
When you use “new” use “delete”
When you use “new[]” use “delete[]”
What is direct memory allocation?
When memory is reserved at run-time (as
opposed to compile-time).
What is a memory leak?
Its when memory is reserved for the program but we’ve lost track of where it is / aren’t using it.