1

I've been given some C++ code which is a method called clamp that returns a integer value. I've been asked to look at the stack in memory which i have and through disassembly i am suppose to be able to see the return values address in the stack. My disassembly and code

What am i meant to look for as i cant find any information online?

int& clamp(int& value, int low, int high) {
    if (value < low)
        return low;
    if (value > high)
        return high;
    return value;
}

I've attached a picture of what i can see and what i believe to be the return values memory address which is red in the memory window. Above is the code for the method as well.

Owen Hodgson
  • 37
  • 1
  • 6
  • This returns an integer reference, not a value. Optimizer can change things and return value may just be in a register. Not necessarily on the stack. – Garr Godfrey Mar 08 '20 at 20:55
  • 3
    Have you discussed `calling conventions` in your class? https://stackoverflow.com/questions/949862/what-are-the-different-calling-conventions-in-c-c-and-what-do-each-mean – JohnFilleau Mar 08 '20 at 20:58
  • I believe you highlighted the part of the stack used to pass the parameters. That's the address of A in red. That same value should appear in EAX as the returned address. – Garr Godfrey Mar 08 '20 at 21:09
  • The function itself results in Undefined Behavior because it potentially returns a reference to a local variable (parameter). – 1201ProgramAlarm Mar 08 '20 at 21:26

0 Answers0