cis 190: c/c++ programmingcis190/fall2014/lectures/04/lec04.pdfgiving command line arguments...

140
CIS 190: C/C++ Programming Lecture 4 Assorted Topics (and More on Pointers) 1

Upload: others

Post on 21-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

CIS 190: C/C++ Programming

Lecture 4

Assorted Topics (and More on Pointers)

1

Page 2: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

2

Page 3: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Makefiles

• list of rules you can call from the terminal

– make ruleTwo will call the ruleTwo

– make will call first rule in the file

• basic formatting

– use # at line beginning to denote comments

– must use tab character, not 8 spaces

3

Page 4: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Rule Construction

target: dependencies (optional)

command

another command (optional)

• target (rule name)

• dependency (right side of colon)

• command (explicit commands)

4

Page 5: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

• let’s create a rule to compile and link the files for Homework 4A:

hw4a.c

karaoke.c

karaoke.h

• what commands will let us do this?

5

Page 6: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

we need to, in order:

1. separately compile hw4a.c

2. separately compile karaoke.c

3. link hw4a.o and karaoke.o together

6

Page 7: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

1. separately compile hw4a.c

• we’ll make a rule called hw4a.o

• what command would we run in the terminal?

• what files does it need to work?

7

Page 8: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

1. separately compile hw4a.c

• we’ll make a rule called hw4a.o

• what command would we run in the terminal?

• what files does it need to work?

– hw4a.c

– so it’s dependent on hw4a.c

8

Page 9: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

2. separately compile karaoke.c

• we’ll call this rule karaoke.o

• what command will compile karaoke.c?

• what files does it need to work?

9

Page 10: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

2. separately compile karaoke.c

• we’ll call this rule karaoke.o

• what command will compile karaoke.c?

• what files does it need to work?

– karaoke.c

– so it’s dependent on karaoke.c

10

Page 11: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

3. link hw4a.o and karaoke.o together

• we’ll call this rule hw4a

• what command will link the files together?

• what files does it depend on?

11

Page 12: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Creating a Rule

3. link hw4a.o and karaoke.o together

• we’ll call this rule hw4a

• what command will link the files together?

• what files does it depend on?

– hw4a.o

– karaoke.o

– so it’s dependent on both of these files

12

Page 13: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Other Common Rules

• a rule to remove .o and executable files clean:

rm –f *.o hw4a

• a rule to remove garbage files cleaner:

rm –f *~

• a rule to run both cleanest: clean cleaner

13

Page 14: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Why Use Makefiles

• makes compiling, linking, executing, etc

– easier

– quicker

– less prone to human error

• allows use to create and run helper rules

– clean up unneeded files (like hw2.c~ or trains.o)

– open files for editing

14

Page 15: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Makefiles and Beyond

• there’s much more you can do with Makefiles

– variables

– conditionals

– system configuration

– phony targets

• more information available here

http://www.chemie.fu-berlin.de/chemnet/use /info/make/make_toc.html

15

Page 16: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

16

Page 17: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Input and Output

• printf

– stdout

– output written to the terminal

• scanf

– stdin

– input read in from user

• redirection

– executable < input.txt > output.txt

17

Page 18: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

FILE I/O Basics

• allow us to read in from and print out to files

– instead of from and to the terminal

• use a file pointer (FILE*) to manage the file(s) we want to be handling

• naming conventions:

FILE* ofp; /* output file pointer */

FILE* ifp; /* input file pointer */

18

Page 19: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Opening a File

FILE* fopen (<filename>, <mode>);

• fopen() returns a FILE pointer

– hopefully to a successfully opened file

• <filename> is a string

• <mode> is single-character string

19

Page 20: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

FILE I/O Reading and Writing

ifp = fopen(“input.txt”, “r”);

• opens input.txt for reading

– file must already exist

20

Page 21: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

FILE I/O Reading and Writing

ifp = fopen(“input.txt”, “r”);

• opens input.txt for reading

– file must already exist

ofp = fopen(“output.txt”, “w”);

