1. (10 pts) Write the code for a simple C program with the following features (t
ID: 3890336 • Letter: 1
Question
1. (10 pts) Write the code for a simple C program with the following
features (the exact details are completely upto you): (i) main() calls a
function foo() which in turn calls a function bar(), (ii) has a global
variable int *a, and (iii) bar() dynamically allocates an integer and
assigns its address to a. On a time-line, show the contents of the
process address space: (a) just before the dynamic memory allocation
in bar(), (b) just after the dynamic memory allocation in bar(), and
(c) just after bar() returns. Be sure to identify speci c contents within
the frames of the process stack.
Explanation / Answer
Answer:
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
void foo();
int bar();
int*a;
int main()
{
cout<<" Here i m main hey foo i m calling you";
foo();
}
void foo()
{
printf("hi my name is foo hey main i m calling bar");
bar();
}
int bar()
{
int n=10;
cout<<" contents of a "<<10;
a=new int(n);
stack<int>c;
c.push(n);
c.push(n);
bar();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.