32
CSE 333 Section 1 C, Pointers, and Gitlab 1

333 Section 1 Slides - courses.cs.washington.edu

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 333 Section 1 Slides - courses.cs.washington.edu

CSE 333 Section 1

C, Pointers, and Gitlab

1

Page 2: 333 Section 1 Slides - courses.cs.washington.edu

LogisticsDue Friday:

Exercise 1 @ 10 am

Due Monday:

HW0 (setup Gitlab ASAP) @11 pm

Exercise 2 @10 am (will be available on Friday after lecture)

2

Page 3: 333 Section 1 Slides - courses.cs.washington.edu

attu/CSE VM Setup and Gitlab Demo

5

Page 4: 333 Section 1 Slides - courses.cs.washington.edu

Attu/CSE VM Updates and gcc 9• Attu and the CSE VMs got an update to Rocky Linux 8.4!

• We’re using gcc 9 this quarter, but need to do a bit to configure it• Log on to attu or CSE VM (whichever one you’re using)• Run this command: touch ~/.gcc9• Log off and log back in• Run gcc –v and verify that it shows gcc version 9

Page 5: 333 Section 1 Slides - courses.cs.washington.edu

Attu/CSE VM Updates and gcc9• Why?

• The bash login scripts look for the .gcc9 file and adjust gcc8 to gcc9 if found

• What if I don’t do this? • Your hw1 will crash spectacularly• We’ve found some fun bugs where the exercise autograder will

generate a warning that you won’t see and you will be marked down ☹

Page 6: 333 Section 1 Slides - courses.cs.washington.edu

Accessing Gitlab• Sign In using your CSE NetID @ https://gitlab.cs.washington.edu/• You should have a repo created for you titled: cse333-21au-<netid>

Page 7: 333 Section 1 Slides - courses.cs.washington.edu

SSH Key Generation

9

Step 1a: Check if you have a key• Run cat ~/.ssh/id_rsa.pub• If you see a long string starting with ssh-rsa or ssh-dsa go to Step 2

Step 1b: Generate a new SSH key if necessary• Run ssh-keygen -t rsa -C "<netid>@cs.washington.edu" to generate a new key

• Click enter to skip creating a password• git docs suggest creating a password, but it’s overkill for 333 and complicates

operations

Page 8: 333 Section 1 Slides - courses.cs.washington.edu

SSH Key Generation

10

Step 2: Copy SSH key• run cat ~/.ssh/id_rsa.pub• Copy the complete key key starting with ssh- and ending with your username and host

Step 3: Add SSH key to gitlab• Navigate to your ssh-keys page (click on your avatar in the upper-right, then “Settings,” then “SSH Keys” in the left-side menu)

• Paste into the “Key” text box and give a “Title” to identify what machine the key is for

• Click the green “Add key” button below “Title”

Page 9: 333 Section 1 Slides - courses.cs.washington.edu

11

First Commit1) git clone <repo url from project page>

• Clones your repo

2) touch README.md• Creates an empty file called README.md

3) git status• Prints out the status of the repo: you should see 1 new file README.md

4) git add README.md• Stages a new file/updated file for commit. git status: README.md staged for commit

5) git commit -m "First Commit"• Commits all staged files with the provided comment/message.

git status: Your branch is ahead by 1 commit.

6) git push• Publishes the changes to the central repo. You should now see these changes in the web interface (may

need to refresh).• Might need git push -u origin master on first commit (only)

Page 10: 333 Section 1 Slides - courses.cs.washington.edu

Git Repo Usage

12

Try to use the command line interface (not Gitlab’s web interface)

Only push files used to build your code to the repo• No executables, object files, etc.• Don’t always use <git add .> to add all your local files

Commit and push when an individual chunk of work is tested and done• Don’t push after every edit• Don’t only push once when everything is done

Page 11: 333 Section 1 Slides - courses.cs.washington.edu

Git References

13

•SSH Key generation: https://gitlab.cs.washington.edu/help/ssh/README.md

•Basic Git Tutorial: https://courses.cs.washington.edu/courses/cse333/21au/resources/git_tutorial.html

•Steps of what we just demo'd:https://courses.cs.washington.edu/courses/cse333/21au/hw/git_setup.html

Page 12: 333 Section 1 Slides - courses.cs.washington.edu

Pointer Review

14

Page 13: 333 Section 1 Slides - courses.cs.washington.edu

Pointers

Pointers are just another primitive data type.

An integer can hold an index into an array.

If memory is a giant array of bytes, then a pointer just holds an index into that array.

15

type *name;

int32_t *ptr;

0x7ff….ptr

ptr

Page 14: 333 Section 1 Slides - courses.cs.washington.edu

510

Pointer Syntax

“Address of”

“Value at”

&

*

16

int32_t x;int32_t *ptr;

ptr

x

