Week 5 - Monday What did we talk about last time? Scope Systems - - PowerPoint PPT Presentation

week 5 monday what did we talk about last time scope
SMART_READER_LITE
LIVE PREVIEW

Week 5 - Monday What did we talk about last time? Scope Systems - - PowerPoint PPT Presentation

Week 5 - Monday What did we talk about last time? Scope Systems programming A C program is like a fast dance on a newly waxed dance floor by people carrying razors. Waldi Ravens In Windows, each drive has its own directory


slide-1
SLIDE 1

Week 5 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  Scope  Systems programming

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

A C program is like a fast dance on a newly waxed dance floor by people carrying razors.

Waldi Ravens

slide-6
SLIDE 6

 In Windows, each drive has its own directory hierarchy

  • C: etc.

 In Linux, the top of the file system is the root directory /

  • Everything (including drives, usually mounted in /mnt) is under the top

directory

  • /bin is for programs
  • /etc is for configuration
  • /usr is for user programs
  • /boot is for boot information
  • /dev is for devices
  • /home is for user home directories
slide-7
SLIDE 7

 There are regular files in Linux which you can further break down into

data files and executables (although Linux treats them the same)

 A directory is a special kind of file that lists other files  Links in Linux are kind of like shortcuts in Windows

  • There are hard links and soft links (or symbolic links)

 File names can be up to 255 characters long

  • Can contain any ASCII characters except / and the null character \0
  • For readability and compatibility, they should only use letters, digits, the

hyphen, underscore, and dot

 Pathnames describe a location of a file

  • They can start with / making them absolute paths
  • Or they are relative paths with respect to the current working directory
slide-8
SLIDE 8

 Every file has a UID and GID specifying the user who owns the

file and the group the file belongs to

 For each file, permissions are set that specify:

  • Whether the owner can read, write, or execute it
  • Whether other members of the group can read, write, or execute it
  • Whether anyone else on the system can read, write, or execute it

 The chmod command changes these settings (u is for owner,

g is for group, and o is everyone else)

slide-9
SLIDE 9

 All I/O operations in Linux are treated like file I/O  Printing to the screen is writing to a special file called stdout  Reading from the keyboard is reading from a special file called

stdin

 When we get the basic functions needed to open, read, and

write files, we'll be able to do almost any kind of I/O

slide-10
SLIDE 10

 A process is a program that is currently executing  In memory, processes have the following segments:

  • Text

The executable code

  • Data

Static variables

  • Heap

Dynamically allocated variables

  • Stack

Area that grows and shrinks with function calls

 A segmentation fault is when your code tries to access a segment

it's not supposed to

 A process generally executes with the same privileges as the user

who started it

slide-11
SLIDE 11
slide-12
SLIDE 12

 To declare an array of a specified type with a given name and

a given size:

 Example with a list of type int:

type name[ size ]; int list[ 100 ];

slide-13
SLIDE 13

 When you declare an array, you are creating the whole array  There is no second instantiation step

  • It is possible to create dynamic arrays using pointers and malloc(), but

