24
 C++: Getting started ...and some more.

comp322_02

Embed Size (px)

Citation preview

Page 1: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 1/24

 

C++: Getting started

...and some more.

Page 2: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 2/24

 

A do-nothing program

/* A sample program in C++

with comments */

#include <iostream>

using namespace std;

int main ()

{

cout << "Hello there! "; // prints Hello World!

cout << "I'm a C++ program"; // prints I'm a C++ program

return 0;

}

Page 3: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 3/24

 

About this program

● Very simple non-OO program.

 – Difference with Java, where you must createclasses

● Demonstrates standard ways to outputtext

● Introduces standard headers and the

concept of “namespaces”

Page 4: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 4/24

 

Dissecting...

● Use // or /* ... */ to put comments.

 – Relevant comments are important formaintainability.

● note the signature of the“main” program – also a must have

 – another form: int main( int argc, char **argv)

● Must “include” the standard I/O headerfor input/output

Page 5: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 5/24

 

Do-nothing program #2

/* A sample program in C++

with comments */

#include <iostream>

int main ()

{

std::cout << "Hello there!\n";

std::cout << "I'm a C++ program" << std::endl;

return 0;

}

Page 6: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 6/24

 

Note...

●  The scope resolution (SR) operator “::”

 – “which entity do I belong to?”

● In this case, namespace association

 – “std::cout”

 – There's a class SR operator

 – Can be used to “unhide”, as we shall see as

we talk about globals

Page 7: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 7/24

 

Quick declarators

● Declarators designate data objects orfunction/methods

● int i = 10; char c = 'A'; string s = “waah!”;

● template <typename T> void SortData( T *data );

●  The above declaration shows use of templates (more on that later in thecourse)

Page 8: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 8/24

 

Know these?

● volatile int i = 0;

● register c = 11;

● char **names;

● const int *j = &i;

● extern int userID;

extern const volatile int clock;

Page 9: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 9/24

Page 10: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 10/24

 

Memory structure (simplified)

Command line argumentsEnvironment variables

Stack (Function/method arguments, locals, return addresses)

Free store (Heap)

Dynamically allocated memory

High address

Low address

Code (Read-only)Uninitialized/Initialized data (No eXecute)

Page 11: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 11/24

 

Locals vs Globals vs Members

● Globals are to be avoided (the OO schoolof thought)

● Called Global because, it's scope starts

where it is declared – Creates confusion with locals

 – Can be inadvertently changed. causing

endless frustrations, broken relationships,hair loss, weight gain, etc etc (not really)

Page 12: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 12/24

 

Global Example

#include <iostream>

int k = 10, i = 0;

void MessMeUp()

{

int k = 11;

for( i = 0; i < N; i++ )

// Do something...

}

Page 13: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 13/24

 

 That said...

● Globals are important in C because of no“data association”

● C++ provides classes

 – meaning, data and code are tightly coupled

 – and thus, C++ programmers tend to not useglobals

Page 14: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 14/24

 

Function calls

● Difference between declaration anddefinition?

● declaration allocates no memory,

definition does.● Function calling and returning process

 – For reference and value parameters

 – For inline functions (in C-lingo, Macros)

Page 15: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 15/24

 

Calling process

● Push return address

 – saves the “where was I” information

● allocate memory for local variables and/or

arguments (important distinction)● copy value or address or arguments

● transfer control of program to function

address.

Page 16: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 16/24

 

Return process

● Pop the stack frame

 – DESTROYS local variables automatically(unless there's dynamic memory

allocated)● Check the return address for validity

●  Transfer control to return address if valid

Page 17: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 17/24

 

Demo

int main(){

int toPass = 10;CallDemoFunction( toPass );

...

int CallDemoFunction( int someArg )

{std::cout <<"Just someArg which is " << someArg << std::endl;return 12;

}

Page 18: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 18/24

 

Points to note

● Copies of arguments may be created, ornot.

 – Meaning, changes to arguments may not

stick or not. – Why?

 – Since the stack frame is destroyed, so are thecopies (and hence local changes)

●  To make changes persistent, must NOT be creating copies of arguments

Page 19: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 19/24

 

Value vs Reference Params

● Instead, must pass address of arguments

●  This is “Pass by reference”

● Otherwise, it's “Pass by value”

●  To reiterate

 – To make permanent changes to arguments,pass by reference

 – Otherwise, pass by value

Page 20: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 20/24

 

So how do we?

● Swap example: C-style firstvoid swap( int *x, int *y )

{

int z = *x;*x = *y;

*y = *z;

}

...

int i = 10, j = 20;

swap( &i, &j );

Page 21: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 21/24

 

C++ is simpler

● C++ has “Reference” typesvoid swap( int &x, int &y )

{

int z = x;x = y;

y = z;

}

...

int i = 10, j = 20;

swap( i, j );

Page 22: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 22/24

 

“No-Worky” version

● Passing by value:void swap( int x, int y )

{

int z = x;x = y;

y = z;

}

...int i = 10, j = 20;

swap( i, j );

Page 23: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 23/24

 

Complete Listing//

// memory_sample.cc//#include <iostream>

using namespace std;

void swap( int &x, int &y ){

int z = x;x = y;y = z;

}

int main(){

int n1 = 10, n2 = 100;swap( n1, n2 );cout << "N1 is " << n1 << " and N2 is " << n2 <<

endl;

return 0;}

Page 24: comp322_02

8/4/2019 comp322_02

http://slidepdf.com/reader/full/comp32202 24/24

 

With g++...

g++ -o memory_sample memory_sample.cc -g2-Wall

./memory_sample

 The flags – -Wall: means with all warnings

 – -g: include debugging information. 0,1,2,3indicates increasing degrees of information.

*VERY USEFUL* – -o: name of the executable output. if omitted,

defaults to a.out.