Stack Exploration (ITI8510)

Allikas: Lambda
Real-Time Operating Systems and Systems Programming
[ @ ]

In preparation for memory management, we shall investigate call stack contents.

Task

Write some small program for further exploration. The program should contain:

  • function main() containing:
    • at least 3 integers, with some (different) default values
    • a string
  • function void foo(int argument) containing:
    • at least 2 integers, with some default values

This base program can do practically anything (including nothing), as long as the function foo() runs.

  1. Modify the program to print the memory addresses of all the variables. Use "%p" for printf() function. Use & operator on the variable to get the memory address as a value.
  2. Within function foo(), use the memory address of the first declared variable to print out the value of address which precedes it and the memory address which follows it. (Use * operator to read memory from given location)
  3. Can you go further? Can you read the variables of main() just by modifying the address of a variable in foo()? (Yes, but try it out)
  4. Can you print the string from main()? Is it located in the stack?
  5. Comment some of the lines which print the addresses of variables in main() (from task 1), check that your program does not use them -- can you still print them by using an address from foo()?
  6. Put the variables back to the stack. Write a function which tries to print out all the stack (4 bytes per line using printf() with "%08x\n", for example: it would be more convenient if you print out decimal, hexadecimal, and char from the location along with the memory address.) until you get a segmentation fault.
Personaalsed tööriistad
Nimeruumid
Variandid
Toimingud
Navigeerimine
Kasulikku
Tööriistad