we haven't talked about it yet

 You must give a fixed size (literal integer or a #define constant)

for the array

  • The version of gcc we are using allows variables, but many

implementations of C do not

 These arrays sit on the stack in C

  • Creating them is fast, but inflexible
  • You have to guess the maximum amount of space you'll need ahead of

time

slide-14
SLIDE 14

 You can access an element of an array by indexing into it, using

square brackets and a number

 Once you have indexed into an array, that variable behaves

exactly like any other variable of that type

 You can read values from it and store values into it  Indexing starts at 0 and stops at 1 less than the length

  • Just like Java

list[9] = 142; printf("%d", list[9]);

slide-15
SLIDE 15

 The length of the array must be known at compile time

  • Our version of gcc has looser rules about this, but C90 insists on true

constants

 There is no length member or length() method  It is possible to find out how many bytes a statically allocated

array uses with sizeof

  • But you can only be sure that works in the function where the array is

defined!

int list[100]; int size = sizeof(list); //400 int length = size/sizeof(int); //100

slide-16
SLIDE 16

 When you create an array, it is not automatically filled with

any particular value

 Inside the array (like any variable in C) is garbage  With regular variables, you might get a warning if you use a

variable before you initialize it

 With an array, you won't

slide-17
SLIDE 17

 Explicit initialization can be done with a list:  You can omit the size if you use an explicit initialization because

the compiler can figure it out

int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; char grades[] = { 'A', 'B', 'C', 'D', 'F'};

slide-18
SLIDE 18

 The C standard library has a function called memset() that can set all

the bytes in a chunk of memory to a particular value

 Using it is guaranteed to be no slower than using a loop to initialize all the

values in your array

  • It usually uses special instructions to set big chunks of memory at the same time

int values[100]; memset(values, 0, sizeof(int)*100); //zeroes out array char letters[26]; memset(letters, 'A', sizeof(char)*26); //sets array to all 'A's

slide-19
SLIDE 19

 memset() is mostly useful for initialization (and usually only for

zeroing things out)

 memcpy() is a fast way to copy values from one array to another

  • Again, it's at least as fast as using your own loop
  • Again, it's somewhat dangerous since it lets you write memory places en

masse

int cubes[100]; int copy[100]; int i = 0; for( i = 0; i < 100; i++) cubes[i] = i*i*i; memcpy(copy, cubes, sizeof(cubes));

slide-20
SLIDE 20

 Using an array in a function where it wasn't created is a little different  You have to pass in the length  The function receiving the array has no other way to know what the

length is

  • sizeof will not work because it is based on what is known at compile time

 The function should list an array parameter with empty square brackets

  • n the right of the variable

 No brackets should be used on the argument when the function is called  Like Java, arguments are passed by value, but the contents of the array

are passed by reference

  • Changes made to an array in a function are seen by the caller
slide-21
SLIDE 21

 Calling code:

int values[100]; for(int i = 0; i < 100; i++ ) values[i] = i + 1; reverse(values, 100);

slide-22
SLIDE 22

 Function:

void reverse(int array[], int length) { int start = 0; int end = length – 1; int temp = 0; while( start < end ) { temp = array[start]; array[start++] = array[end]; array[end--] = temp; } }

slide-23
SLIDE 23

 In C, you can't return the kind of arrays we're talking about

  • Why?

 They are allocated on the stack  When a function returns, all its memory disappears  If you dynamically allocate an array with malloc(), you can

return a pointer to it

slide-24
SLIDE 24
slide-25
SLIDE 25

 An array takes up the size of each element times the length of

the array

 Each array starts at some point in computer memory  The index used for the array is actually an offset from that

starting point

 That's why the first element is at index 0

slide-26
SLIDE 26

 We can imagine that we have an array of type int of length

10

 Let's say the array starts at address 524

12 43

  • 9

6 789

  • 23

23 10 6

0 1 2 3 4 5 6 7 8 9

524 528 532 536 540 544 548 552 556 560

Addresses Indexes

slide-27
SLIDE 27

 It is legal to declare multidimensional arrays in C  They'll work just as you would expect  Except! You have to give the second dimension when passing to a function (otherwise, it

won't know how big of a step to take when going from row to row)

char board[8][8]; void clearBoard( char board[][8]) { for(int i = 0; i < 8; i++ ) for(int j = 0; j < 8; j++ ) board[i][j] = ' '; }

slide-28
SLIDE 28

 Write a program that reads an integer from the user saying how many

values will be in a list

  • Assume no more than 100
  • If the user enters a value larger than 100, tell them to try a smaller value

 Read these values into an array  Find

  • Maximum
  • Minimum
  • Mean
  • Variance
  • Median
  • Mode
slide-29
SLIDE 29
slide-30
SLIDE 30

 Strings

slide-31
SLIDE 31

 Keep reading K&R chapter 5  Keep working on Project 2

  • Due Friday

 Exam 1 next Monday!