Pointers, Memory, Abstractions Pointers
ÿ A pointer is the a variable/value that is a memory address ÿ Using pointers presents some of the same problems as being Spiderman þ Addresses like 1, 2, 3, …, 0x0024ab03 þ With great power comes great responsibility… • Hexadecimal or base-16 digit represents 4 bits þ New, more ugly, and harder to detect bugs • Character is 8 bits, integer is 32 bits ÿ Why pointers? þ Every variable is stored somewhere in memory, typically we can ignore where þ Allow different sections of code to share data easily þ Enable linked data structures 0x00 0x08 0x0c 0x?? double x = 32.6; ÿ The BIG pointer concepts int y = 18; 32.6 18 "hello" þ Pointer declaration string s = "hello"; þ NULL (0) pointer þ The string variable s is actually the same size as int y þ BAD (uninitialized) pointers • Storage for the letters is elsewhere, string references it þ Memory allocation from heap þ Pointer dereference ÿ What about a permanent, but forwardable email address? þ Pointer assignment ÿ Other kinds of references?
A Computer Science Tapestry 12.1 A Computer Science Tapestry 12.2
Binky Code What's a pointer, why good, why bad?
void main() { ÿ Pointer is a memory address, it's an indirect reference to int *x; // Allocate the pointers x and y memory or an object. int *y; // but not the _pointee_ þ Rather than say we have an int, we say we have a pointer to an int þ Ifxisanint,xptrcanbeapointertox x = new int; • Same thing works with Date, Dice, Student, … • Not much use to have pointer to int, but pointer to class!! *x = 42; // Dereference x - store 42 in pointee *y = 13; // CRASH! ÿ Pointers force us to think about the machine and memory þ Knowledge is powerful, freedom from it liberating y = x; // Pointer assignment ÿ Pointers allow us to work at a lower level, but permit inheritance and a higher level of design/programming *y = 13; // Store 13 in its (shared) pointee þ Built-in array and tvector, C-style string and
A Computer Science Tapestry 12.3 A Computer Science Tapestry 12.4 Pointer example What's the point?
ÿ new, dereference, selector operator, copy semantics ÿ What's the difference between a vector of Dates and a vector of pointers to Dates? What about Courses, Students, etc.? CD c1("Beatles", "Rubber Soul", 1965); tvector
A Computer Science Tapestry 12.5 A Computer Science Tapestry 12.6
Thinking about pointersort.cpp Local memory is ephemeral…
ÿ The class Group uses a tvector
A Computer Science Tapestry 12.7 A Computer Science Tapestry 12.8 The trouble with pointers
ÿ Local vs. Heap allocation ÿ Another address of bug Dice * makeDie(int sides) Dice * makeDie(int sides) { { return new Dice(sides); Dice d(sides); } return &d; } þ What about the code below with different versions? Dice * cube = makeDie(4); cout << cube->NumSides() << endl; ÿ Pointer Advice þ Always initialize pointer variables, 0/NULL or new • 0/NULL means errors are reproduceable • Possible to assign another pointer value too þ Don't call new unless you want another object allocated
A Computer Science Tapestry 12.9