Topic#5 - CS162 - Topic #5 Lecture: Pointers and Dynamic...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS162 - Topic #5 Lecture: Pointers and Dynamic Memory What are pointers Why dynamically allocate memory How to dynamically allocate memory What about deallocation? Walk thru pointer exercises Programming Assignment Discussion Stage #2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS162 - Pointers In C++, a pointer is just a different kind of variable. This type of variable points to another variable or object (i.e., it is used to store the memory address of another variable nor an object). Such pointers must first be defined and then initialized. Then, they can be manipulated.
Background image of page 2
CS162 - Pointers A pointer variable is simply a new type of variable. Instead of holding an int, float, char, or some object's data. ...it holds an address. A pointer variable is assigned memory. the contents of the memory location is some address of another “variable”. Therefore, the value of a pointer is a memory location.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS162 - Pointers We can have pointers to (one or more) integers floating point types characters structures objects of a class Each represents a different type of pointer
Background image of page 4
CS162 - Pointers We define a pointer to an integer by: int * ptr; //same as int *ptr; Read this variable definition from right to left: ptr is a pointer (that is what the * means) to an integer. this means ptr can contain the address of some other integer
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS162 - Pointers At this point, you may be wondering why pointers are necessary. They are essential for allowing us to use data structures that grow and shrink as the program is running. after midterm time we will learn how to do this. ..with linked lists We are no longer stuck with a fixed size array throughout the lifetime of our program.
Background image of page 6
CS162 - Pointers But first, we will learn that pointers can be used to allow us to set the size of an array at run-time
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

Topic#5 - CS162 - Topic #5 Lecture: Pointers and Dynamic...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online