Simple C Programming Questions Please answer parts 19 through 25. Some are True
ID: 3753122 • Letter: S
Question
Simple C Programming Questions
Please answer parts 19 through 25. Some are True or False others are multiple choice questions. No programming necessary just answer 19-25 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.
19. How many levels of indirection does the expression **totalCalls contain?
A: 0 B: 1 C: 2 D: 3 E: none of these
20. The assertion in the function F() will fail when heap space is exhausted. When an assertion fails (that is, when the assertion’s expression evaluates to false), the failing expression is displayed along with the name of the source file that contains the assertion and the line number of the source file that contains the assertion. T or F? assert() is a macro whose definition depends on the conditional operator ? :. Hint You will need to study the header file <assert.h>.
21. Assume int *p = (int *) malloc(sizeof(int)); What can you confidently say about deallocation from the heap using the pointer p, free(p), based on your observations of Problem34.c output? After free(p),
A: the value of p is changed and the value of the data p pointed-to is changed
B: the value of p is unchanged but the value of the data p pointed-to is changed
C: the value of p is changed but the value of the data p pointed-to is unchanged
D: the value of p is unchanged and the value of the data p pointed-to is unchanged E: none of these
22. Early-in-program-execution of successive dynamic-allocations from the heap appear to “grow” the heap
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: usually downwards E: none of these
23. When the macro TRACESTACK is defined, how many bytes of memory “leaks” each time the function F() is called?
A: 3010 B: 3016 C: 3210 D: 4016 E: none of these
24. T or F? When the macro TRACESTACK is not defined, the function F() does not do any dynamic- allocation/dynamic-deallocation from the heap.
25. T or F? The formal parameter list of the function UnusedFunction1(), that is (void), and the formal parameter list of the function UnusedFunction2(), that is (), are semantically-equivalent (that is, they mean exactly the same thing).
//---------------------------------------------------
// 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
Explanation / Answer
19). The answer is option c i.e. 2 ( it is the count of the * i.e. indirection operator).
20). The answer is true
21). The answer is A, (the value of p is changed and the value of the data p pointed-to is changed).
22). the answe is C (Heaps only "grow" in a direction in very naive implementat, the direction a stack grows is , it always goes toward smaller addresses "i.e. 'Up'")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.