Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and References

Similar presentations


Presentation on theme: "Pointers and References"— Presentation transcript:

1 Pointers and References
CSCE 221H Texas A&M University 1 1

2 Pointers & Memory 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20

3 5 Pointers & Memory x int x = 5; 0x00 0x04 0x08 0x0B 0x10 0x14 0x18

4 5 Pointers & Memory x y int x = 5; int* y = &x
operator ‘&’ takes the memory address of an object/variable 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 5 0x04 x y

5 5 Pointers & Memory x y z int x = 5; int* y = &x int* z = y; 0x04 0x04
0x0B 0x10 0x14 0x18 0x1B 0x20 5 0x04 0x04 x y z

6 Pointers & Memory x y z int x = 5; int* y = &x int* z = y; *z = 0;
operator ‘*’ dereferences a pointer in memory to access the underlying object/data 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 0x04 0x04 x y z

7 Allocating memory using new
Point *p = new Point(5, 5); new can be thought of a function with slightly strange syntax new allocates space to hold the object. new calls the object’s constructor. new returns a pointer to that object. Just like java. But you have to think about pointers.

8 Deallocating memory using delete
// allocate memory Point *p = new Point(5, 5); ... // free the memory delete p; For every call to new, there must be exactly one call to delete. No garbage collection.

9 Using new with arrays int x = 10; int* nums1 = new int[10]; // ok
int* nums2 = new int[x]; // ok Dynamically allocates an array of 10 integers C++ equivalent of the following C code int* nums = (int*)malloc(x * sizeof(int));

10 Using delete on arrays // allocate memory int* nums1 = new int[10];
int* nums3 = new int[x][4][5]; ... // free the memory delete[] nums1; delete[] nums3; Have to use delete[].

11 Destructors delete calls the object’s destructor.
delete frees the space occupied by an object. A destructor cleans up after the object. Releases resources such as memory, file handles, locks, etc.

12 Destructors – an Example
class Segment { public: Segment(); virtual ~Segment(); private: Point *m_p0, *m_p1; }; Ask about virtual. (what’s wrong here…)

13 Destructors – an Example
Segment::Segment() { m_p0 = new Point(0, 0); m_p1 = new Point(1, 1); } Segment::~Segment() if(m_p0) delete m_p0; if(m_p1) delete m_p1;

14 Syntactic Sugar “->”
Point *p = new Point(5, 5); // Access a member function: (*p).move(10, 10); // Or more simply: p->move(10, 10); Dereference the object then call one of its functions Using p to “point” at the method.

15 Stack vs. Heap On the Heap / Point *p = new Point();
Dynamic allocation On the Stack / Automatic allocation Point *p = new Point(); Point *ps = new Point[n]; Point p; Point ps[10]; On the left we have a pointer that we must dereference. Still must delete p on the left – problem. on the right, everything is ok. What happens when p goes out of scope?

16 Dynamic Memory (resizing an array)
S

17 Dynamic Memory (resizing an array)
S T int* T = new int[2*n];

18 Dynamic Memory (resizing an array)
S T for (i = 0 to n) T[i] = S[i];

19 Dynamic Memory (resizing an array)
S T delete[] S;

20 Dynamic Memory (resizing an array)
T S = T;

21 Passing by value void Math::square(int i) { i = i*i; } int main() {
cout << i << endl; What’s the output? 5, i was copied. When passing by value, a copy is always induced in c++ (not so in c++11). Only pass ‘trivially-copyable’ objects by value, e.g., int, double, char.

22 Passing by reference void Math::square(int &i) { i = i*i; }
int main() { int i = 5; Math::square(i); cout << i << endl; What’s the output now? 25, i was changed. When passing by reference, a copy of the memory address is passed into a function. Always pass large objects by reference, e.g., string, MyClass, vector<T>, etc.

23 What is a reference? An alias – another name for an object. int x = 5;
int &y = x; // y is a // reference to x y = 10; What happened to x? – it is now 10 What happened to y? – silly question? y is x.

24 Introducing: const void Math::printSquare(const int &i){ i = i*i;
cout << i << endl; } int main() { int i = 5; Math::printSquare(i); Math::printCube(i); Won’t compile. const means that the underlying data should not change. Only const functions can be called on const objects. If you can, you should make this guarantee. const extends the type to allow you to specify which data is read-only Why is this good? --- gets the compiler to help make sure certain values don’t change.

25 Can also pass pointers to const
void Math::printSquare(const int *pi) { *pi = (*pi) * (*pi); cout << pi << endl; } int main() { int i = 5; Math::printSquare(&i); Math::printCube(&i); Still won’t compile. What exactly are we saying is const here? It is the integer *pi that is const, not the pointer. pi can be changed, but we can’t change anything through it.

26 Declaring things const
const River nile; const River* nilePc; River* const nileCp; const River* const nileCpc Disproving Heraclites ? Read pointer declarations from right to left.

27 Read pointer declarations right to left
// A const River const River nile; // A pointer to a const River const River* nilePc; // A const pointer to a River River* const nileCp; // A const pointer to a const River const River* const nileCpc

28 Lab Assignment Create a class called Point with two private data members, a public function Print, and constructors. Statically allocate an array of 10 Points, initialize them with non-zero data, and print them. Dynamically allocate an array of 10 Points, initialize them with non-zero data, and print them. Loop through the second array using pointer arithmetic and print them again (hint p+1 is a pointer to the second element on the array)


Download ppt "Pointers and References"

Similar presentations


Ads by Google