CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases,

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 Review structures  Program to demonstrate a structure containing a pointer.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Pointers OVERVIEW.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Variables and memory addresses
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture 20-something CIS 208 Wednesday, April 27 th, 2005.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Code: BCA302 Data Structures with C Prof.(Dr.) Monalisa Banerjee By.
Pointers What is the data type of pointer variables?
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
Introduction to Programming
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
Student Book An Introduction
INC 161 , CPE 100 Computer Programming
C Basics.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Data Structures and Database Applications Abstract Data Types
Programming fundamentals 2 Chapter 2:Pointer
Chapter 16-2 Linked Structures
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
نوع داده هاي انتزاعي Abstract Data Types
Computer Skills2 for Scientific Colleges
Pointers Lecture 2 Tue, Jan 24, 2006.
5.1 Introduction Pointers Powerful, but difficult to master
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
C++ Pointers and Strings
Variables, Pointers, and Arrays
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
Chapter 9: Pointers and String
Pointers and dynamic objects
CSCE 206 Lab Structured Programming in C
Doubly Linked List Implementation
C++ Pointers and Strings
Programming fundamentals 2 Chapter 3:Pointer
Abstract Data Types Stacks CSCI 240
Lecture 4 – Data collection List ADT
Presentation transcript:

CISC220 Spring 2010 James Atlas Lecture 04: Pointers, Functions, Memory Management, ADTs, Classes, Hardware, Software, Graphics, Networking, AI, Databases, Scientific Computing, Parallel Architectures

Question 4 What is output from the following code: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

Question 5 Change the code to work correctly using references: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(void) { int a = 0; int b = 5; swap(a,b); cout << a << endl; }

Question 6 What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = &blocks[0]; char temp; /*1*/ temp = blocks[0]; /*2*/ temp = *(blocks + 2); /*3*/ temp = *(ptr + 1); ptr = blocks + 1; /*4*/ temp = *ptr; /*5*/ temp = *(ptr + 1);

Question 7 What is the value of temp after each assignment? char blocks[3] = {'A','B','C'}; char *ptr = blocks; char temp; /*1*/ temp = *++ptr; /*2*/ temp = ++*ptr; /*3*/ temp = *ptr++; /*4*/ temp = *ptr;

Question 8 Write code to reverse a string using only pointer arithmetic (no array accessors): int main(void) { char s[10] = "abcde"; for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } reverseString(s); for (int i = 0; s[i] != NULL; i++) { cout << s[i]; } cout << endl; return 0; }

Question 9 What is output from the following code ? int *x, *y, *z; x = new int[20]; x[10] = 0; y = x; x[10] = 5; cout << x[10] << ", " << y[10] << endl; x[10] = 15; z = new int[10]; for (int i = 0; i < 10; i++) z[i] = x[i]; z[10] = 25; cout << x[10] << ", " << y[10] << ", " << z[10] << endl;

Structs Combined data struct Fraction { int numerator; int denominator; }; Fraction **fractions; // What is this?

Classes Reading - K+W Chap ,

Abstract Data Types (ADTs) Combination of data and operations Encapsulates implementation details Provides an interface for usage

ADT Example A mathematical set of integers, S Four operations –add(x) where x is an integer –remove(x) removes integer x from the set –contains(x) returns true if the set contains x –size() returns the number of elements in the set No implementation details! –The actual set could be implemented using an array, a linked list, a hashtable, or even a web blog (although posting/parsing a web blog for a simple integer would be very innefficient!)

C++ Classes One way to represent ADTs Header file specifies the interface Class file encapsulates the implementation