Recursive calls to a function - why is the address of the parameter passed to it lowering with each call? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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 containersHow to pass normal param as well as template param in a template function in C++?Are 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?Having issues with .h file, it doesn't seem to be linking correctly

How is an IPA symbol that lacks a name (e.g. ɲ) called?

How to leave only the following strings?

Converting a text document with special format to Pandas DataFrame

Why isn't everyone flabbergasted about Bran's "gift"?

Is my guitar’s action too high?

Has a Nobel Peace laureate ever been accused of war crimes?

How to mute a string and play another at the same time

What is the ongoing value of the Kanban board to the developers as opposed to management

Does traveling In The United States require a passport or can I use my green card if not a US citizen?

Protagonist's race is hidden - should I reveal it?

How do I deal with an erroneously large refund?

Why do C and C++ allow the expression (int) + 4*5?

Married in secret, can marital status in passport be changed at a later date?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

What is the difference between 准时 and 按时?

Is Vivien of the Wilds + Wilderness Reclamation a competitive combo?

Are Flameskulls resistant to magical piercing damage?

How to ask rejected full-time candidates to apply to teach individual courses?

What could prevent concentrated local exploration?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Who's this lady in the war room?

If gravity precedes the formation of a solar system, where did the mass come from that caused the gravity?

Can gravitational waves pass through a black hole?

Raising a bilingual kid. When should we introduce the majority language?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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 containersHow to pass normal param as well as template param in a template function in C++?Are 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?Having 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;








9















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
    7 hours ago






  • 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
    7 hours ago












  • 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
    6 hours ago







  • 6





    @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.
    5 hours ago











  • @T.C. So, the problem is that the callee might remember and use it still?

    – Deduplicator
    5 hours ago

















9















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
    7 hours ago






  • 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
    7 hours ago












  • 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
    6 hours ago







  • 6





    @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.
    5 hours ago











  • @T.C. So, the problem is that the callee might remember and use it still?

    – Deduplicator
    5 hours ago













9












9








9








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++






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 7 hours ago









drescherjm

6,59923553




6,59923553






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 7 hours ago









tears allotears allo

491




491




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
    7 hours ago






  • 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
    7 hours ago












  • 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
    6 hours ago







  • 6





    @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.
    5 hours ago











  • @T.C. So, the problem is that the callee might remember and use it still?

    – Deduplicator
    5 hours ago












  • 4





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

    – UnholySheep
    7 hours ago






  • 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
    7 hours ago












  • 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
    6 hours ago







  • 6





    @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.
    5 hours ago











  • @T.C. So, the problem is that the callee might remember and use it still?

    – Deduplicator
    5 hours ago







4




4





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

– UnholySheep
7 hours ago





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

– UnholySheep
7 hours ago




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
7 hours ago






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
7 hours ago














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
6 hours ago






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
6 hours ago





6




6





@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.
5 hours ago





@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.
5 hours ago













@T.C. So, the problem is that the callee might remember and use it still?

– Deduplicator
5 hours ago





@T.C. So, the problem is that the callee might remember and use it still?

– Deduplicator
5 hours ago












1 Answer
1






active

oldest

votes


















18














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?

    – ᆼᆺᆼ
    1 hour ago











  • @ᆼᆺᆼ 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
    1 hour 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









18














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?

    – ᆼᆺᆼ
    1 hour ago











  • @ᆼᆺᆼ 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
    1 hour ago
















18














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?

    – ᆼᆺᆼ
    1 hour ago











  • @ᆼᆺᆼ 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
    1 hour ago














18












18








18







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 7 hours ago









David SchwartzDavid Schwartz

140k14146232




140k14146232












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

    – ᆼᆺᆼ
    1 hour ago











  • @ᆼᆺᆼ 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
    1 hour ago


















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

    – ᆼᆺᆼ
    1 hour ago











  • @ᆼᆺᆼ 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
    1 hour ago

















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

– ᆼᆺᆼ
1 hour ago





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

– ᆼᆺᆼ
1 hour ago













@ᆼᆺᆼ 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
1 hour ago






@ᆼᆺᆼ 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
1 hour 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

How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

Чепеларе Съдържание География | История | Население | Спортни и природни забележителности | Културни и исторически обекти | Религии | Обществени институции | Известни личности | Редовни събития | Галерия | Източници | Литература | Външни препратки | Навигация41°43′23.99″ с. ш. 24°41′09.99″ и. д. / 41.723333° с. ш. 24.686111° и. д.*ЧепелареЧепеларски Linux fest 2002Начало на Зимен сезон 2005/06Национални хайдушки празници „Капитан Петко Войвода“Град ЧепелареЧепеларе – народният ски курортbgrod.orgwww.terranatura.hit.bgСправка за населението на гр. Исперих, общ. Исперих, обл. РазградМузей на родопския карстМузей на спорта и скитеЧепеларебългарскибългарскианглийскитукИстория на градаСки писти в ЧепелареВремето в ЧепелареРадио и телевизия в ЧепелареЧепеларе мами с родопски чар и добри пистиЕвтин туризъм и снежни атракции в ЧепелареМестоположениеИнформация и снимки от музея на родопския карст3D панорами от ЧепелареЧепелареррр