Download int - Radford University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

ALGOL 68 wikipedia , lookup

Supercomputer architecture wikipedia , lookup

Java performance wikipedia , lookup

Subroutine wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Transcript
ITEC 320
Lecture 26
C++ 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…
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
C++
=
+
Simula 67
Beautiful yet impractical
Introduction
C
Ridiculously fast, yet difficult to
work with
Java
• Some parts are C-like
• Memory management?
• Designed not to fail
You
Introduction
Computer
C++
• Complete control
– Assembly to OO
• Memory management Did you check if all seven pointers were
non-null or only six?
• Speed
Do you feel lucky, well do ya?
• Used widely
Introduction
“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
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.cpp
g++ -o Hello hello.cpp
./Hello
Introduction
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
Producing
programs
File.java
javac
file.cpp
g++ –o file file.c
Introduction
File.class
JVM
java
file
Actual machine code
conversion
Machine code that executes
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
Introduction
C++
Hardware
•
•
•
•
•
1 gigabyte =1024 megabytes
1 megabyte = 1024 KB
1 KB = 1024 bytes
1 byte = 8 bits
Bit is a 0 or a 1
Introduction
Byte (maybe)
Address
Data table
Type
Size (in bytes)
int
4
double
8
char
1
float
4
boolean
1
int a;
a=4;
Introduction
OS give me 4 bytes
Translate
to binary
OS store bits
at location----
4 bytes of memory at
location -------
Bits stored in memory
Bits
• What is the purpose of memory?
0 or 1
Base two versus base 10
0 1 2 3 4 5 6 7
Binary
1
0
int
char
1
22 + 21 + 20
A = 65
B = 66
C = 67
5
float
1111111 0000001
Introduction
String
One byte of memory
Ascii table is 127 characters
Heap
versus
stack
• Heap = free memory
• Stack = where programs are
void first()
{
int a;
int b;
}
int main()
{
int c;
}
Introduction
Heap
first();
main
a b
c
Using
memory
Java
Stack
Heap
int a;
String bob = new String();
bob = “Jones”;
Java magically takes care of
cleaning it up
C++
int first;
int* a;
a = new int;
delete a;
Heap
C++ requires you manually clean up
a = (int*)malloc(sizeof(int));
free(a);
Introduction
Stack
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);
Introduction
Possible b/c
of access C++ gives us
Using
pointers
• Creation
int* a;
a = new int;
• Dereference
*a = 4;
Put 4 into the address stored in A
• Arrays
int array1[10];
int* array2 = new int[10];
array1[0] =4;
array2[0]=4;
Introduction
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
Classes /
Pointers
class A
{
public:
A();
void print();
private:
int b;
};
A::A()
{
b=4;
}
void A::print()
{
cout << b << endl;
}
Introduction
A* example = new A();
example->print();
A object;
object.print();
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
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)
Introduction
NULL
• Universal
int* a;
a = new int;
delete a;
if (a != NULL)
cout << *a << endl;
• Doesn’t autoset to NULL, takes more
work…
Introduction
Protected
memory
• Whenever you steal something and are
caught…
• Whenever you access memory that
doesn’t belong to you
Introduction
Buffer
overflows
•
•
•
•
•
Buffer
instruction 1, instruction 2
Array of size 50
What you wrote
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
Introduction