ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development...

27
ITEC 320 Lecture 26 C++ Introduction

Transcript of ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development...

Page 1: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

ITEC 320

Lecture 26C++ Introduction

Page 2: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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…

Page 3: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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++….

Page 4: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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)

Page 5: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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!

Page 6: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

Pictures

Simula 67Beautiful yet impractical C

Ridiculously fast, yet difficult to work with

C++=

+

Page 7: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

Java

• Some parts are C-like• Memory management?• Designed not to fail

You Computer

Page 8: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 9: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 10: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

Producing programs

File.java File.classjavac

javaJVM

conversionActual machine code

file.cpp file

g++ –o file file.c

Machine code that executes

Page 11: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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…

Page 12: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

Memory

Java C++

Page 13: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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)

Page 14: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 15: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 16: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 17: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 18: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 19: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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;

Page 20: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 21: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 22: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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();

Page 23: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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

Page 24: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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)

Page 25: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

NULL

• Universal

• Doesn’t autoset to NULL, takes more work…

int* a;a = new int;delete a;if (a != NULL)

cout << *a << endl;

Page 26: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

Introduction

Protected memory

• Whenever you steal something and are caught…

• Whenever you access memory that doesn’t belong to you

Page 27: ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between 100-500kloc.

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