首页 > > 详细

辅导data编程、辅导C++程序、c/c++语言编程调试解析Java程序|辅导数据库SQL

1 [5 marks] Memory and Numbers. Consider the execution of the following code on a little-endian processor.
Assume that the address of i is 0x1000 and that the compiler allocates the three global variables contiguously, in
the order they appear, wasting no memory between them other than what is required to ensure that they are properly
aligned (and assuming that char’s are signed, and int’s and pointers are 4-bytes long).
1a For every byte of memory whose value you can determine from the information given above, give its value in
hex on the line labeled with its address. For addresses whose value you can not determine, check the Unknown
box.
0x1000: Unknown or Value ____________
0x1001: Unknown or Value ____________
0x1002: Unknown or Value ____________
0x1003: Unknown or Value ____________
0x1004: Unknown or Value ____________
0x1005: Unknown or Value ____________
0x1006: Unknown or Value ____________
0x1007: Unknown or Value ____________
0x1008: Unknown or Value ____________
0x1009: Unknown or Value ____________
0x100a: Unknown or Value ____________
0x100b: Unknown or Value ____________
2
2 [10 marks] Global Variables and Arrays. Answer the following questions about global variables.
int i;
int a[5];
int *b;
2a Give assembly code for the C statement: i = a[2].
2b Give assembly code for the C statement: b[0] = a[b[i]];.
2c How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b[a[3]] = a[3] + a[i];
0 1 2 3 4 5 6 7 8
3
3 [10 marks] Structs and Instance Variables. Answer the following questions about the global variables a and b,
declared as follows. Assume that int’s and pointers are 4-bytes long.
struct A {
char x;
int y[3];
struct B* z;
};
struct A* a;
struct B b;
struct B {
struct A j;
int k;
};
Treat each sub-question separately (i.e., do not use values computed in prior sub-questions). Comments are not
required, but they will probably help you.
3a Give assembly code for the C statement: b.k = a->z[2].k;.
3b How many memory reads are required to execute the statement? Fill in a single multiple choice option below.
b.k = a->z->j.z->j.y[2];
0 1 2 3 4 5 6 7 8
4
4 [10 marks] Static Control Flow. Answer the following questions on static control flow.
4a Give assembly code for the following. Assume x and y are global integer variables.
if (x > 0 && x < 10) {
y = x;
} else {
y = 0;
}
4b Consider a procedure with the following signature: int foo(int a, int b);
Give assembly code for the following.
x = foo(x, y);
5
5 [10 marks] C Pointers. Consider the following global variable declarations.
int a[4] = (int[4]){5, 4, 3, 2};
int b[3] = (int[3]){6, 7, 8};
int* c;
Assume that the address of a is 0x1000, the address of b is 0x2000, and the address of c is 0x3000. Now, consider
the the execution of the following additional code.
c = &a[3];
b[*c] = a[1];
c = (b+1);
*c = *c + 1;
*c = *c + 1;
a[3] = c[1];
Indicate the value of each of the following expressions (i.e., the values of a, b, and c) below by checking Unchanged
if the execution does not change the value or by entering its new value in the space provided. Assuming that int’s
are 4-bytes long. Answer these questions about the value of these variables following the execution of this code.
a[0]: Unchanged or Changed to Value ____________
a[1]: Unchanged or Changed to Value ____________
a[2]: Unchanged or Changed to Value ____________
a[3]: Unchanged or Changed to Value ____________
b[0]: Unchanged or Changed to Value ____________
b[1]: Unchanged or Changed to Value ____________
b[2]: Unchanged or Changed to Value ____________
c: Unchanged or Changed to Value ____________
6
6 [12 marks] Dynamic Allocation. Consider each of the following pieces of C code to determine whether it contains
(or may contain) a memory-related problem. Label each of the following code snippets as one of the following:
A: There is no memory leak or dangling pointer; nothing needs to be changed with malloc or free.
B: There is no memory leak or dangling pointer, but the code would be improved by moving malloc or free.
C: There is a possible memory leak that is best resolved by adding, removing or moving malloc or free.
D: There is a possible memory leak that is best resolved by adding reference counting.
E: There is a possible dangling pointer that is best resolved by adding, removing or moving malloc or free.
F: There is a possible dangling pointer that is best resolved by adding reference counting.
You can assume that the starting point for each snippet of code is a call to foo, and that copy is in a different module.
Do not fix any bugs; for each part, fill in a single multiple choice bubble based off of the options above. Most of the
code snippets are very similar. Changes from previous versions and/or key things to look for in bold font.
6a int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
free(d);
}
A B C D E F
6b int* copy(int s) {
int* d = malloc(sizeof(int));
*d = s;
free(d);
return d;
}
void foo (int s) {
int* d = copy(s);
printf("value is %d", *d);
}
A B C D E F
6c void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int d = 0;
copy(s, &d);
printf("value is %d", d);
}
A B C D E F
7
6d void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
free(d);
printf("value is %d", *d);
}
A B C D E F
6e void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(d);
free(d);
}
A B C D E F
6f void copy(int s, int* d) {
*d = s;
}
void foo (int s) {
int* d = malloc (sizeof(int));
copy(s, d);
printf("value is %d", *d);
process(*d);
free(d);
}
A B C D E F
8
7 [8 marks] Reference Counting. Consider the following code that is implemented in three independent modules
(and a main module) that share dynamically allocated objects that should be managed using reference counting. The
call to rc_malloc has been added for you; recall that rc_malloc sets the allocated object’s reference count to 1.
7a What does this program print when it executes?
7b Add calls to rc_keep_ref and rc_free_ref to correct implement reference counting for this program
(all modules).
7c Assuming this program implements reference counting correctly, give the reference counts (number of pointers
currently pointing to) the following two objects when printf is called from main?
*p:
*q:
7d Add code at point TODO so that the program is free of memory leaks and dangling pointers. Your code
may call any of the procedures shown here as well as rc_free_ref. It may not directly access the global
variable b_values (note that this variable is not listed in the “header file contents” section of the code and
so it would not be in scope in main if the modules were implemented is separate files).
9
/***********
* Module a
*/
int* a_create(int i) {
int* value = rc_malloc(sizeof(int));
*value = i;
return value;
}
/**********
* Module b
*/
#define B_SIZE 4
int* b_values[B_SIZE];
void b_init() {
for (int i=0; i

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!