Week 2 (Functions, Strings, Arrays, PRNs) Flashcards
Which security flaw are arrays in C vulnerable to?
Buffer Overflow since there is the capability to exceed boundary of contiguous memory assigned to the array.
What happens if an array is declared with a size N, but is not initialised?
The array is assigned contiguous memory from the heap, and elements are then populated randomly with 0’s or other random values.
What happens if an element is added to the array at an index that surpasses the arrays boundaries?
The value is assigned to a memory address outside of the contiguous memory that was assigned to an array.
How many bytes does a memory address traditionally accomodate?
1 byte.
How do you calculate the memory needed for an array?
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.
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?
[2] -> Row
[3] -> Column
arr[1][1]
How is a 2D array stored in memory?
Contiguously by its columns.
What are the 5 important string functions?
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
Why are Pseudo Random Numbers not truly random?
Because they are generated via an algorithm, thus will eventually repeat.
What are the 5 Random Number Distributions?
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.
Which distribution does the rand( ) function perform?
Uniform -> We know it is algorithmic and not truly random because if we continuously generate rand( ), the output will eventually repeat.
What is a way we can try and improve randomness?
Using seeding -> Set a random seed number to operate on the rand( ) output. For example, a seed can be the current time.
What does a function definition consist of?
Function header and function body.
What are the 4 main aspects of C functions?
- 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.
What is the purpose of a functional prototype?
- Improve documentation
- Assists compiler in checking code