Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CMPSC 311 Exam 2

March 28, 2014

1    Short Questions (36pts total, 3pts each, be brief)

1.  How does C know how long a string is?

2.  The variable x contains the value 0x2468abcd. How would this be stored in:

Big-endian:   

Little-endian:                                               

3. What is the return value from the main function used for?

4.  Name two kinds of variables which are typically allocated on the stack.

5. It is good practice to do what to a pointer after calling free?

6. What is a memory leak?

7.  Unix-like systems allow you to take part of a ile and treat it directly as part of a process’s virtual address space. What is this mechanism called?

8. Write an egrep-style regular expression that matches in the word try” but not in the words “tryout” or pastry” .

9. What is an assertion?

10. What is the GDB command that shows you the current list of stack frames (function calls)?

11. Write a C statement that prints the string Oh no!” to the standard error stream.

12. If the current working directory is /home/user/docs, what is a relative path that refers to the ile /home/user/src/code.c?

2    Medium Questions (34pts total)

13.  (6pts) In C, what is the value of the expression 10 | 6? What is the value of the expression 10 || 6?

14.  (6pts) Write a shell command or snippet (not C) which tries to print the ile first.txt to

standard output, and if that fails, prints the ile second.txt to standard output instead.     Bonus: write two more shell commands or snippets that also do this, each in a diferent way.

15.  (6pts) In the C language, an identiier” is the name of a variable or function. Valid identiiers must start with a letter or underscore character (  ), followed by any combination of letters, numbers, and underscores.  Write an egrep-style regular expression that matches valid C identiiers.

16.  (6pts) What will the command

echo echo echo | bash

do? Explain why.

17.  (5pts) What is the diference between the strcpy and the strncpy functions? Which should you use when dealing with untrusted input, and why?

18.  (5pts) What does newly allocated memory contain immediately after a call to calloc? What does it contain immediately after a call to malloc?

3    Long Questions (30pts total, 10pts each)

19.  Fill out the following two box-and-arrow diagrams to describe the state of the program at Point A and Point B labeled in the code below. Be sure to include the values of any integers (for a pointer the arrow is sucient).

uint32_t x[3] = {4, 6, 8};

uint32_t *p = &x;

uint32_t **p2p = &p;

// Point A

p++;

(*p2p)++;

(*p)++;

// Point B

p2p

p

x[3]

x[2]

x[1]

x[0]

Point A

 

 

 

 

 

 

p2p

p

x[3]

x[2]

x[1]

x[0]

Point B

 

 

 

 

 

 

Note that the positive direction in these diagrams is up.

20. We discussed three speciic types of allocation that take place in C. For each of these three types, give  (1) the name  (e.g.,  “static allocation”),  (2) the segment in a process’s virtual address space where the allocation typically happens, and (3) the lifetime of variables/memory allocated in that way.

21.  The make  instruction function should take four individual pieces of data, pack them into a single 32-bit hardware instruction, and return the result. These pieces of data correspond to bits within the instruction as shown in the following table:

Bits

Width

Field

0–5

6

Opcode

6–9

4

Register number

10– 19

10

Address

20–31

12

Immediate value

Fill out the rest of the make  instruction function.

uint32_t make_instruction(int opcode, int reg, int addr, int immed)

{