• opens output.txt for writing

– if file exists, it will be overwritten

21

Page 22: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

FILE I/O Reading and Writing

ifp = fopen(“input.txt”, “r”);

• opens input.txt for reading

– file must already exist

ofp = fopen(“output.txt”, “w”);

• opens output.txt for writing

– if file exists, it will be overwritten

22

Page 23: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Dealing with FILE Pointers

• FILE pointers should be handled with the same care as allocated memory

1. check that it works before using

2. gracefully handle failure

3. free when finished

23

Page 24: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Handling FILE Pointers

1. check that it worked before using

• if the FILE pointer is NULL, there was an error

2. gracefully handle failure

• print out an error message

• exit or re-prompt the user, as appropriate

3. free the pointer when finished

• use fclose() and pass in the file pointer

24

Page 25: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Standard Streams in C

• three standard streams: stdin, stdout, stderr

• printf() and scanf() automatically access stdout and stdin, respectively

• printing to stderr prints to the terminal

– even if we use redirection

25

Page 26: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Using File Pointers

• fprintf fprintf(ofp, “print: %s\n”, textStr);

– output written to where ofp points

• fscanf fscanf(ifp, “%d”, &inputInt);

– input read in from where ifp points

26 LIVECODING LIVECODING

Page 27: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Using stderr with fprintf

/* if an error occurs */

if (error)

{

fprintf(stderr,

“An error occurred!”);

exit(-1);

/* exit() requires <stdlib.h> */

}

27

LIVECODING LIVECODING

Page 28: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Reaching EOF with fscanf

• fscanf() returns an integer

– number of items in argument list that were filled

• if no data is read in, it returns EOF

– EOF = End Of File (pre-defined)

• once EOF is returned, we have reached the end of the file

– handle appropriately (e.g., close)

28 LIVECODING LIVECODING

Page 29: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Reaching EOF Example

• example usage: while (fscanf(ifp, “%s”, str) != EOF)

{

/* do things */

}

/* while loop exited, EOF reached */

• to use fscanf() effectively, it helps to know basic information about the layout of the file

29 LIVECODING LIVECODING

Page 30: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

30

Page 31: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Giving Command Line Arguments

• command line arguments are given after the executable name on the command line

– allows user to change parameters at run time without recompiling or needing access to code

– also sometimes called CLAs

• for example, the following might allow a user to set the maximum number of train cars:

> ./hw2 25

31

Page 32: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Handling Command Line Arguments

• handled as parameters to main() function int main(int argc, char **argv)

• int argc – number of arguments

– including name of executable

• char **argv – array of argument strings

32

Page 33: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

More About argc/argv

• names are by convention, not required

• char **argv can also be written as char *argv[]

• argv is just an array of strings (the arguments)

• for example, argv[0] is the executable

− since that is the first argument passed in

33

Page 34: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

34

Page 35: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

• in this example:

– argc = ???

– argv[0] is ???

– argv[1] is ???

– argv[2] is ???

35

Page 36: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

• in this example:

– argc = 3 (executable, number, and city)

36

Page 37: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

• in this example:

– argc = 3 (executable, number, and city)

– argv[0] is “./hw2”

37

Page 38: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

• in this example:

– argc = 3 (executable, number, and city)

– argv[0] is “./hw2”

– argv[1] is “25”

38

Page 39: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Command Line Argument Example

> ./hw2 25 Savannah

– set max # of cars and a departure city

• in this example:

– argc = 3 (executable, number, and city)

– argv[0] is “./hw2”

– argv[1] is “25”

– argv[2] is “Savannah”

39

Page 40: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

How to Use argc

• before we begin using CLAs, we need to make sure that we have been given what we expect

• check that the value of argc is correct

– that the number of arguments is correct

• if it’s not correct, exit and prompt user with expected program usage

40 LIVECODING LIVECODING

Page 41: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

How to Use argv

• char **argv is an array of strings

• if an argument needs to be an integer, we must convert it from a string

