10 marks Complete the following C program memoryc so that it displays the

10 marks complete the following c program memoryc so

This preview shows page 4 - 7 out of 11 pages.

(10 marks) Complete the following C program, memory.c, so that it displays the addresses ofa.functions f1, f2, and mainb.all string literals such as "Hello, world!";c.all initialized global variables;d.all un-initialized global variables;e.all dynamically allocated memories;f.all formal parameters in functions;g.all local variables;h.start and end of its command line arguments;i.start and end of its environment (variables and values).2. /* 3.* name;memory.c4.*/ ;
Image of page 4
5. 6. #include <stdlib.h> 7. #include <stdio.h> 8. #include <math.h> 9. #include <string.h> 10. 11. extern char **environ; 12. 13. int global_x = 10; // initialised global variable 14. int global_y; // un- initialised global variable 15. char global_array1[] = "Hello, world!"; // initialised global array 16. char global_array2[10]; // un- initialised global array 17. char *global_pointer1 = "bye!"; // global pointer to a string literal 18. char *global_pointer2; // un- initialised global pointer 19. float global_float = 100.1; // initialised global variable 20. double global_double; // un- initialised global variable 21. 22. int f2(int x) 23. { 24. char * f2_p; 25. int f2_x = 21; 26. 27. f2_p = malloc(1000); // dynamically allocated memory 28. 29. // print out the address of x 30. // print out the addresses of f2_p, and f2_x 31. // print out the starting address of the dynamically allocated memory 32. ..... 33. 34. L: f2_x = 10; 35. return f2_x; 36. } 37. 38. void f1(int x1, int x2, float x3, char x4, double x5, int x6) 39. { 40. int f1_x = 10; 41. int f1_y; 42. char *f1_p1 = "This is inside f1"; // pointer to another string literal 43. char *f1_p2; 44. 45. f1_p2 = malloc(100); // dynamically allocated memory 46. 47. // print out the addresses of x1, x2, x3, x4, x5, x6 48. // print out the addresses of f1_x, f1_y, f1_p1, f1_p2 49. // print out the address of the string literal "This is inside f1" 50. ..... 51.
Image of page 5
52. f1_y = f2(10); 53. return; 54. } 55. 56. int main(int argc, char *argv[]) 57. { 58. 59. printf("My OS bit size: %lu\n", sizeof(void *) * 8); 60. 61. // print out the addresses of argc, argv 62. // print out the starting address and end address of the command line arguments of this process 63. // print out the starting address and end address of the environment of this process 64. // print out the starting addresses of function main, f1, and f2 65. // print out the addresses of global_x, global_y, global_array1, global_array2, global_pointer1, 66. // global_pointer2, global_float, global_double 67. // print out the addresses of string literals "Hello, world!" and "bye" 68. 69. ..... 70. 71. // call function f1 with suitable arguments such as 12, -5, 33.7, 'A', 1.896e-10, 100 72. f1( .... ); 73. 74. exit(0); 75. } The following example shows how to display the virtual addresses in hexadecimal notation, eg: int x; char *p="this is a string literal"; printf("address of variable x starts at %p\n", &x)); printf("address of variable p starts at %p\n", &p)); printf("address of the string literal starts at %p\n", p); 76.(30 marks) Based on the information generated from the above program, produce a memory map table showing the layout of literals, initialised global variables, uninitialised global variables, formal parameters of each function, local variables, dynamically allocated variables, functions, environment and command line arguments in the memory when the program runs at label L in function f2 .
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 11 pages?

  • One '18
  • Virtual memory, command line arguments, Unit Coordinator, party source code

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture