Explain what is meant by a "Buffer-Overflow Attack". What kind of code is susceptible to such an attack? How can you re-write your code to protect it from this threat?
int *readArray(int size)
to dynamically allocate an array of appropriate size and populate it with numbers from standard input.
int main(int argc, char *argv[]){ int size = 0; scanf("%d", &size); return 0; } int *readArray(int size){ }
What is wrong with the following example
int main(int argc, char *argv[]){ int size = 0; scanf("%d", &size); int *arr = createArray(size); return 0; } int *createArray(int size){ int new_array[size]; return new_array; }
x
and y
:
typedef struct point Point; struct point { double x; double y; };For each of these two representations, write a function which, given two points
p
and q
,
returns the distance between them:
double distA( double p[2], double q[2] ); double distS( Point p, Point q );Do you find one version of the function "easier to read" than the other?
struct _student
with attributes int zid
and double height
. Create an array of these structs and populate each struct with infromation from standard input.
If time allows:
tallest_student
that takes in an array of student structs and returns zid of the tallest student.
allocate_string
to dynamically allocate read in strings.
char * allocate_string(char * current); int main(int argc, char *argv[]){ char buffer[100]; char *strings[100]; int count = 0; while(fgets(buffer,100,stdin)!= NULL && count < 100){ strings[count] = allocate_string(buffer); count++; } int i; for(i = 0; i < count; i++){ printf("%s\n",strings[i]); free(strings[i]); } return 0; } char * allocate_string(char *current){ }What is the benefit of dynamically allocating strings over having a static array char strings[100][100]?