Lecture 18 - Managing Memory

Lecture 18 - Managing Memory - Managing Memory (and low...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Managing Memory (and low level Data Structures) Lectures 24, 25 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/hkaiser/fall_2011/csc1254.html Programming Principle of the Day Avoid Premature Optimization Dont 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, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 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 Sometimes absolute performance matters 11/22/2011, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 3 Pointers and Arrays Array is a kind of container, however its 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, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 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 its possible to access an object its possible to retrieve its address For instance: x // if x is an object &x // then &x is the address of this object p // if p is the address of an object *p // then *p is the object itself 11/22/2011, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 5 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 Pointers are built-in data types which need to be initialized in order to be meaningful 11/22/2011, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 6 p x 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 int *p; // pointer to an int, *p has type int int * p; // pointer to an int, p has type int* 11/22/2011, Lectures 24, 25 CSC 1254, Fall 2011, Managing Memory 7 Pointers A small (but full) example: int main() {...
View Full Document

Page1 / 35

Lecture 18 - Managing Memory - Managing Memory (and low...

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