1 / 27

ITEC 320

ITEC 320. Lecture 26 C + + Introduction. Qualifications. Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc of C++ (GOW was 250kloc) Managed teams of programmers Developed API used by dozens of people, applications used by thousands

chaeli
Download Presentation

ITEC 320

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 320 Lecture 26 C++ Introduction

  2. Qualifications • Ten years of C++ development (albeit not fulltime) • Written somewhere between 100-500kloc of C++ (GOW was 250kloc) • Managed teams of programmers • Developed API used by dozens of people, applications used by thousands • Point: I’m not just repeating what I read 15 minutes ago to you…

  3. C++ • History of C++… • “C++ was designed to provide Simula’s facilities for program organization together with C’s efficiency and flexibility for systems programming. “ • Write coding in assembly, C, C++….

  4. OO • Simula 67 • 1965 (Vietnam, civil rights, Thunderbirds debuted) • Based off of algol… • Introduced basics of OO programming that we use today • Classes, inheritance, class variables, instance variables, class methods (virtual)

  5. C • Multiple people using a computer at the same time… • Broken project called Multics…. • Need was still there, it became UNIX • Needed a language for the new OS… • CPL,BCPL,B, then C!

  6. Pictures C++ = + Simula 67 Beautiful yet impractical C Ridiculously fast, yet difficult to work with

  7. Java • Some parts are C-like • Memory management? • Designed not to fail You Computer

  8. C++ • Complete control • Assembly to OO • Memory management • Speed • Used widely Did you check if all seven pointers were non-null or only six? Do you feel lucky, well do ya? “If programming in Pascal is like being put in a straightjacket, then programming in C is like playing with knives, and programming in C++ is like juggling chainsaws.” – Anonymous

  9. Hello World #include <iostream> #include <string> using namespace std; intmain(intargc, char** argv) { string hello = "Hello World"; cout << hello << endl; return 0; } System libraries Standard namespace (functions?) Function definition, entry point for every C/C++ program (command line args) String class and initialization Print to console Return code of program to OS Command Prompt: vi hello.cpp g++ -o Hello hello.cpp ./Hello Returns 0, can be used with scripts

  10. Producing programs File.java File.class JVM Actual machine code javac java conversion file.cpp Machine code that executes file g++ –o file file.c

  11. Tools • g++ - Free C++ compiler • VI / Emacs / Crimson editor (or others) • putty and rucs / your own linux machine • C++ should be portable between windows and linux…it usually isn’t • Develop on platform you use to demonstrate…

  12. Memory Java C++

  13. Hardware • 1 gigabyte =1024 megabytes • 1 megabyte = 1024 KB • 1 KB = 1024 bytes • 1 byte = 8 bits • Bit is a 0 or a 1 Byte (maybe) Address

  14. Data table int a; a=4; 4 bytes of memory at location ------- OS give me 4 bytes Translate to binary OS store bits at location---- Bits stored in memory

  15. Bits • What is the purpose of memory? 0 or 1 Base two versus base 10 0 1 2 3 4 5 7 6 A = 65 B = 66 C = 67 String One byte of memory Ascii table is 127 characters Binary int char 1 0 1 5 float 22 + 21 + 20 1111111 0000001

  16. Heap versus stack • Heap = free memory • Stack = where programs are void first() { int a; intb; } int main() { intc; } Heap a b first(); c main

  17. Using memory Java C++ Stack int first; int* a; a = new int; delete a; a = (int*)malloc(sizeof(int)); free(a); int a; String bob = new String(); bob = “Jones”; Stack Heap Heap Java magically takes care of cleaning it up C++ requires you manually clean up

  18. Stack versus heap • Stack should be as small as possible • Function calls go on stack • Include local variables • Less that has to go on stack, better • Parameter passing • Copying memory == bad

  19. C++ • Pointers • Contains a memory address, not data • Can allow strong or weak typing • int* • void* • Can point to N items of that particular type • int* a = new int[10]; • delete [] a;

  20. More on arrays • Initializing arrays Java: int[] array = new int[10]; for (inti=0; i<array.length; i++) { array[i]=0; } C++: int* array = new int[10]; memset(array,sizeof(int)*10,0); Possible b/c of access C++ gives us

  21. Using pointers • Creation • Dereference • Arrays int* a; a = new int; *a = 4; Put 4 into the address stored in A int array1[10]; int* array2 = new int[10]; array1[0] =4; array2[0]=4; 40 bytes of memory on stack 4 bytes of memory on stack C++ caveat: There is no length function like in java Have to keep track of yourself

  22. Classes / Pointers class A { public: A(); void print(); private: intb; }; A::A() { b=4; } void A::print() { cout << b << endl; } A* example = new A(); example->print(); A object; object.print();

  23. References • Local variable, points to memory address int* a = new int; int& b = a; *a=4; b=6; cout << a << “ “ << b <<endl; Java uses references everywhere However, when you send a parameter to a function it copies the var

  24. Issues with pointers Function a -Creates memory for an array of 10 Function b Sets values of array Function c Frees memory Function d Prints out values Dangling pointer No way to know if it is good or not (NULL)

  25. NULL • Universal • Doesn’t autoset to NULL, takes more work… int* a; a = new int; delete a; if (a != NULL) cout << *a << endl;

  26. Protected memory • Whenever you steal something and are caught… • Whenever you access memory that doesn’t belong to you

  27. Buffer overflows Buffer instruction 1, instruction 2 • Array of size 50 • Get value from web form • Write into array • You forgot to check the size • Overwrite memory in your program • Can causes new code • Or cause program to die What you wrote

More Related