Consider following code:

#include <iostream>
using namespace std;
void test_func(int address) {
    cout<<&address<<" ";
    if(address < 0x7FFBEE26) {
int main()
    return 0;

Hello from main() is certainly not reached, since the recursive calls to test_func never end.

However, from what I can see in the cout present in test_func - the addresses being printed are lower and lower with each iteration. Why is that happening?

  • 4
    You are passing a copy - that has to have an address – UnholySheep Apr 22 at 20:50
  • 1
    Remember that the default stack size on linux is 10MB and its 1 MB on windows. Also the stack need not be in the same location each time you run your program. – drescherjm Apr 22 at 21:04
  • I can't understand why this isn't eligible for tail-call optimization. The invocation of test_func is the last line in the function... – cyberbisson Apr 22 at 21:33
  • 9
    @cyberbisson The parameters of the nested invocations of test_func must appear to have different addresses per language rules, and because the address of address was passed to operator<< the compiler can't prove that this is unobservable. – T.C. Apr 22 at 22:46
  • 1
    @Deduplicator Yes. – T.C. Apr 22 at 23:48

Likely address is being placed on the stack and, on your platform, the stack grows downward in memory. See this question about stack growth direction for more.

  • Is it placed on the stack instead of in a register because its address is taken? – ᆼᆺᆼ Apr 23 at 2:35
  • 3
    @ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers is cdecl, which passes parameters on the call stack only. Compile your code for 64bit, or alter your function to use a register-based calling convention, and you will likely see different results – Remy Lebeau Apr 23 at 2:55
  • @RemyLebeau What even is the address of a passed-by-register argument? Apparently, GCC artificially moves the register value onto the stack and then takes the address: godbolt.org/z/LRz5DS – ComFreek Apr 23 at 7:00
  • @ComFreek registers don't have addresses. Copying a register value to a memory block is the only way to get a memory address. – Remy Lebeau Apr 23 at 7:43
  • It's difficult to put address in a register because there will be many instances of address at the same time, one for each instance oftest_func that's in the process of executing. – David Schwartz Apr 23 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.