– using the atoi() function (from <stdlib.h>)

intArg = atoi(“5”);

intArg = atoi( argv[2] );

41 LIVECODING LIVECODING

Page 42: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Optional Command Line Arguments

• argument(s) can optional

– e.g., default train to size 20 if max size not given

• number of acceptable CLAs is now a range, or at least a minimum number

• should only use the CLAs you actually have

42

Page 43: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Handling Optional CLAs

if (argc > MAX_ARGS) {

/* print out error message */

exit(-1);

}

if (argc >= SIZE_ARG+1) {

trainSize = argv[SIZE_ARG];

} else {

trainSize = DEFAULT_TRAIN_SIZE;

} 43

Page 44: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

44

Page 45: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Random Numbers

• useful for many things:

– cryptography, games of chance & probability, procedural generation, statistical sampling

• random numbers generated via computer can only be pseudorandom

45

Page 46: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Pseudo Randomness

• “Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.” – John von Neumann

• pseudorandom

– appears to be random, but actually isn’t

– mathematically generated, so it can’t be

46

Page 47: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Seeding for Randomness

• you can seed the random number generator

• same seed means same “random” numbers

– good for testing, allow identical runs

void srand (unsigned int seed);

srand(1);

srand(seedValue);

47

Page 48: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Seeding with User Input

• can allow the user to choose the seed

– gives user more control over how program runs

srand(userSeedChoice);

• obtain user seed choice via

– in-program prompt (“Please enter seed: ”)

– as a command line argument

• can make this an optional CLA

48

Page 49: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Seeding with Time

• can also give a “unique” seed with time()

– need to #include <time.h> library

• time() returns the seconds since the “epoch”

– normally since 00:00 hours, Jan 1, 1970 UTC

• NOTE: if you want to use the time() function, you can not have a variable called time error: called object ‘time’ is not a function

49

Page 50: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Example of Seeding with time()

• get the seconds since epoch int timeSeed = (int) time(0);

– time() wants a pointer, so just give it 0

– returns a time_t object, so we cast as int

• use timeSeed to seed the rand() function

srand(timeSeed);

• NOTE: running again within a second will return the same value from time()

50

Page 51: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Generating Random Numbers

int rand (void);

• call the rand() function each time you want a random number

int randomNum = rand();

• integer returned is between 0 and RAND_MAX

– RAND_MAX guaranteed to be at least 32767

51

Page 52: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Getting a Usable Random Number

• if we want a smaller range than 0 - 32767?

• use % (mod) to get the range you want

/* 1 to MAX */

int random = (rand() % MAX) + 1;

/* returns MIN to MAX, inclusive */

int random = rand() % (MAX – MIN + 1) + MIN;

52

Page 53: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

53

Page 54: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Why Pointers Again?

• important programming concept

• understand what’s going on “inside”

• other languages use pointers heavily

– you just don’t see them!

• but pointers can be difficult to understand

– abstract concept

– unlike what you’ve learned before

54

Page 55: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• all variables have two parts:

– value

5

55

Page 56: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• all variables have two parts:

– value

– address where value is stored

0xFFC0 5

56

Page 57: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• all variables have two parts:

– value

– address where value is stored

• x’s value is 5

0xFFC0 5

value

57

Page 58: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• all variables have two parts:

– value

– address where value is stored

• x’s value is 5

• x’s address is 0xFFC0

0xFFC0 5

value address

58

Page 59: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• so the code to declare this is: int x = 5;

0xFFC0 5

value address

59

Page 60: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• we can also declare a pointer: int x = 5;

int *ptr;

0xFFC0 5

value address

60

Page 61: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• and set it equal to the address of x:

int x = 5;

int *ptr;

ptr = &x;

0xFFC0 5

value address

61

Page 62: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• ptr = &x

0xFFC0 5

value address

62

Page 63: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• ptr = &x

• *ptr = x

0xFFC0 5

value address

63

Page 64: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Regular Variables

• ptr points to the address where x is stored

• *ptr gives us the value of x

