Final Review Flashcards

1
Q

%f (default _ digits after decimal point)

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

displays 2 digits after the decimal point

A

%.2f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

char: format specifier _, size: _ (normally)

A

%c 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

int: format specifier _, size: _ (normally)

A

%d 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

double: format specifier _, size: _ (normally)

A

%lf 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

character constants are enclosed

in single quotes or double quotes?

A

single

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
char ch;
int i;
i = 'a'; // what's i?
ch = 65; // A
ch = ch + 1; // what's ch?
A

i is 97, ch is C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

range of char type

A

-128,127

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

double m = 5/6; /* int / int = int */

printf(“Result of 5/6 is %f\n”, m);

A

Result of 5/6 is 0.000000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

double n = (double)5/6; /* double / int = double */

printf(“Result of (double)5/6 is %f\n”, n);

A

Result of (double)5/6 is 0.833333

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

double o = 5.0/6; /* double / int = double */

printf(“Result of 5.0/6 is %f\n”, o);

A

Result of 5.0/6 is 0.833333

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

int p = 5.0/6; /* double / int = double but then
converted to int */
printf(“Result of 5.0/6 is %d\n”, p);

A

Result of 5.0/6 is 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Memory model:

Logical address: 0 -> 2^32 - 1

A

Code, Static Data, Dynamic Data(Heap)->, <-Stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Memory model:
int x = 10;
int y;
int f(int p, int q) {
int j = 5;
return p * q + j;
}
int main() {
int i = x;
y = f(i, i);
return 0;
}
A

w2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Initializing arrays

A

int a[10]

