Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Similar documents
Computational Methods of Scientific Programming Fall 2007

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

6.096 Introduction to C++ January (IAP) 2009

Kurt Schmidt. October 30, 2018

High Performance Programming Programming in C part 1

Common Misunderstandings from Exam 1 Material

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

COMP 2355 Introduction to Systems Programming

Object Oriented Programming. Solved MCQs - Part 2

Comp151. Construction & Initialization

Dynamic memory allocation

CS 376b Computer Vision

The University of Nottingham

Dynamic arrays / C Strings

Arrays and Pointers. CSE 2031 Fall November 11, 2013

An Introduction to C++

Structures and Pointers

September 10,

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Advanced Systems Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CSE 333 Lecture smart pointers

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

A brief introduction to C++

C++ for Java Programmers

Object-Oriented Programming, Iouliia Skliarova

CS201- Introduction to Programming Current Quizzes

C Programming Basics II

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Fast Introduction to Object Oriented Programming and C++

Arrays, Pointers and Memory Management

Assumptions. History

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CSE 333 Lecture 2 Memory

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CSC 1600 Memory Layout for Unix Processes"

CSE 124 Discussion (10/3) C/C++ Basics

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Lecture 15a Persistent Memory & Shared Pointers

Dynamic Data Structures. CSCI 112: Programming in C

AN OVERVIEW OF C++ 1

C for C++ Programmers

G52CPP C++ Programming Lecture 9

C++ Important Questions with Answers

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Dynamic memory allocation (malloc)

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

EL2310 Scientific Programming

Memory Organization. The machine code and data associated with it are in the code segment

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Final CSE 131B Spring 2005

Memory Management I. two kinds of memory: stack and heap

Supporting Class / C++ Lecture Notes

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

COMP 2355 Introduction to Systems Programming

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

PRINCIPLES OF OPERATING SYSTEMS

Final CSE 131B Spring 2004

A Tour of the C++ Programming Language

Call The Project Dynamic-Memory

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Evolution of Programming Languages

Intermediate Programming, Spring 2017*

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSE 333 Lecture 4 - malloc, free, struct, typedef

CSCI 2212: Intermediate Programming / C Storage Class and Dynamic Allocation

COSC Software Engineering. Lecture 16: Managing Memory Managers

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Arrays and Pointers (part 1)

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

C++_ MARKS 40 MIN

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

1d: tests knowing about bitwise fields and union/struct differences.

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

ECE264 Fall 2013 Exam 3, November 20, 2013

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

CS11 Intro C++ Spring 2018 Lecture 1

o Code, executable, and process o Main memory vs. virtual memory

Transcription:

12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring Chris Hill

Summary Today we finish up C and start C++ Final C topics Structures: A method for grouping like variables together Memory management Start of C++ History Ideas of classes and objects Examples to demonstrate concepts 10/11/2011 12.010 Lec 10 2

