#include #include #include using namespace std; // ============================================================================== // This function takes two stack memory addresses (start and end) and prints // out the contents of the memory between and including those // addresses. // // To make the output more readable, the function assumes that // "values" will be between -1000 & 1000 and addresses will be within // +/- 1000 bytes from the start address // void print_stack(int* start, int* end) { // we assume that the start address is "above" the end address on // the stack. this means that the start location was allocated // before the end location. assert (start >= end); cout << "-----------------------------------------" << endl; for (int* address = start; address >= end; address--) { // this location might store a value (we assume all values are integers) int value = *address; // it might store an address (so we cast it to a pointer) int* if_address = (int*)value; cout << "location: " << address; // if it's a value if (abs(value) < 1000) cout << " VALUE: " << value << endl; // if it's an address else if (abs(start-if_address) < 1000) cout << " POINTER: " << if_address << endl; // otherwise it's probably garbage else cout << " garbage? " << endl; } cout << "-----------------------------------------" << endl; }