– (dereferencing ptr)

0xFFC0 5

value address

64

Page 65: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• but what about the variable ptr?

– does it have a value and address too?

0xFFC0 5

value address

65

Page 66: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• but what about the variable ptr?

– does it have a value and address too?

• YES!!!

0xFFC0 5

value address

66

Page 67: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• ptr’s value is just “ptr” – and it’s 0xFFC0

0xFFC0 5

value address

67

Page 68: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• ptr’s value is just “ptr” – and it’s 0xFFC0

0xFFC0 5

value address

0xFFC0

value

68

Page 69: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• ptr’s value is just “ptr” – and it’s 0xFFC0

• but what about its address?

0xFFC0 5

value address

0xFFC0

value

69

Page 70: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• ptr’s value is just “ptr” – and it’s 0xFFC0

• but what about its address?

– its address is &ptr

0xFFC0 5

value address

0xFFC0

value

70

Page 71: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• ptr’s value is just “ptr” – and it’s 0xFFC0

• but what about its address?

– its address is &ptr

0xFFC0 5

value address

0xFFC4 0xFFC0

value address

71

Page 72: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• if you want, you can think of value and address for pointers as this instead…

0xFFC0 5

value address

0xFFC4 0xFFC0

value address

72

Page 73: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• address where it’s stored in memory

0xFFC0 5

value address

0xFFC4 0xFFC0

value address where it’s stored in memory

73

Page 74: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – Pointer Variables

• address where it’s stored in memory

• value where it points to in memory

0xFFC0 5

value address

0xFFC4 0xFFC0

value where it

points to in memory

address where it’s stored in memory

74

Page 75: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• each process gets its own memory chunk, or address space

Stack

Heap

Global/static vars

Code

0x000000

0xFFFFFFF

4 GB address space

Function calls, locals

Dynamically allocated memory

“data segment”

“code segment” 75

Page 76: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• you can think of memory as being “owned” by: – the OS

• most of the memory the computer has

– the process • a chunk of memory given by the OS – about 4 GB

– the program • memory (on the stack) given to it by the process

– you • when you dynamically allocate memory in the program

(memory given to you by the process )

76

Page 77: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• the Operating System has a very large amount of memory available to it

the OS the OS the OS the OS

77

Page 78: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• when the process begins, the Operating System gives it a chunk of that memory

the OS the OS

the OS Stack

Heap

Global/static vars

Code 78

Page 79: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• when the process begins, the Operating System gives it a chunk of that memory

Stack

Heap

Global/static vars

Code 79

Page 80: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• when the process begins, the Operating System gives it a chunk of that memory

Stack

Heap

Global/static vars

Code 80

Page 81: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• within that chunk of memory, only the stack and the heap are available to you and the program

Stack

Heap

Global/static vars

Code 81

Page 82: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• within that chunk of memory, only the stack and the heap are available to you and the program

Stack

Heap

82

Page 83: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• within that chunk of memory, only the stack and the heap are available to you and the program

Stack

Heap

83

Page 84: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• some parts of the stack are given to the program for variables

Stack

Heap

84

Page 85: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• some parts of the stack are given to the program for variables

Stack

Heap

program variables

85

Page 86: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and when a function is called, the program is given more space on the stack for the return address and in-function variables

Stack

Heap

program variables

function return address & variables

86

Page 87: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

Stack

Heap

program variables

function return address & variables

87

Page 88: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

CAR* train;

char* userStr;

int* intArray;

Stack

Heap

program variables

function return address & variables

88

Page 89: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

CAR* train;

char* userStr;

int* intArray;

Stack

Heap

program variables

function return address & variables

intArray

train

userStr

?

?

? 89

Page 90: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

Stack

Heap

intArray = (int*) malloc(…)

program variables

function return address & variables

intArray

train

userStr

?

?

90

Page 91: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

Stack

Heap

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

program variables

function return address & variables

intArray

userStr

train

?

91

Page 92: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory Basics – “Owning” Memory

