Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Computer science - Programming Abstractions, Lecture 12, Lecture notes of Programming Abstractions

Slides for the couse of Programming Abstractions for Computer Science. More pointers, recursive data, linked lists

Typology: Lecture notes

2010/2011

Uploaded on 10/01/2011

hollyb
hollyb 🇺🇸

4.8

(38)

204 documents

1 / 3

Related documents


Partial preview of the text

Download Introduction to Computer science - Programming Abstractions, Lecture 12 and more Lecture notes Programming Abstractions in PDF only on Docsity! Admin Today’s topics • More pointers, recursive data, linked lists Reading • linked lists Ch 9.5(sort of), handout #21 • algorithms, big O Ch 7 (next) Assign 3 due, Assign 4 out • Joy poll? • Boggle awesomeness • Paper copy delinquency Lecture #12 Simple pointer operations int main() { int num; int *p, *q; p = new int; *p = 10; q = new int; *q = *p; q = p; delete p; delete q; // bad idea, q already deleted! q = NULL; // NULL is zero pointer, used as sentinel value Pointer basics Pointers are distinguished by type of pointee • Type double* not same as int* Pointers are uninitialized until assigned • Dereferencing a uninitialized pointer is bad news Dynamic allocation via new • Operator new allocates memory from heap, returns address Manual deallocation via delete • Forgetting to delete means memory is orphaned • Accessing deleted memory has unpredictable consequences Pointers and dynamic arrays int main() { int *arr = new int[10]; for (int i = 0; i < 10; i++) arr[i] = i; delete[] arr; // delete[] if allocated with new[] Raw arrays can be trouble • Manually allocated and deallocated • Don't know their length • No bounds-checking • Cannot easily change size once allocated • Allocate new space, copy over, update pointer Vector uses array behind scenes, but hides issues Use of pointers Axess database A course has pointers to enrolled students • Allocate studentT record in heap for new student • Each course student enrolls in stores pointer to record • Saves space by not repeating student information • If student gets new phone number, change in one place only! struct studentT { string first, last; string address, phone; }; struct courseT { string dept, name; Vector<studentT *> students; }; Recursive data Recursion applied to data • Self-referential, self-similar • Within itself, data has smaller version repeated Examples • Matroshka dolls • Nesting boxes • Onions • Structure containing pointer to same structure A recursive struct struct Entry { string name, address, phone; Entry *next; }; Each entry points to another Entry! Wired together, you get a linked list! name address phone next Rein 123 Pine 555-0123 Kalev 204 Elm 555-8765 Mati 89 Oak 555-9425 Creating a node Entry *GetNewEntry() { cout << "Enter name (ENTER to quit):"; string name = GetLine(); if (name == "") return NULL; Entry *newOne = new Entry; newOne->name = name; cout << "Enter address: "; newOne->address = GetLine(); cout << "Enter phone: "; newOne->phone = GetLine(); newOne->next = NULL; // no one follows return newOne; }
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved