Question

A programmer is having difficulty debugging the following C program. In theory, with infinite memory, this...

A programmer is having difficulty debugging the following C program. In theory, with infinite memory, this program would run forever but in practice, this program crashes because it runs out of memory.

Explain the behavior of the program by translating the definition of f into lambda calculus and then reducing the application f(f). This program assumes that the type checker does not check the types of arguments to functions.

int f(int (*g)(...)) { /* g points to a function that returns an int */
      return g(g);

}
int main() {

      int x;
      x = f(f);
      printf("Value of x = %d\n", x);
      return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If we explain this using calculus.

let's assume

f(x) = X^2;

f(f(x)) = (f(x))^2 = (X^2)^2 = X^(2+2)

f(f(f(x))) = (f(f(x))^2 = ((f(x))^2)^2 = ((X^2)^2)^2 = X^(2+2+2)

------

f(f(f(f(f(f....... )))))) = X^(2+2+2+ .........)

= which will be great value

if we go like this forever.

  

int f(int (*g)(...)) { /* g points to a function that returns an int */

return g(g);

}

int main() {

int x;

x = f(f);

printf("Value of x = %d\n", x);

return 0;

}

  

If we consider this program each time function is called a stack frame is

generated in the memory and as we see this function calling will go forever

then stack boundry will be cross and it will result in a run-time program

crash which stack overflow.

Add a comment
Know the answer?
Add Answer to:
A programmer is having difficulty debugging the following C program. In theory, with infinite memory, this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you were debugging the push() function of your program. Which of the following variables would...

    Suppose you were debugging the push() function of your program. Which of the following variables would be accessible to the debugger before the function is called? static struct node *stack; static struct node *new_node() { int size = sizeof(struct node); return malloc(size); void push(void *value) { struct node *n = new_node(); n->value = value; n->next = stack; stack = n; return malloc(size); void push (void *value) { struct node *n = new_node(); n->value = value; n->next = stack; stack =...

  • Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int...

    Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int <stdio.h> TO PRINT toPrintCPtr "Good luck!" TO PRINT; main for (i -0; i < sizeof (TO PRINT)-1; i++) printf("%c %c\n", toPrintCPt r [1], toupper(toPrintCPt r [i])); return(EXIT SUCCESS); Please tell where the following objects are stored in memory. Your choices are a. ROM BIOS b. kernal Memory (the OS) c. shared library memory (the glibc library) d. .text segment e. .rodata segment f. .data...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • These are my answere to the following questions: are they right? 1. B 2. T 3....

    These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....

  • QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

    QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT