Upload
muriel-park
View
213
Download
0
Tags:
Embed Size (px)
Citation preview
ITEC 320
Lecture 26C++ Introduction
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• Point: I’m not just repeating what I
read 15 minutes ago to you…
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++….
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)
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!
Introduction
Pictures
Simula 67Beautiful yet impractical C
Ridiculously fast, yet difficult to work with
C++=
+
Introduction
Java
• Some parts are C-like• Memory management?• Designed not to fail
You Computer
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
Introduction
Hello World
#include <iostream>#include <string>using namespace std;
int main(int argc, char** argv){ string hello = "Hello World"; cout << hello << endl; return 0;}
Command Prompt:vi hello.cppg++ -o Hello hello.cpp./Hello
System libraries
Standard namespace (functions?)
Function definition, entry point for everyC/C++ program (command line args)String class and initializationPrint to consoleReturn code of program to OS
Returns 0, can be used with scripts
Introduction
Producing programs
File.java File.classjavac
javaJVM
conversionActual machine code
file.cpp file
g++ –o file file.c
Machine code that executes
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…
Introduction
Memory
Java C++
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)
Introduction
Data table
Type Size (in bytes)
int 4
double 8
char 1
float 4
boolean 1
int a;
a=4;
OS give me 4 bytes 4 bytes of memory atlocation -------
Translateto binary OS store bits
at location----Bits stored in memory
Introduction
Bits
• What is the purpose of memory?
0 or 1
0 1 2 3 4 5 6 7
One byte of memoryA = 65B = 66C = 67
Base two versus base 10
22 + 21 + 20
1 0 1
5
Binary intchar
float
Ascii table is 127 characters
1111111 0000001
String
Introduction
Heap versus stack
• Heap = free memory• Stack = where programs are
Heap
mainfirst();
void first(){
int a;int b;
}int main(){
int c;}
ca b
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 ofcleaning it up
C++ requires you manually clean up
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
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;
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/cof access C++ gives us
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 stack4 bytes of memory on stack
C++ caveat:There is no length function like in javaHave to keep track of yourself
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();
Introduction
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 everywhereHowever, when you send a parameter to a function it copies the var
Introduction
Issues with pointers
Function a-Creates memory for an array of 10
Function bSets values of array
Function cFrees memory
Function dPrints out values
Dangling pointerNo way to know if it is good or not (NULL)
Introduction
NULL
• Universal
• Doesn’t autoset to NULL, takes more work…
int* a;a = new int;delete a;if (a != NULL)
cout << *a << endl;
Introduction
Protected memory
• Whenever you steal something and are caught…
• Whenever you access memory that doesn’t belong to you
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 2Buffer
What you wrote