Consider following program. Note that execution begins at “main”. int *a; int b;
ID: 3885863 • Letter: C
Question
Consider following program. Note that execution begins at “main”.
int *a;
int b;
int f = 4;
void DoFunctionB(int in1, int* in2) {
f++; // Accesses the global variable
in2 = malloc(500 * sizeof(int*)); // Dynamically allocate some int*’s
*in2 = in1;
}
int DoFunctionA(int in1, int* in2) {
for(c=0;c++;c<128){
DoFunctionB(in1++,in2++); // Call DoFunctionB with the passed-in args
}
i = in2;
return(in1);
}
void main( void ) {
int c=4;
int *d, *e;
d = malloc(500 * sizeof(int)); // Dynamically allocate some int’s
e = malloc(500 * sizeof(int)); // Dynamically allocate some int’s
b = DoFunctionA(c,a); // Pass some variables into DoFunctionA
}
1- For each variable (a-f) describe whether the variable itself is global or local, static or not static, and initialized or uninitialized.
2- For each variable (a-f), describe where it is located when DoFunctionB is running. That is, for each variable, describe whether it is on the stack, in statically allocated RAM, or on the heap. (Note that, to get to DoFunctionB, main and DoFunctionA must also be running, and malloc has already been called. Assume that when main calls DoFunctionA and when DoFunctionA calls DoFunctionB, all non-static local variables in main and DoFunctionA are pushed onto the stack. Assume all calls to malloc are successful.) For the pointers, consider both the pointer itself and consider the memory region that each pointer “points to”.
3- Assume that an “int” and an “int *” are both 4 bytes (32 bits) in size. When DoFunctionB is running (and its call to malloc has been made), how much space is in use in RAM? Consider the allocated RAM area, the heap, and the stack.
Explanation / Answer
And hence a total of 6028 bytes are used in RAM.
i.e. (4)*4 + (500*4)*2 + (4)*2 + (4) + (500*4) = 6028 bytes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.