6.088.1 Introduction to C/C++

Add to Favourites
Post to:

C/C++ empowerment What is C? The C memory machine Logistics Goodbye The Adventures of Malloc and NewLecture 1: The Abstract Memory MachineEunsuk Kang and Jean YangMIT CSAIL January 19, 2010Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye C: outdated, old, antiquated. . . Photograph removed due to copyright restrictions. Please see http://www.psych.usyd.edu.au/pdp-11/Images/ken-den_s.jpeg. Figure: Dennis Ritche and Ken Thompson in 1972.Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye C: fast, faster, fastest Figure: Benchmark times from the Debian language shootout.Eunsuk Kang and Jean Yang The Adventures of Malloc and New C/C++ empowerment What is C? The C memory machine Logistics Goodbye Congratulations on choosing to spend your time wisely! Figure: XKCD knows that tools are important. Courtesy of xkcd.com. Original comic is available here: http://xkcd.com/519/Eunsuk Kang and Jean Yang The Adventures of Malloc and New C/C++ empowerment What is C? The C memory machine Logistics Goodbye Lecture plan 1. Course goals and prerequisites. 2. Administrative details (syllabus, homework, grading).3. High-level introduction to C. 4. C philosophy: “the abstract memory machine.” 5. How to get started with C. 6. Wrap-up and homework. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye 6.088: a language (rather than programming) course Images of Wonder Woman and circuit boards removed due to copyright restrictions. Course goal: to help proficient programmers understand how and when to use C and C++. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Background check Expected knowledge • Basic data structures (linked lists, binary search trees, etc.)? • Familiarity with basic imperative programming concepts.• Variables (scoping, global/local). • Loops. • Functions and function abstraction. Other knowledge • Functional programming? • Systems programming? • Hardware? • OOP with another language? Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Course syllabus Day Date1 1/19 2 1/20 3 1/21 4 1/22 5 1/23 6 1/24 Topic Meet C and memory management Memory management logistics More advanced memory management Meet C++ and OOP More advanced OOP Tricks of the trade, Q & A LecturerJeanJeanJeanEunsukEunsukEunsukEunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Administrivia Homework • Daily homework to be submitted via the Stellar site. • Graded �+, �,or �−. • Homework i will be due 11:59 PM the day after Lecture i; late submissions up to one day (with deductions). • Solutions will be released one day following the due date.Requirements for passing • Attend lectures–sign in at back. • Complete all 5 homework assignments with a � average.Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Recommended references Books Kernighan, Brian, and Dennis Ritchie. The C Programming Language. Upper Saddle River, NJ: Prentice Hall, 1988. ISBN: 9780131103627. Roberts, Eric. The Art and Science of C. Reading, MA: Addison-Wesley, 1994. ISBN: 9780201543223. Online resources http://www.cprogramming.comEunsuk Kang and Jean Yang The Adventures of Malloc and NewCover images of the following books removed due to copyright restrictions:C/C++ empowerment What is C? The C memory machine Logistics Goodbye The C family C • Developed in 1972 by Dennis Ritchie at Bell Labs.• Imperative systems language. C++ • Developed in 1979 by Bjarne Stroustrup at Bell Labs. • Imperative, object-oriented language with generics.C� (outside scope of course) • Multi-paradigm language with support for imperative, function, generic, and OO programming and memory management. • Developed at Microsoft, release circa 2001. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Vocabulary check • Imperative, declarative, functional• Compiled, interpreted • Static, dynamic • Memory-managed Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Typically, C is. . . • Compiled. • Imperative. • Manually memory-managed. • Used when at least one of the following matters: • Speed. • Memory. • Low-level features (moving the stack pointer, etc.). Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Thinking about C in terms of memory. . . Figure: Women operating the ENIAC. Eunsuk Kang and Jean Yang The Adventures of Malloc and New C/C++ empowerment What is C? The C memory machine Logistics Goodbye Layers of abstraction over memory Level of abstraction Languages Directly manipulate memory Assembly (x86, MIPS) Access to memory C, C++ Memory managed Java, C, Scheme/Lisp, ML Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye It’s a memory world Figure: Processors read from memory, do things, and write to memory.Eunsuk Kang and Jean Yang The Adventures of Malloc and New Diagram of how processor operations interact with memory.Figure by MIT OpenCourseWare.C/C++ empowerment What is C? The C memory machine Logistics Goodbye C access to memory: the heap The heap is a chunk of memory for the C program to use. • Can think of it as a giant array. • Access heap using special pointer syntax. • The whole program has access to the heapa . Addr. Contents . . . . . . 0xbee 0xbeef 0xbf4 0xfeed . . . . . . aDepending on what the operating system allows Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Manual memory management Goals • Want to allow the program to be able to designate chunks of memory as currently in use. • Want to be able to re-designate a piece of memory as “freed” when the program is done with it. C support Standard library (stlib.h) has malloc and free functions. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye The other C memory: the stack C functions get allocated on the stack. • Functions are “pushed on” to the stack when called. • Functions are “popped off” the stack when they return.• Functions can access any memory below the current top of the stack. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Memory layout: process context Eunsuk Kang and Jean Yang The Adventures of Malloc and NewStack diagram.Figure by MIT OpenCourseWare.C/C++ empowerment What is C? The C memory machine Logistics Goodbye Getting started with C Photograph removed due to copyright restrictions. Please see http://www-03.ibm.com/ibm/history/exhibits/vintage/vintage_4506VV4002.html.Figure: IBM 29 card punch, introduced late 1964.Eunsuk Kang and Jean Yang The Adventures of Malloc and New C/C++ empowerment What is C? The C memory machine Logistics Goodbye Using C 1. Obtain a C compiler (GCC recommended–more instructionson site for downloading GCC or using it on MIT servers.)2. Write a simple C program. #include /∗ Headers to include . ∗/int main() {printf(”Hello world!”);} 3. Compile: gcc -o run hello hello.c4. Run: ./run hello Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Functions void print sum( int arg1 , int arg2) {int sum = arg1 + arg2; /∗ Printf is a special function taking variablenumber of arguments . ∗/printf(”The sum is %d\n”, sum);/∗ The return is optional . ∗/return ; } /∗ Each executable needs to have a main function with type int . ∗/int main() {print sum(3, 4); return 0; } Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Local and global variables int x; int y, x= 1; z; /∗ Functions void foo() {int x; x= 2; } can have local variables . ∗//∗ Arguments void bar(int x= 3; } are locally x) { scoped . ∗/Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Conditionals int foo( int x) {/∗ C has the usual boolean operators . ∗/if (3 ==x) {return 0; }} int /∗ bar () Note {that conditions are integer type , where 1 is true ! ∗/if (1) {return 0; } } Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Loops For loopsvoid foo() {int i; for (i=1; i < 10; ++i ) {printf (”%d\n”, i); }} While loops void bar() {int lcv =0; while (lcv < 10) {printf (”%d\n”, lcv); + lcv ; }} Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye When can we call what? Each function needs to be declared (but not necessarily defined) before we call it. /∗ Declaration . ∗/void print sum(int , int ); /∗ Each executable needs to have a main function with type int . ∗/int main() {print sum(3, 4); return 0; } /∗ Definition . ∗/void print sum( int arg1 , int arg2) {/∗ Body defined here . ∗/} Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Including headers Header definitions allow us to use things defined elsewhere. • Header files (.h files) typically contain declarations (variables, types, functions). Declarations tell the compiler “these functions are defined somewhere.” • Function definitions typically go in .c files. • Angle brackets indicate library header files; quotes indicate local header files. #include /∗ Library file . ∗/#include ”mylib .h” /∗ Local file . ∗/• The compiler’s -I flag indicates where to look for library files (gcc -I [libdir] -o [output] [file]). Eunsuk Kang and Jean Yang The Adventures of Malloc and NewC/C++ empowerment What is C? The C memory machine Logistics Goodbye Until tomorrow. . . Homework (due tomorrow) • Get a C compiler up and running. • Compile and run “Hello world.” Make a small extension to print the system time. • Play around with gdb and valgrind. • More details on the course website. Questions? • The course staff will be available after class. Eunsuk Kang and Jean Yang The Adventures of Malloc and NewMIT OpenCourseWarehttp://ocw.mit.edu 6.088 Introduction to C Memory Management and C++ Object-Oriented ProgrammingJanuary IAP 2010For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Description
This cource helps the proficient programmers to understand how and when to use C and C++. This lecture notes covers the following topics.

C and memory management,Memory management logistics,More advanced memory management,Meet C++ and OOP and More advanced OOP

“Eunsuk Kang & Jean Yang, 6.088 .1 Introduction to C/C++ , 6.088 Introduction to C Memory Management and C++ Object-Oriented Programming, Electrical Engineering and Computer Science, Engineering, Massachusetts Institute of Technology: MIT Open Course Ware,http://ocw.mit.edu (21-08-2011).License: Creative Commons BY-NC-SA: http://ocw.mit.edu/terms/#cc".

Comments

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
LearnOnline Through OCW
OpenCourseWare
User
102 Followers

Your Facebook Friends on WizIQ

Explore Similar Courses

Program in C++

Price:$149

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect