Week 2 (Functions, Strings, Arrays, PRNs) Flashcards

1
Q

Which security flaw are arrays in C vulnerable to?

A

Buffer Overflow since there is the capability to exceed boundary of contiguous memory assigned to the array.

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

What happens if an array is declared with a size N, but is not initialised?

A

The array is assigned contiguous memory from the heap, and elements are then populated randomly with 0’s or other random values.

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

What happens if an element is added to the array at an index that surpasses the arrays boundaries?

A

The value is assigned to a memory address outside of the contiguous memory that was assigned to an array.

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

How many bytes does a memory address traditionally accomodate?

A

1 byte.

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

How do you calculate the memory needed for an array?

A

You multiple the data type size in bytes by the number of elements in the array. For example, int arr[10] -> 4 *10 = 40 bytes aka 40 memory locations.

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

For a 2D array in C, int arr[2][3], which subscript bracket represents the rows and which represents the columns? How would you access the 2nd element on the bottom row?

A

[2] -> Row
[3] -> Column

arr[1][1]

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

How is a 2D array stored in memory?

A

Contiguously by its columns.

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

What are the 5 important string functions?

A

strlen -> Returns the length
strcpy -> Copies arg 2 to arg 1
strcat -> Concats arg 2 to arg 1
strcmp -> Compares for equality
sprintf -> Places result in buffer

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

Why are Pseudo Random Numbers not truly random?

A

Because they are generated via an algorithm, thus will eventually repeat.

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

What are the 5 Random Number Distributions?

A

Uniform -> All values are distributed evenly.
Gaussian -> The bell curve, a result of the central limit theorem.
Poisson -> The distribution of a count.
Bernoulli -> The distribution of 2 mutually exclusive events. E.g tossing a coin.
Gamma -> A distribution of positive values only.

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

Which distribution does the rand( ) function perform?

A

Uniform -> We know it is algorithmic and not truly random because if we continuously generate rand( ), the output will eventually repeat.

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

What is a way we can try and improve randomness?

A

Using seeding -> Set a random seed number to operate on the rand( ) output. For example, a seed can be the current time.

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

What does a function definition consist of?

A

Function header and function body.

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

What are the 4 main aspects of C functions?

A
  • Can’t overload
  • All functions are pass-by-value
  • Functions that return no value are procedures
  • If a function header doesn’t specify a type, it assumes an int is returned.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of a functional prototype?

A
  • Improve documentation
  • Assists compiler in checking code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why does rand( ) always give the same numbers? How does srand( time(NULL) ) solve this?

A

rand( ) returns a PRN which is algorithmic and always starts at the same numbers.
srand( ) seeds this with ideally a unique number such as current time or PID.

17
Q

What are 2 good seeds for srand( ) ?

A

The current PID.
The current time.