Description
- Consider the following code:
| #include <stdio.h>
int main(int argc, char *argv[]) { int x = 32; int *y = &x; int z = *y; printf(“z = %d\n”, z); return 0; } |
1
2
3
4
5
6
7
8
9
- What is the output?
- What is the value of y?
- In what memory (static, stack, or heap) do the variable x, y, z exist during runtime?
- Consider the following code:
| #include <stdio.h>
int main(int argc, char *argv[]) { int *x = (int *)200; long z = (long)x; printf(“z = %d\n”, z); return 0; } |
1
2
3
4
5
6
7
8
- What is the output?
- In what memory (static, stack, or heap) do the variable x, z exist during runtime? (c) Why is this code bad practice (even though it compiles/runs without a seg fault)?
- Consider the following code:
| #include <stdio.h>
int main(int argc, char *argv[]) { int *x = new int[100]; x[0] = 500; int z = x[25]; printf(“z = %d\n”, z); return 0; } |
1
2
3
4
5
6
7
8
9
(a) What is the output? (Careful, a compile/run will not give you the correct answer.) (b) In what memory (static, stack, or heap) do the variable x, z exist during runtime?
- What is stored on the heap?
- What is the memory issue?
- Consider the following code:
| #include <stdio.h>
int main(int argc, char *argv[]) { int *x = new int[100]; int *y = x+10; for (int i = 0; i < 100; i++) { x[i] = i; } printf(“y[10] = %d\n”, y[10]); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
- Why is the output?: y[10] = 20
- Write a line of code that would free up memory using only x.
- Write a line of code that would free up memory using only y.
- Consider the following code:
| #include <stdio.h>
int main(int argc, char *argv[]) { int *x = new int[100]; int *y = new int[100]; int **z = NULL; for (int i = 0; i < 100; i++) { x[i] = i; y[i] = 100-i; } z = &x; printf(“(*z)[10] = %d\n”, (*z)[10]); z = &y; printf(“(*z)[10] = %d\n”, (*z)[10]); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- What is the value of x and y?
- What is the value z on lines 14 and 16?
- What does the code fragment “(*z)[10]” mean? (Describe what the code must do the evaluate that code fragment.)
- Why are there two different outputs for “(*z)[10]”? Here is the output of the program:
(*z)[10] = 10
(*z)[10] = 90
- Write the commands to free memory.
- Consider the following code:
| #include <stdio.h> #include <stdlib.h>
class Student { public: int mId; double mGPA; }; int main(int argc, char *argv[]) { Student *students = new Student[100]; Student ** studentsPtr = new Student*[100]; srand(100); // Seed random number generator for (int i = 0; i < 100; i++) { students[i].mId = i+1; // Generate a “random” GPA from 0.0-4.0 students[i].mGPA = 4 * (((double)rand())/RAND_MAX); studentsPtr[i] = students+i; } // This is Bubble Sort: for (int i = 0; i < 100; i++) { for (int j = 1; j < 100; j++) { // Based on GPA if (studentsPtr[j-1]->mGPA > studentsPtr[j]->mGPA) { Student *temp = studentsPtr[j]; studentsPtr[j] = studentsPtr[j-1]; studentsPtr[j-1] = temp; } } } for (int i = 0; i < 100; i++) { printf(“%f\n”, studentsPtr[i]->mGPA); } return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
- What is the value of students?
- What is the value of studentsPtr?
- Which of the above is being sorted?
- Write the code to sort students without nesting for loopss. (Hint: use studentsPtr.) (e) Write the commands to free memory.