• and every time you allocate memory, the process gives you space for it on the heap

Stack

Heap

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

program variables

function return address & variables

intArray

userStr

train

92

Page 93: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Owning” Memory

• don’t forget – those pointers are program variables, so where they are stored is actually on the stack with the rest of the program variables!

– they are program variables because they are declared in the program’s code

Stack

Heap

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

program variables

function return address & variables

intArray

userStr

train

93

Page 94: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• but how does the process get any of that memory back?

Stack

Heap

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

program variables

function return address & variables

intArray

userStr

train

94

Page 95: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• when a function returns, the program gives that memory on the stack back to the process

Stack

Heap

program variables

function return address & variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

95

Page 96: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• when a function returns, the program gives that memory on the stack back to the process

return fxnAnswer;

Stack

Heap

program variables

function return address & variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

96

Page 97: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• when a function returns, the program gives that memory on the stack back to the process

return fxnAnswer;

Stack

Heap

program variables

function return address & variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

97

Page 98: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• when a function returns, the program gives that memory on the stack back to the process

Stack

Heap

program variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

98

Page 99: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• and when you use free(), the memory you had on the heap is given back to the process

Stack

Heap

program variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

99

Page 100: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• and when you use free(), the memory you had on the heap is given back to the process

free(intArray);

Stack

Heap

program variables

intArray

userStr

train

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

100

Page 101: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• and when you use free(), the memory you had on the heap is given back to the process

free(intArray);

Stack

Heap

program variables

intArray = (int*) malloc(…)

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

intArray

userStr

train

101

Page 102: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – “Returning” Memory

• and when you use free(), the memory you had on the heap is given back to the process

Stack

Heap

program variables

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

userStr

train

intArray 102

Page 103: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• but simply using free() doesn’t change anything about the intArray variable

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

103

Page 104: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• but simply using free() doesn’t change anything about the intArray variable

• it still points to that space in memory

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

104

Page 105: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• but simply using free() doesn’t change anything about the intArray variable

• it still points to that space in memory

• it’s still stored on the stack with the rest of the variables

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

105

Page 106: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• intArray is now a

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

106

Page 107: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• intArray is now a dangling pointer

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

107

Page 108: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• intArray is now a dangling pointer

– points to memory that has been freed

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

108

Page 109: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• intArray is now a dangling pointer

– points to memory that has been freed

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

109

Page 110: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• intArray is now a dangling pointer

– points to memory that has been freed

– memory which is now back to being owned by the process, not you Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

110

Page 111: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• if we tried to free() intArray’s memory again

• we would get a

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

111

Page 112: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• if we tried to free() intArray’s memory again

• we would get a

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

112

Page 113: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• if we tried to free() intArray’s memory again

• we would get a

• to prevent segfaults, good programming practices dictate that after free()ing, we set intArray to be equal to

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

113

Page 114: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• if we tried to free() intArray’s memory again

• we would get a

• to prevent segfaults, good programming practices dictate that after free()ing, we set intArray to be equal to

Stack

Heap

program variables

userStr

train

intArray

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL

114

Page 115: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Memory Errors

• NOTE: if you try to free a NULL pointer, no action occurs (and it doesn’t segfault!)

• much safer than accidentally double free()ing memory

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

115

Page 116: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• the process is capable of giving memory to you and the program as many times as necessary (including having that memory returned), as long as it doesn’t run out of memory to hand out

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

116

Page 117: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

117

Page 118: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

intArray = (int*)

malloc ( sizeof(int)

* HUGE_NUM);

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

118

Page 119: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

intArray = (int*)

malloc ( sizeof(int)

* HUGE_NUM);

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

intArray = (int*) malloc (sizeof(int) * HUGE_NUM)

119

Page 120: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

intArray = (int*)

malloc ( sizeof(int)

* HUGE_NUM);

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

intArray = (int*) malloc (sizeof(int) * HUGE_NUM)

120

Page 121: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

intArray = (int*)

malloc ( sizeof(int)

* HUGE_NUM);

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

