Question: Simple C Programming Questions Please answer parts 12 through 18. Some are True or False others are multiple choice questions. No programming necessary just answer
Simple C Programming Questions
Please answer parts 12 through 18. Some are True or False others are multiple choice questions. No programming necessary just answer 12-18 to receive Positive score. If any are skipped, I will rate negatively. I have attached Problem34.c and its sample output if you need it to answer the questions.
12. T or F? (calls == &calls[0]) && (&*calls == &calls[0])
13. calls points-to a dynamically-allocated array-of (n+1) integers that resides in the heap. When sizeof(int) = 4, n = 10, and calls = 0043600016, what is &calls[n]?
A: 0043600416 B: 0043601016 C: 0043604016 D: 0043602816 E: none of these
14. Fact The amount of heap space requested for each *memoryLeak is 1 byte (that is sizeof(char)); the amount of heap space requested for **totalCalls is 4 bytes (that is sizeof(int)); and the amount of heap space requested for each *mallocThenFree is 8 bytes (that is, sizeof(double)). However, T or F? it appears that more bytes of heap are actually used (that is, allocated) than are requested each time malloc() is called.
15. The F(1) reference (that is, the function F() called with n = 1) is made
701,408,733 times for n = 44
1,134,903,170 times for n = 45
?,???,???,??? times for n = 46 and True or False?
2,971,215,072 times for n = 47.
16. How big is the activation record for each activation of function F()?
A: 6010 B: C016 C: 3016 D: 6016 E: none of these
17. Successive allocations of F() activation records appear to grow the run-time stack
A: monotonically upwards (from lower main memory locations to higher memory locations)
B: monotonically downwards (from higher main memory locations to lower memory locations)
C: randomly with no consistent direction D: not at all (that is, the run-time stack does not grow in size)
E: none of these
18. Notice, all main memory addresses are output using the format specifier "%p". How many 4-bit nibbles big is a main memory address? A: 2 B: 4 C: 8 D: 32 E: none of these
//---------------------------------------------------
// Problem #34
// Problem34.c
//---------------------------------------------------
#include
#include
#include
#include
#define TRACESTACK
#define F0 0
const int F1 = 1;
int *calls;
//---------------------------------------------------
int main()
//---------------------------------------------------
{
int F(int n);
void UnusedFunction1(void);
void UnusedFunction2();
int n;
fprintf(stdout,"n? ");
while ( fscanf(stdin,"%d",&n) != EOF )
{
int i,Fn,**totalCalls;
calls = (int *) malloc(sizeof(int)*(n+1));
totalCalls = (int **) malloc(sizeof(int *));
*totalCalls = (int *) malloc(sizeof(int));
printf(" 1) stdout = %p ",stdout);
printf(" 2) stdin = %p ",&*stdin);
printf(" 3) sizeof(FILE) = %X ",sizeof(FILE));
printf(" 4) &main() = %p ",main);
printf(" 5) &F() = %p ",F);
printf(" 6) &UnusedFunction1 = %p ",UnusedFunction1);
printf(" 7) &UnusedFunction2 = %p ",UnusedFunction2);
printf(" 8) &F1 = %p ",&F1);
printf(" 9) &calls = %p ",&calls);
printf("10) &n = %p ",&n);
printf("11) &i = %p ",&i);
printf("12) &Fn = %p ",&Fn);
printf("13) &totalCalls = %p ",&totalCalls);
printf("14) calls = %p ",calls);
printf("15) &calls[0] = %p ",&calls[0]);
printf("16) &calls[n] = %p ",&calls[n]);
printf("17) totalCalls = %p ",totalCalls);
printf("18) *totalCalls = %p ",*totalCalls);
printf("19) **totalCalls = %10d ",**totalCalls);
printf("20) &\"n? \" = %p ","n? ");
printf("21) &\"n? \" = %p ",&"n? ");
printf("22) &\"%%d\" = %p ","%%d");
for (i = 0; i <= n; i++)
calls[i] = 0;
Fn = F(n);
printf("F(%2d) = %10d ",n,Fn);
**totalCalls = 0;
for (i = 0; i <= n; i++)
{
**totalCalls += calls[i];
printf(" F[%2d] called %10d times ",i,calls[i]);
}
printf(" ============================= ");
printf(" Total calls %10d times ",**totalCalls);
free(calls);
printf("23) calls = %p ",calls);
printf("24) calls[n] = %10d ",calls[n]);
free(*totalCalls);
printf("25) *totalCalls = %p ",*totalCalls);
printf("26) **totalCalls = %10d ",**totalCalls);
free(totalCalls);
printf("27) totalCalls = %p ",totalCalls);
printf("28) *totalCalls = %p ",*totalCalls);
printf(" n? ");
}
system("PAUSE");
return( 0 );
}
//---------------------------------------------------
int F(int n)
//---------------------------------------------------
{
calls[n]++;
#ifdef TRACESTACK
{
char *memoryLeak = (char *) malloc(sizeof(char));
double *mallocThenFree = (double *) malloc(sizeof(double));
assert( memoryLeak != NULL );
printf(" n = %2d, &n = %p, memoryLeak = %p, mallocThenFree = %p ",n,&n,memoryLeak,mallocThenFree);
free(mallocThenFree);
}
#endif
if ( n == 0 )
return( F0 );
else if ( n == 1 )
return( F1 );
else
return( F(n-1) + F(n-2) );
}
//---------------------------------------------------
void UnusedFunction1(void)
//---------------------------------------------------
{
}
//---------------------------------------------------
void UnusedFunction2()
//---------------------------------------------------
{
}
Sample Program Dialog (with TRACESTACK defined)
n? 5
1) stdout = 00425A78
2) stdin = 00425A58
3) sizeof(FILE) = 20
4) &main() = 0040100A
5) &F() = 00401005
6) &UnusedFunction1 = 0040100F
7) &UnusedFunction2 = 00401014
8) &F1 = 0042301C
9) &calls = 00426794
10) &n = 0012FF7C
11) &i = 0012FF78
12) &Fn = 0012FF74
13) &totalCalls = 0012FF70
14) calls = 00430060
15) &calls[0] = 00430060
16) &calls[n] = 00430074
17) totalCalls = 00430030
18) *totalCalls = 00431FF0
19) **totalCalls = -842150451
20) &"n? " = 00423064
21) &"n? " = 00423064
22) &"%d" = 00423174
n = 5, &n = 0012FF20, memoryLeak = 00431FC0, mallocThenFree = 00431F80
n = 4, &n = 0012FEC0, memoryLeak = 00431F90, mallocThenFree = 00431F50
n = 3, &n = 0012FE60, memoryLeak = 00431F60, mallocThenFree = 00431F20
n = 2, &n = 0012FE00, memoryLeak = 00431F30, mallocThenFree = 00431EF0
n = 1, &n = 0012FDA0, memoryLeak = 00431F00, mallocThenFree = 00431EC0
n = 0, &n = 0012FDA0, memoryLeak = 00431ED0, mallocThenFree = 00431E90
n = 1, &n = 0012FE00, memoryLeak = 00431EA0, mallocThenFree = 00431E60
n = 2, &n = 0012FE60, memoryLeak = 00431E70, mallocThenFree = 00431E30
n = 1, &n = 0012FE00, memoryLeak = 00431E40, mallocThenFree = 00431E00
n = 0, &n = 0012FE00, memoryLeak = 00431E10, mallocThenFree = 00431DD0
n = 3, &n = 0012FEC0, memoryLeak = 00431DE0, mallocThenFree = 00431DA0
n = 2, &n = 0012FE60, memoryLeak = 00431DB0, mallocThenFree = 00431D70
n = 1, &n = 0012FE00, memoryLeak = 00431D80, mallocThenFree = 00431D40
n = 0, &n = 0012FE00, memoryLeak = 00431D50, mallocThenFree = 00431D10
n = 1, &n = 0012FE60, memoryLeak = 00431D20, mallocThenFree = 00431CE0
F( 5) = 5
F[ 0] called 3 times
F[ 1] called 5 times
F[ 2] called 3 times
F[ 3] called 2 times
F[ 4] called 1 times
F[ 5] called 1 times
=============================
Total calls 15 times
23) calls = 00430060
24) calls[n] = -572662307
25) *totalCalls = 00431FF0
26) **totalCalls = -572662307
27) totalCalls = 00430030
28) *totalCalls = DDDDDDDD
Sample Program Dialog (with TRACESTACK not defined)
n? 30
1) stdout = 00425A78
2) stdin = 00425A58
3) sizeof(FILE) = 20
4) &main() = 0040100A
5) &F() = 00401005
6) &UnusedFunction1 = 0040100F
7) &UnusedFunction2 = 00401014
8) &F1 = 0042301C
9) &calls = 00426794
10) &n = 0012FF7C
11) &i = 0012FF78
12) &Fn = 0012FF74
13) &totalCalls = 0012FF70
14) calls = 00431F70
15) &calls[0] = 00431F70
16) &calls[n] = 00431FE8
17) totalCalls = 00430080
18) *totalCalls = 00430050
19) **totalCalls = -842150451
20) &"n? " = 00423444
21) &"n? " = 00423444
22) &"%d" = 00423198
F(30) = 832040
F[ 0] called 514229 times
F[ 1] called 832040 times
F[ 2] called 514229 times
F[ 3] called 317811 times
F[ 4] called 196418 times
F[ 5] called 121393 times
F[ 6] called 75025 times
F[ 7] called 46368 times
F[ 8] called 28657 times
F[ 9] called 17711 times
F[10] called 10946 times
F[11] called 6765 times
F[12] called 4181 times
F[13] called 2584 times
F[14] called 1597 times
F[15] called 987 times
F[16] called 610 times
F[17] called 377 times
F[18] called 233 times
F[19] called 144 times
F[20] called 89 times
F[21] called 55 times
F[22] called 34 times
F[23] called 21 times
F[24] called 13 times
F[25] called 8 times
F[26] called 5 times
F[27] called 3 times
F[28] called 2 times
F[29] called 1 times
F[30] called 1 times
=============================
Total calls 2692537 times
23) calls = 00431F70
24) calls[n] = -572662307
25) *totalCalls = 00430050
26) **totalCalls = -572662307
27) totalCalls = 00430080
28) *totalCalls = DDDDDDDD
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