ptr = &x;x = 5;*ptr = 10;

Page 15: 333 Section 1 Slides - courses.cs.washington.edu

Exercise 1

17

Page 16: 333 Section 1 Slides - courses.cs.washington.edu

18

5

22

42

x

y

z

X (foo)

y (foo)

z (foo)

42

37

Draw a memory diagram like the one above for the following code and determine what the output will be.

void foo(int32_t *x, int32_t *y, int32_t *z) { x = y; *x = *z; *z = 37;}

int main(int argc, char *argv[]) { int32_t x = 5, y = 22, z = 42; foo(&x, &y, &z); printf("%d, %d, %d\n", x, y, z); return EXIT_SUCCESS;}

So, the code will output 5, 42, 37.

Page 17: 333 Section 1 Slides - courses.cs.washington.edu

C-Strings

19

Page 18: 333 Section 1 Slides - courses.cs.washington.edu

C-Strings

- A string in C is declared as an array of characters that is terminated

by a null character ‘\0’.

- When allocating space for a string, remember to add an extra

character for the null terminator.

20

char str_name[size];

Page 19: 333 Section 1 Slides - courses.cs.washington.edu

char str[6] = "Hello";

Example

21

index 0 1 2 3 4 5

value ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ \0

- If using String literals, C will set it up for you

Page 20: 333 Section 1 Slides - courses.cs.washington.edu

char* str = "Hello";

Example

22

index 0 1 2 3 4 5

value ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ \0

- You can also use a pointer. C will allocate the characters in read

only memory, and the pointer will point to the first character in the

string.

0x7f..str

Page 21: 333 Section 1 Slides - courses.cs.washington.edu

Exercise 1 b

23

Page 22: 333 Section 1 Slides - courses.cs.washington.edu

void bar(char *str) { str = "ok bye!";}

int main(int argc, char *argv[]) { char *str = "hello world!"; bar(str); printf("%s\n", str); // should print "ok bye!" return EXIT_SUCCESS;}

char* str

char* str

main stack frame

static data

["hello world!\0"]

["ok bye!\0"]

bar stack frame

24

Modifying the argument str in bar will not effect str in main because arguments in C are always passed by value.

In order to modify str in main, we need to pass a pointer to a pointer (char **) into bar and then dereference it:

The following code has a bug. What’s the problem, and how would you fix it?

Page 23: 333 Section 1 Slides - courses.cs.washington.edu

Output Parameters

25

Page 24: 333 Section 1 Slides - courses.cs.washington.edu

Output Parameters

Definition: a pointer parameter used to store output in a location specified by

the caller.

Useful for returning multiple items :)

26

Page 25: 333 Section 1 Slides - courses.cs.washington.edu

Output Parameter example

Consider the following function:

void getFive(int ret){ ret = 5;}

Will the user get the value '5'?

27

No! You need to use a pointer so that the caller

can see the change

void getFive(int* ret){ *ret = 5;}

Page 26: 333 Section 1 Slides - courses.cs.washington.edu

Exercise 2

28

Page 27: 333 Section 1 Slides - courses.cs.washington.edu

char *strcpy(char *dest, char *src) {

}

How is the caller able to see the changes in dest if C is pass-by-value?

Why do we need an output parameter? Why can’t we just return an array we create in strcpy?

29

char *ret_value = dest; while (*src != '\0') { *dest = *src; src++; dest++; } *dest = '\0'; // don’t forget the null terminator! return ret_value;

If we allocate an array inside strcpy, it will be allocated on the stack. Thus, we have no control over this memory after strcpy returns, which means we can’t safely use the array whose address we’ve returned.

The caller can see the copied over string in dest since we are dereferencing dest. Note that modifications to dest that do not dereference will not be seen by the caller(such as dest++). Also note that if you used array syntax, then dest[i] is equivalent to *(dest+i).

Page 28: 333 Section 1 Slides - courses.cs.washington.edu

Exercise 3 (Bonus)

30

Page 29: 333 Section 1 Slides - courses.cs.washington.edu

31

Given the following command: “mkdir -v cats dogs” and argv = 0x1000, draw a box-and-arrow memory diagram of argv and its contents for when mkdir executes.

Page 30: 333 Section 1 Slides - courses.cs.washington.edu

32

1) argv[0]

2) argv + 1

3) *(argv[1] + 1)

4) argv[0] + 1

5) argv[0][3]

Page 31: 333 Section 1 Slides - courses.cs.washington.edu

Exercise 4 (Bonus)

33

Page 32: 333 Section 1 Slides - courses.cs.washington.edu

void product_and_sum(int *input, int length, int *product, int *sum) {

}

34

int temp_sum = 0; int temp_product = 1; for (int i = 0; i < length; i++) { temp_sum += input[i]; temp_product *= input[i]; } *sum = temp_sum; *product = temp_product;