Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?What is the direction of stack growth in most modern systems?Why isn't sizeof for a struct equal to the sum of sizeof of each member?How to call a parent class function from derived class function?Why do we need virtual functions in C++?Pretty-print C++ STL containersAre the days of passing const std::string & as a parameter over?Recursive Reverse FunctionWhy can I not move unique_ptr from a set to a function argument using an iterator?Why can I not call reserve on a vector of const elements?recursive variadic template function call “loses” pointer on second argument typeHaving issues with .h file, it doesn't seem to be linking correctly

Restricting the options of a lookup field, based on the value of another lookup field?

Could moose/elk survive in the Amazon forest?

Apply a different color ramp to subset of categorized symbols in QGIS?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

Check if a string is entirely made of the same substring

Where was the County of Thurn und Taxis located?

Mistake in years of experience in resume?

Philosophical question on logistic regression: why isn't the optimal threshold value trained?

Is there any pythonic way to find average of specific tuple elements in array?

Extracting Dirichlet series coefficients

Injection into a proper class and choice without regularity

Multiple options vs single option UI

Should the Product Owner dictate what info the UI needs to display?

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

Does a large simulator bay have standard public address announcements?

How exactly does Hawking radiation decrease the mass of black holes?

Who's the random kid standing in the gathering at the end?

Creating a chemical industry from a medieval tech level without petroleum

SFDX - Create Objects with Custom Properties

Why do games have consumables?

How to have a sharp product image?

Will I lose my paid in full property

std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?

Find a stone which is not the lightest one



Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?


What is the direction of stack growth in most modern systems?Why isn't sizeof for a struct equal to the sum of sizeof of each member?How to call a parent class function from derived class function?Why do we need virtual functions in C++?Pretty-print C++ STL containersAre the days of passing const std::string & as a parameter over?Recursive Reverse FunctionWhy can I not move unique_ptr from a set to a function argument using an iterator?Why can I not call reserve on a vector of const elements?recursive variadic template function call “loses” pointer on second argument typeHaving issues with .h file, it doesn't seem to be linking correctly






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








10















Consider following code:



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


int main()

test_func(512);
cout<<"Hello";
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?










share|improve this question









New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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

















10















Consider following code:



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


int main()

test_func(512);
cout<<"Hello";
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?










share|improve this question









New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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













10












10








10


1






Consider following code:



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


int main()

test_func(512);
cout<<"Hello";
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?










share|improve this question









New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Consider following code:



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


int main()

test_func(512);
cout<<"Hello";
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?







c++ recursion memory-address






share|improve this question









New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Muntasir

6381919




6381919






New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 22 at 20:48









tears allotears allo

594




594




New contributor




tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






tears allo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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












  • 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







4




4





You are passing a copy - that has to have an address

– UnholySheep
Apr 22 at 20:50





You are passing a copy - that has to have an address

– UnholySheep
Apr 22 at 20:50




1




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






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






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




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





@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




1





@Deduplicator Yes.

– T.C.
Apr 22 at 23:48





@Deduplicator Yes.

– T.C.
Apr 22 at 23:48












1 Answer
1






active

oldest

votes


















22














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.






share|improve this answer























  • 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
    2 days ago












  • @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
    2 days ago











  • 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
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






tears allo is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55800947%2frecursive-calls-to-a-function-why-is-the-address-of-the-parameter-passed-to-it%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









22














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.






share|improve this answer























  • 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
    2 days ago












  • @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
    2 days ago











  • 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
    2 days ago















22














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.






share|improve this answer























  • 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
    2 days ago












  • @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
    2 days ago











  • 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
    2 days ago













22












22








22







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 22 at 20:50









David SchwartzDavid Schwartz

140k14147232




140k14147232












  • 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
    2 days ago












  • @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
    2 days ago











  • 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
    2 days ago

















  • 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
    2 days ago












  • @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
    2 days ago











  • 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
    2 days ago
















Is it placed on the stack instead of in a register because its address is taken?

– ᆼᆺᆼ
Apr 23 at 2:35





Is it placed on the stack instead of in a register because its address is taken?

– ᆼᆺᆼ
Apr 23 at 2:35




3




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






@ᆼᆺᆼ 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
2 days ago






@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
2 days ago














@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
2 days ago





@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
2 days ago













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
2 days ago





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
2 days ago












tears allo is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















tears allo is a new contributor. Be nice, and check out our Code of Conduct.












tears allo is a new contributor. Be nice, and check out our Code of Conduct.











tears allo is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55800947%2frecursive-calls-to-a-function-why-is-the-address-of-the-parameter-passed-to-it%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?