char letters[4] = {‘a’, ‘q’, ‘e’, ‘r’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
int i = 19;
int *p;
int *q;
*p = i;  // valid?
q = &i  // valid?
A
*p = i; /*error*/
q = &i /* valid */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
What's c?
char *cptr;
char c = 'a';
cptr = &c;
*cptr = 'b';
A

b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
What does this do?
int a[3] = {1, 3, 5};
int *p;
p = a; 
*p = 10;
p[1] = 4;
*(p + 2) = 3;
A
a[0] = 10
a[1] = 4
a[2] = 3;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Passing Arrays as Parameters:
int i[3] = {10, 9, 8};
sum(i);
int sum( ? ) ;

A
int i[3] = {10, 9, 8};
sum(i);
int sum(int *a, int size);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
Multi-dimensional arrays
int a[3][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
int *x = ?; // assign a to x.
x[i][j] == ?
A

Arrays in C are stored in row-major order
int *x = (int *)a;
x[i][j] == *(x + i * n + j)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
Passing by value / reference
int a = 5;
int *p = malloc(sizeof(int));
*p = 5;
void increment (int* x) {
(*x)++;
}
increment(?); // pass a
increment(?); // pass p
A

increment(&a);

increment(p);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
Dynamic allocation:
Draw memory model
int x = 2;
int a[4];
int *b;
int main() {
b = malloc(4 *
sizeof(int));
b[0] = 10;
b[1] = 20;
}
A

w3 p13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
Dangling pointers
int *a = malloc(10 * sizeof(int));
...
free(a);
printf("%d\n", a[0]);
A

Dereferencing a pointer after the memory it
refers to has been freed is called a “dangling
pointer”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

void *memcpy(void *dest, const void *src, size_t n);

A

DESCRIPTION
The memcpy() function copies n bytes from
memory area src to memory area dest. The memory areas
must not overlap. Use memmove(3) if the memory areas do
overlap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
common errors: – forgetting to allocate memory when a pointer is declared – dereferencing a pointer after it’s been free’d – losing track of a memory block without free’ing (memory leak)!
w3
26
Struct
struct student { char *name; int age; };
27
File Operations: | fopen, fread, fwrite, fprintf, fgets, fscanf, fclose.
Remember to use fclose to close each file | when done.
28
``` char *fgets(char *s, int size, FILE *stream); reads the next line from a file pointer – It reads at most _?_ characters – Reading stops after a -?- – eliminate the ‘\n’ or not? – The end of the string? ```
``` reads the next line from a file pointer – It reads at most size -1 characters – Reading stops after a newline or EOF – Does not eliminate the ‘\n’ ! – Appends a ‘\0’ character at the end of the string. ```
29
FILE* f = fopen(...); | fprintf(f, “My name is %s\n”, myname);
w3
30
``` Open a file: FILE *fopen(const char *filename, const char *mode); Close: void fclose(FILE *stream); ```
FILE *fp; fp = fopen(argv[1], "r"); "r" Open for reading "w" Open for writing (file need not exist) "a" Open for appending (file need not exist) "r+" Open for reading and writing, starting at beginning "w+" Open for reading and writing (truncate if file exists) "a+" Open for reading and writing (append if file exists)
31
strchr(); | strrchr();
``` #include char *strchr(const char *s, int c); The strchr() function returns a pointer to the first occurrence of the character c in the string s. ``` ``` char *strrchr(const char *s, int c); The strrchr() function returns a pointer to the last occurrence of the character c in the string s. ```
32
Compare: fgrets, fscanf; fread, fwrite;
Recall that fgets, fscanf will read characters. • By contrast, fread and fwrite operate on bytes.
33
``` size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); ``` ``` size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); ```
read nmemb * size bytes into memory at ptr write nmemb * size bytes from ptr to the file pointer stream n = fwrite(&num, sizeof(num), 1, fp);
34
``` Moving around a file int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream); void rewind(FILE *stream); ```
``` • set the file position for stream • add offset bytes to the position specified by whence • whence can be – SEEK_SET - beginning of file – SEEK_CUR - current position in file – SEEK_END - end of file ```
35
DIR *opendir(const char *filename); struct dirent *readdir(DIR *dirp); closedir(dp);
w3
36
Command-Line Arguments | argc, argv
argc (“argument count”) is the number of command-line arguments. argv[0] points to the name of the program, while argv[1] through argv[argc-1] point to the remaining command-line arguments. • argv[argc] is always a null pointer - a special pointer that points to nothing.
37
char **p; for (p = &argv[1]; *p != NULL; p++) printf("%s\n", *p);
w3
38
– length is the number of non-null characters | currently present in the string
size is the amount of memory allocated for | storing the string
39
Duplicating a string
strdup allocates memory automatically | char * str2 = strdup(str1);
40
Concatenating strings
char *strncat(char *s1, const char *s2, size_t n); – appends the contents of string s2 to the end of s1, and returns s1.
41
Comparing strings
``` int strcmp(const char *s1, const char *s2) Careful: if (str1 == str2) - compares the pointers (the addresses), NOT the string contents!! ```
42
Pointers to Functions
``` int cube(int x) { return x*x*x; } int (*f)(int); f = cube; printf ("%d\n", (*f)(5)); ```
43
``` void map(int *nums, int size, int (*f)(int)) { int i; for(i = 0; i < size; i++) nums[i] = (*f)(nums[i]); } ```
map(a, 8, square);
44
``` Changing the pointer void increment (int **x) { int * p = malloc (sizeof(int)); *p = 13; // assign a value in the location where p points to *x = p; // make *x point to the same addr where p points // no return, void } ``` *a = 5; increment(&a);
int* increment (int *x) { int * p = malloc (sizeof(int)); *p = 13; // assign a value in the location where p points to x = p; // now make x point to the same addr where p points return x; } ``` *a = 5; a = increment(a); ```
45
extern:
``` informs the compiler that i is an int variable, but doesn't cause it to allocate space. ```
46
header
#include "list.h"
47
``` Makefiles target first prerequistite all out of date prerequisites all prerequisites ```
$@ $? $^
48
-c flag
-c flag in gcc does compilation of file | without linking.
49
Compile the object and lin
gcc –c main.c gcc –c list.c gcc -o program main.o list.o
50
Process: fork() getpid() getppid()
Return: -1: error 0: in child process PID: in parent process, pid is child's.
51
Differences between parent and child
pid, ppid pending alarms cleared for child pending signals are cleared for child address space
52
A terminating process sends its parent a ? | signal
SIGCHILD
53
Orphan | process:
is a process whose parent died | is adopted by the init process (PID 1)
54
wait() = waitpid(-1, &status, 0);
pid_t wait(int *status)
55
WIFEXITED(status)
if( WIFEXITED ( status ) ) printf("normal termination\n");
56
pid_t waitpid(pid_t pid, int *status, int options);
pid = -1: wait for any child option == WNOHANG: non-blocking option == 0: blocking
57
orphan process | zombie process
A child process whose parent process terminates before it does becomes an orphan process. A child process that terminates but is never waited on by its parent becomes a zombie process.
58
exec()
On success, exec never returns | On failure, returns -1
59
system call to convert a FILE object to a fd:
int fileno(FILE *fp)
60
Assign a stream interface to a file descriptor
FILE *fdopen(int fd, const char *mode);
61
``` Often we want the stdout of one process to be connected to the stdin of another process ```
Set one FD to the value of another. returnCode = dup2(oldFD, newFD); ``` int fd[2], pid; int filedes = open ("file1", O_RDONLY); dup2 (filedes, fileno(stdin)); close (filedes); pipe (fd); ```
62
pipe()/dup2() Example p33
``` if((pid = fork ()) == 0) {/* child */ dup2 (fd[1], fileno(stdout)); close (fd[0]); close (fd[1]); execl ("/usr/bin/sort", "sort", (char *) 0); } else if(pid > 0){ /* parent */ dup2 (fd[0], fileno(stdin)); close (fd[1]); close (fd[0]); execl ("/usr/bin/uniq", "uniq", (char *) 0); } else { perror("fork"); exit(1); } ```
63
popen() | simplifies the sequence of:
```  generating a pipe  forking a child process  duplicating file descriptors  passing command execution via an exec() ```
64
FILE *pipeFP; | pipeFP = popen("/usr/bin/ls *.c", "r")
"w” – write data as input (stdin) to command | "r” – read input from command’s stdout
65
TCP/IP | Transmission Control Protocol
soucre address, dest. address bytes, ack, port data
66
sin_addr can be set to
INADDR_ANY
67
``` Create a listen socket Create an sockaddr_in address bind listen accept ```
int listenfd; struct aockaddr_in addr; listenfd = socket(AF_INET, SOCK_STREAM, 0); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(PORT); bind(listenfd, (struct sockaddr *)&addr, sizeof(addr); listen(listenfd, 1); struct sockaddr_in q; accept(listenfd, (struct sockaddr *)&q, &len);
68
Select():
``` fd_set allset, readset; struct timeval timeout; timeout.tv_set = 0; timeout.tv_usec = 0; FD_ZERO(&allset); FD_SET(listenfd, &allset); readset = allset; select(maxfd+1, &readset, NULL, NULL, &timeout); if(FD_ISSET(listenfd, &readset)) ```
69
``` int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout); ``` struct timeval;
NULL, wait forever | 0, test and return immediately;
70
Single quotation marks, as in the preceding example, will always get you exactly what's inside the quotation marks--any characters that might otherwise have special meaning to the shell (like the dollar sign or the backslash) are treated literally. Use double quotation marks when you want to assign a string that contains special characters the shell should act on. The backslash is used to escape (treat literally) a single character (such as $ or *) that might otherwise be treated as a special character by the shell.
howdy='Good Morning $USER !' echo $howdy Good Morning $USER ! howdy="Good Morning $USER !" echo $howdy Good Morning hermie ! costmsg="Price is $5.00" echo $costmsg Actual result: Price is .00 $ costmsg="Price is \$5.00" $ echo $costmsg Actual result: Price is $5.00
71
iters="1 2 3 4" for i in $iters; do echo $i done
for i in 1 2 3 4; do echo $i done
72
``` • Double quotes inhibit wildcard replacement only. • Single quotes inhibit wildcard replacement, variable substitution and command substitution. • Back quotes cause command substitution. ```
$ echo "Today is `date`" • Today is Thu Sep 19 12:28:55 EST 2002 * $ echo ’Today is `date`’ * Today is `date`
73
``` if test ! -d notes then echo not found else echo found fi ```
if [ ! -d notes ] then ... ``` -z: true is empty string str1 = str2 int1 -eq int2 -ne, -gt, lt, le -a: and -o: or ```
74
If statements just check the return value of | the command.
test is just a command that returns a value.
75
Command line arguments
chmod u+x myscript $0, $1, $2 $* == $1, $2
76
Positional Parameters | $0, $#, $*, $@, "$*", "$@",
``` $0 Name of the script $# Number of positional parameters $* Lists all positional parameters $@ Same as $* except when in quotes “$*” Expands to a single argument (“$1 $2 $3”) “$@” Expands to separate arguments (“$1” “$2” “$3”) $1 .. $9 First 9 positional parameters ${10} 10th positional parameter ```
77
set and shift
set – assigns positional parameters to its arguments. – $ set `date` – $ echo "The date today is $2 $3, $6" – The date today is May 25, 2006
78
shift – change the meaning of the positional | parameters
``` #!/bin/sh while test "$1" do echo $1 shift done $ giant2 fee fie fo fum fee fie fo fum giant2 1 ```
79
expr
x=1 x=`expr $x + 1` y=`expr 3 \* 5` y = $((3*5))
80
expr $string : $substring
Returns the length of matching | substring at beginning of string.
81
``` read #!/bin/sh echo "Enter your name:" read fName lName echo "First: $fName" echo "Last: $lName" ```
``` $ name Enter your name: Alexander Graham Bell First: Alexander Last: Graham Bell ```
82
Reading from a file
while read line do echo $line done < $file
83
You can create your own functions or | subroutines:
``` myfunc() { arg1=$1 arg2=$2 echo $arg1 $globalvar return 0 } ```
84
``` int pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*func)(void*), void *arg); ```
pthread_t thread_ID; int fd, result; fd = open(“afile”, “r”); result = pthread_create(&thread_ID, NULL, myThreadFcn, (void *)&fd); ``` void *myThreadFcn(void *p) { tdata *mydata = (tdata *) p; write(mydata->fd, mydata->name, 7); close(mydata->fd); free(mydata); return NULL; } ```
85
``` int pthread_join(pthread_t tid, void **status) ```
join