intArray = (int*) malloc (sizeof(int) * HUGE_NUM)

121

Page 122: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

• malloc will return NULL

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

122

Page 123: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

(also program variables)

Memory Basics – Running Out

• if you try to allocate memory, but there’s not enough contiguous space to handle your request

• malloc will return NULL

• that’s why you must check that intArray != NULL before you use it

Stack

Heap

program variables

userStr

train

userStr = (char*) malloc(…)

train = (CAR*) malloc(…)

NULL intArray

123

Page 124: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Quick Note on Segfaults

• segfaults are not consistent (unfortunately)

• even if something should result in a segfault, it might not (and then occasionally it will) – this doesn’t mean there isn’t an error!

– C is trying to be “nice” to you when it can

• you have to be extra-super-duper-careful with your memory management!!!

124

Page 125: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

125

Page 126: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• how do different types of variables get passed to and returned from functions?

• passing by value

• passing by reference

– implicit: arrays, strings

– explicit: pointers

126

Page 127: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• some simple examples: int Add(int x, int y);

int answer = Add(1, 2);

void PrintMenu(void);

PrintMenu();

int GetAsciiValue(char c);

int ascii = GetAsciiValue (‘m’);

• all passed by value

127

Page 128: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing arrays to functions void TimesTwo(int array[], int size);

int arr [ARR_SIZE];

/* set values of arr */

TimesTwo(arr, ARR_SIZE);

• arrays of any type are passed by reference – changes made in-function persist

128

Page 129: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing arrays to functions

void TimesTwo(int array[], int size);

void TimesTwo(int * array, int size);

• both of these behave the same way

– they take a pointer to:

• the beginning of an array

• an int – that we (can) treat like an array

129

Page 130: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing strings to functions void PrintName(char name[]);

void PrintName(char *name );

char myName [NAME_SIZE] = “Alice”;

PrintName(myName);

• strings are arrays (of characters)

– implicitly passed by reference

130

Page 131: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing pointers to int to functions

void Square(int *n);

int x = 9;

Square(&x);

• pass address of an integer (in this case, x)

131

Page 132: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing int pointers to function

void Square(int *n);

int x = 9;

int *xPtr = &x;

Square(???);

• pass ???

132

Page 133: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• passing int pointers to function

void Square(int *n);

int x = 9;

int *xPtr = &x;

Square(xPtr);

• pass xPtr, which is an address to an integer (x)

133

Page 134: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• returning pointers from functions

CAR* MakeCar(void) {

CAR temp;

return &temp; }

• temp is on the stack – so what happens?

134

Page 135: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• returning pointers from functions

CAR* MakeCar(void) {

CAR temp;

return &temp; }

• temp is on the stack – so it will be returned to the process when MakeCar() returns!

135

Page 136: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• returning pointers from functions

CAR* MakeCar(void) {

CAR* temp;

temp = (CAR*) malloc (sizeof(CAR));

return temp; }

• temp is on the heap – so what happens?

136

Page 137: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Memory and Functions

• returning pointers from functions

CAR* MakeCar(void) {

CAR* temp;

temp = (CAR*) malloc (sizeof(CAR));

return temp; }

• temp is on the heap – so it belongs to you and will remain on the heap until you free() it

137

Page 138: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Outline

• Makefiles

• File I/O

• Command Line Arguments

• Random Numbers

• Re-Covering Pointers

• Memory and Functions

• Homework

138

Page 139: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Homework 4A

• Karaoke

• File I/O

• command line arguments

• allocating memory

• no grade for Homework 4A

• turn in working code or -10 points for HW 4B

139

Page 140: CIS 190: C/C++ Programmingcis190/fall2014/lectures/04/lec04.pdfGiving Command Line Arguments •command line arguments are given after the executable name on the command line –allows

Quick Notes

• answered questions from HW2 on Piazza

• magic numbers

– should use #defines as you code

– not replace with #define after you’re done

• elegant solution to printing the full train

140