Lecture 18 - Managing Memory

Lecture 18 - Managing Memory - ManagingMemory...

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

View Full Document Right Arrow Icon
Managing Memory (and low level Data Structures) Lectures 24, 25 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜ hkaiser /fall_2011/csc1254.html  
Background image of page 1

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

View Full DocumentRight Arrow Icon
Programming Principle of the Day Avoid Premature Optimization Don’t even think about optimization unless your code is  working, but slower than you want. Only then should you start  thinking about optimizing, and then only wi th the aid of  empirical data.  "We should forget about small efficiencies, say about 97%  of the time: premature optimization is the root of all evil"  - Donald Knuth.  http://en.wikipedia.org/wiki/Program_optimization 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 2
Background image of page 2
Low Level Data Structures We were using the standard library data  structures – containers How are these built? Low level language facilities and data structures Closer to computer hardware (in semantics and  abstraction level) Why do we need to know how are these built? Useful techniques, applicable in other contexts More dangerous, require solid understanding 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Pointers and Arrays Array is a kind of container, however it’s less  powerful and more dangerous Pointers are kind of random access iterators for  accessing elements of arrays Pointers and Arrays are the most primitive data  structures available in C/C++ Closely connected concepts, inseparable in real  world usage 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 4
Background image of page 4
Pointers A pointer is a value representing the  address  of  an object Every distinct object has a distinct address  denoting the place in memory it lives If it’s possible to access an object it’s possible to  retrieve its address For instance: x // if ‘x’ is an object &x // then ‘&x’ is the address of this 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Pointers The ‘&’ is the address-of operator Distinct from defining reference types! The ‘*’ is the dereference operator Same as for any other iterator as well If ‘p’ contains the address of ‘x’ we say that ‘ the  pointer  p  points to  x’ 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 6 p x
Background image of page 6
Pointers Initialize to zero means ‘point to no object’ Null pointer (special value, as no object has this  address) Pointers have types! The address of an object of type T is ‘ pointer to T Written as: T* For instance: int x; // object of type int 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Pointers A small (but full) example: int main() { int x = 5; // p points to x int * p = &x; cout << "x = " << x << endl; // change the value of x through p *p = 6; 11/22/ 2011,  Lecture s 24,  25 CSC 1254,  Fall 2011,  Managing  Memory 8 p x: 5 p x: 6
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/29/2011 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 35

Lecture 18 - Managing Memory - ManagingMemory...

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

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