Topic#6 - CS162 Topic#6 Lecture Pointers and Dynamic Memory...

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

View Full Document Right Arrow Icon
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory Review Dynamically allocating structures Combining the notion of classes and pointers Destructors Programming Assignment Discussion Questions?
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 - Review of Pointers What is a pointer? How would you define a pointer variable, that can point to a float? Would this change if you wanted the pointer to reference an array of floats? Show how to dynamically allocate an array of 20 floats Show two ways of accessing element 19
Background image of page 2
CS162 - Review of Pointers What operator allocates memory dynamically? What does it really mean to allocate memory? Does it have a name? Why is it important to subsequently deallocate that memory? What operator deallocates memory?
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 - Dynamic Structures Let’s take these notions and apply them to dynamically allocated structures What if we had a video structure, how could the client allocate a video dynamically? video *ptr = new video; Then, how would we access the title? *ptr.title ? Nope! WRONG
Background image of page 4
CS162 - Dynamic Structures To access a member of a struct, we need to realize that there is a “precedence” problem. Both the dereference (*) and the member access operator (.) have the same operator precedence. ...and they associate from right to left So, parens are required: (*ptr).title Correct (but ugly)
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 - Dynamic Structures A short cut (luckily) cleans this up: (*ptr).title Correct (but ugly) Can be replaced by using the indirect member access operator (->) . .. it is the dash followed by the greater than sign: ptr->title Great!
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 24

Topic#6 - CS162 Topic#6 Lecture Pointers and Dynamic Memory...

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

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