ECS 30 Lecture Notes - Lecture 11: Memory Address, Memory Management, Array Data Structure
Wednesday, 25 April 2018
ECS 030 - Lecture 11
Pointers Continued + Start of Arrays
Pointers Usage
-The idea of accessing objects through their memory address is used in many ways:
•1. Output arguments for functions
•2. Arrays
•3. Character strings
•4. Dynamic memory allocation
•5. Function pointers
-All usages will be discussed in further detail later throughout the course
Variable Swap (without pointers)
-swap() - function you can write yourself to
swap values of variables
•swap() does not receive variables a and
b, but only their value
-since swap() is its own function, it
does not actually operate with the
variables themselves (a and b), but
only with their value, the variables
themselves are not altered
-meaning: the global addresses of the
variables a and b, after being plugged
into the function swap(), are not
altered or touched
-cannot swap values without
pointers
•graphical representation:
1
Wednesday, 25 April 2018
Variable Swap (with pointers)
-Through pointers and dereferencing, swap() actually operates on a and b
•if you use the * in front of the variable in the function, the function changes the true
values of the defined variables by reaching their memory address
-x and y are output parameters, with side-effects outside of function
•only used as tools to point to the variables
"
2
Document Summary
The idea of accessing objects through their memory address is used in many ways: 1. All usages will be discussed in further detail later throughout the course. Swap() - function you can write yourself to swap values of variables: swap() does not receive variables a and b, but only their value. Since swap() is its own function, it does not actually operate with the variables themselves (a and b), but only with their value, the variables themselves are not altered. Meaning: the global addresses of the variables a and b, after being plugged into the function swap(), are not altered or touched. Cannot swap values without pointers: graphical representation: X and y are output parameters, with side-effects outside of function: only used as tools to point to the variables. Drawbacks: approach is not scalable, won"t work if you have 100 grades.