Structures and Types Struct alone is still unclear - typedef typedef struct { double cx; double cy; double cz; t_point; main() { t_point point; point.cx = 3.; point.cy=3.; point.cz=2.; plot(point); 10/11/2011 12.010 Lec 10 3

Structures and Types Derived types just like basic types e.g. can use arrays typedef struct { double cx; double cy; double cz; t_point; main() { t_point point[10]; int i; for (i=0;i<10;++i) { point[i].cx = 3.; point[i].cy=3.; point[i].cz=(double)i; for (i=0;i<10;++i) { plot(point[i]); 10/11/2011 12.010 Lec 10 4

Memory Management Application code creates variables and arrays at runtime <stdlib.h> - malloc, calloc, free, realloc + sizeof e.g main(int argc, char *argv[]) { double *foo; int nel; int i; /* Create an array of size nel at runtime */ sscanf(argv[1], %d\n,&nel); foo = (double *) calloc(nel,sizeof(*foo)); if ( foo == NULL ) exit(-1); for (i=0;i<nel;++i) { foo[i]=i; free(foo); 10/11/2011 12.010 Lec 10 5

Remember - *, & short a; short *ptr_to_a; a = 1; ptr_to_a = &a; *ptr_to_a = 1; a 0001 0001 0002 0003 0x00 0xFF &a Here compiler allocated memory for you foo = (double *) calloc(3,sizeof(*foo)); Here application allocates memory explicitly. Allows more control but requires careful bookkeeping. 10/11/2011 12.010 Lec 10 6

Towards C++ C essentials syntax v. fortran call by reference v. call by value pointers structure, typedef memory management C is also the basis for C++ 10/11/2011 12.010 Lec 10 7

C++ Object Oriented - Allows you to build/compose v. complex applications from building blocks Appeared around 1984 (Bjarne Stroustrup, Bell Labs) ANSI standard 1997 Syntax is like C. Getting started: a few extra keywords + few new formalized concepts. Book C++ The Core Language O Reilly Successful because you can compose applications from other peoples building blocks. Windows etc. V. complex in detail, like Mathemetica takes many years to learn everything!! 10/11/2011 12.010 Lec 10 8

C++ concept C language + classes Class is a formal way to think about good program design. Modularity, encapsulation, hierarchy, abstraction A class has Methods ( program logic) Data ( variables ) can be private or public Example string Methods: set, get Data: string text, string length 10/11/2011 12.010 Lec 10 9

C++ Basic Example main() { String s; printf("executable code starting\n"); s.set("hello"); printf("%s\n",s.get()); printf("executable code ending\n"); Compile using g++ Will write out hello + some other stuff 10/11/2011 12.010 Lec 10 10

C++ Basic Example main() { String s; Class Instance of the Class printf("executable code starting\n"); s.set("hello"); Methods printf("%s\n",s.get()); printf("executable code ending\n"); 10/11/2011 12.010 Lec 10 11

String Class - Behind the Scenes /* ===== Class interface definition ===== */ class String { public: String(); /* Constructor */ ~String(); /* Destructor */ void set(char *s); /* Set a string */ char *get(); /* Get string value */ private: char *str; /* Pointer to the string */ int lngth; /* Length of the string */ ; 10/11/2011 12.010 Lec 10 12

String Class Example Methods /* Set str to point to a private copy of s */ void String::set(char *s) { lngth = strlen(s); str = new char[lngth+1]; strcpy(str, s); /* Return the pointer to the string */ char *String::get() { return str; 10/11/2011 12.010 Lec 10 13

String Class Example Methods /* Constructor */ String::String() { str = 0; set(""); printf("i created a string\n"); /* Destructor */ String::~String() { delete[] str; printf("i deleted a string\n"); 10/11/2011 12.010 Lec 10 14

z Application Example Throwing a ball in the air w = w z = z 0 0 + t + ò wdt 0 gt Get initial velocity and length of experiment. Calculate time evolution of w and z. Print out trajectory z 0 w 0 10/11/2011 12.010 Lec 10 15

C Procedural Form main ( ) { float t=10.; float w0=10.; t_gball *theball;/* Stats for the ball */ /* Allocate space for full ball time history */ createball(w0, &theball ); /* Step forward the ball state */ stepforwardstate( t, &theball ); /* Write table of output */ printtrajectory( t, w0, theball); 10/11/2011 12.010 Lec 10 16

C++ Using Ball Class main() {float w0 = 10.; float t=10.; Ball b; b.initialize(w0); b.simulate(t); b.printtrajectory(); All info. is held in b. Fewer args, cleaner abstraction. 10/11/2011 12.010 Lec 10 17

Summary Finished up C with structures and memory management Started with C++ C++ is C with the addition of classes Class is a formal way to think about good program design. Modularity, encapsulation, hierarchy, abstraction A class has Methods ( program logic) Data ( variables ) can be private or public 10/11/2011 12.010 Lec 10 18

MIT OpenCourseWare http://ocw.mit.edu 12.010 Computational Methods of Scientific Programming Fall 2011 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.