Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code: // // funWithFooAndBar.c - fun with function calls

ID: 646833 • Letter: C

Question

Consider the following code:

//
// funWithFooAndBar.c - fun with function calls
//
#include
#include

void bar()
{
static int bar_cnt = 0;
bar_cnt++;
printf("Now inside bar() - count = %d ! ", bar_cnt);
}

void foo()
{
void *addr[1];
printf("Now inside foo() ! ");
// you can only modify this section

addr[1] = bar;

addr[2] = bar;
return;
}

int main (int argc, char *argv[])
{
volatile void *space[100];

foo();
printf("Back in main ");
return 0;
}

For the first part of the assignment, we will simply modify some code to smash the stack by writing beyond the end of an array and thus, overwriting the return address, so that a function call to foo()returns to bar()on its way back to main(). In particular, you want the output to be:

Now inside foo()!

Now inside bar()

Explanation / Answer

Now it was working!!

#include <stdio.h>

void bar()
{
static int bar_cnt = 0;
bar_cnt++;
printf("Now inside bar() - count = %d ! ", bar_cnt);
}
void foo()
{
void *addr[1];
printf("Now inside foo() ! ");
bar(); //I am calling bar function
// you can only modify this section
//addr[1] = bar; //we didnt declare bar any where so it shows segmentati0n error
//addr[2] = bar;

return;
}
int main (int argc, char *argv[])
{
volatile void *space[100];
foo();
printf("Back in main ");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote