In: Computer Science
C Programming
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to.
char[] class = {'C','O','M','P','1','2','2'"};
#define int n = 4;
long long fibb(long long a, long long b, int n)
{
return (--n>0)?(fibb(b, a+b, n)):(a);
}
int main()
{
fib(3);
//illustrate what memory looks like at this point
return 0;
}
SOLUTION -
Since there are no dynamic allocations statements in the given code, all the variables are allocated in Stack only.
(1)The class array is a Global array so it will be allocated in the Global variable section in Stack
(2)In the main function there is no variable declaration so the main function allocation is nil
(3)From main we call fibb() function, in that we have a,b,n are allocated, Due to the recursive call multiple allocations of 'fibb' will be there during the execution as shown below
(4)After execution completed the allocations is shown below:
Note:#define only allocates symbolic constants which will be replaced during compilation.So no memory is allocated to #define variables
The given code is having some error so i rectified them
#define n1 10
char class[] = {'C','O','M','P','1','2','2'};
long long fibb(long long a, long long b, int n)
{
return (--n>0)?(fibb(b, a+b, n)):(a);
}
int main()
{
fibb(3,2,3);
//illustrate what memory looks like at this point
return 0;
}
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------