Download presentation
Presentation is loading. Please wait.
Published byMuriel Park Modified over 9 years ago
1
ITEC 320 Lecture 26 C++ Introduction
2
Introduction Qualification s 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
Introduction 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
Introduction 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
Introduction 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
Introduction Pictures Simula 67 Beautiful yet impractical C Ridiculously fast, yet difficult to work with C++ = +
7
Introduction Java Some parts are C-like Memory management? Designed not to fail YouComputer
8
Introduction 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
Introduction Hello World #include using namespace std; int main(int argc, char** argv) { string hello = "Hello World"; cout << hello << endl; return 0; } Command Prompt: vi hello.cpp g++ -o Hello hello.cpp./Hello 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 Returns 0, can be used with scripts
10
Introduction Producing programs File.java File.class javac java JVM conversion Actual machine code file.cpp file g++ –o file file.c Machine code that executes
11
Introduction 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
Introduction Memory JavaC++
13
Introduction Hardware 1 gigabyte =1024 megabytes 1 megabyte = 1024 KB 1 KB = 1024 bytes 1 byte = 8 bits Bit is a 0 or a 1 Address Byte (maybe)
14
Introduction Data table TypeSize (in bytes) int4 double8 char1 float4 boolean1 int a; a=4; OS give me 4 bytes 4 bytes of memory at location ------- Translate to binary OS store bits at location---- Bits stored in memory
15
Introduction Bits What is the purpose of memory? 0 or 1 012345 6 7 One byte of memory A = 65 B = 66 C = 67 Base two versus base 10 2 2 + 2 1 + 2 0 1 0 1 5 Binaryint char float Ascii table is 127 characters 1111111 0000001 String
16
Introduction Heap versus stack Heap = free memory Stack = where programs are Heap main first(); void first() { int a; int b; } int main() { int c; } c a b
17
Introduction Using memory Java int a; String bob = new String(); bob = “Jones”; C++ int first; int* a; a = new int; delete a; a = (int*)malloc(sizeof(int)); free(a); Stack Heap Stack Heap Java magically takes care of cleaning it up C++ requires you manually clean up
18
Introduction 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
Introduction 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
Introduction More on arrays Initializing arrays Java: int[] array = new int[10]; for (int i=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
Introduction 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
Introduction Classes / Pointers class A { public: A(); void print(); private: int b; }; A::A() { b=4; } void A::print() { cout << b << endl; } A* example = new A(); example->print(); A object; object.print();
23
Introduction Reference s 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
Introduction 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
Introduction NULL Universal Doesn’t autoset to NULL, takes more work… int* a; a = new int; delete a; if (a != NULL) cout << *a << endl;
26
Introduction Protected memory Whenever you steal something and are caught… Whenever you access memory that doesn’t belong to you
27
Introduction Buffer overflows 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 instruction 1, instruction 2 Buffer What you wrote
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.