Question
Simple C Programming Questions Please answer parts A through L. Some are True or False others are multiple choice questions. No programming necessary just answer
Simple C Programming Questions
Please answer parts A through L. Some are True or False others are multiple choice questions. No programming necessary just answer the 10 parts (A through L) 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.
PartA.
T or F? stdout is a macro defined as a pointer-to (the main memory address-of) the 1st element of (at index 0 of) an array-of FILE variables.
PartB
(Continuing 1) T or F? sizeof(FILE) is 2016 = 3210.
PartC
(Continuing 1) T of F? FILE is the name of a struct type.
PartD
(Continuing 1) How many elements does an object or instance of a FILE type contain?
A: 1 B: 2 C: 4 D: 8 E: none of these
PartE
The essence ofbut not exact syntax that you will find forthe function prototype for printf() (that is also found in ) is
int printf(const char *, ...);
How many actual parameters can be passed to printf()?
A: always exactly 1 B: always exactly 2 C: 2 or more D: 1 or more E: none of these
PartF
The displayed addresses of the functions main(), F(), UnusedFunction1(), and UnusedFunction2() are very close to one another. How close, measured in bytes, are the closest 2 addresses? A: 1510 B: 910 C: 516 D: 410 E: none of these
Note The displayed addresses of the functions are too close to represent the actual address-of the first instruction of the body of each function because the function bodies must be larger than the few bytes that separate them. Dr. Hanna suspects what we are actually looking at is a table of starting addresses.
Part G
The global constant F1 (whose main memory address is 0042301C16) and the literal string constant "n? " (address 0042306416) are only 4816 or 7210 bytes apart, so, Y or N? is it reasonable to assume they are stored in the same area of memory?
PartH
(Continuing 8) The global constant F1 is separated from the global variable calls (address 0042679416) by 377816 bytes of memory. How many full (do not include partial) 1K-byte memory pages is 377816 = 00110111011110002 bytes? Note The displayed addresses of F1 and calls are probably not close enough to be considered in the same area of main memory.
A: 3716 B: 3710 C: 1310 D: 1410 E: none of these
Part I
The variables n, i, Fn, and totalCalls are local variables defined in the function main() that are automatically-allocated in the activation record for main() when main() is activated (when it is called by the operating system and becomes active). The variables occupy contiguous memory locations in the activation record. The activation record is on the run-time stack. How big is each of the variables? A: 1 byte B: 2 bytes C: 4 bytes D: 8 bytes E: none of these
Part L
The value of the variable calls before it is initialized is an indeterminate piece of RAM garbage, but the value of calls after it is initialized with the malloc() function return value is a main memory address that points-into which area of main memory? Hint See line 154!
A: the run-time stack (see Question 10) B: the global constant area (see Question 8)
C: the function address table (see Question 7) D: the heap 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started