Recursive calls to a function - why is the address of the parameter passed to it lowering with each call? ...
Is it OK if I do not take the receipt in Germany?
How to create a command for the "strange m" symbol in latex?
What documents does someone with a long-term visa need to travel to another Schengen country?
Weaponising the Grasp-at-a-Distance spell
Does using the Inspiration rules for character defects encourage My Guy Syndrome?
Why aren't these two solutions equivalent? Combinatorics problem
Married in secret, can marital status in passport be changed at a later date?
Why aren't road bike wheels tiny?
Like totally amazing interchangeable sister outfit accessory swapping or whatever
Unix AIX passing variable and arguments to expect and spawn
Meaning of "Not holding on that level of emuna/bitachon"
Why not use the yoke to control yaw, as well as pitch and roll?
Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?
Is Vivien of the Wilds + Wilderness Reclamation a competitive combo?
Will I be more secure with my own router behind my ISP's router?
How to make an animal which can only breed for a certain number of generations?
How can I introduce the names of fantasy creatures to the reader?
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?
Can gravitational waves pass through a black hole?
Should man-made satellites feature an intelligent inverted "cow catcher"?
How to ask rejected full-time candidates to apply to teach individual courses?
How to leave only the following strings?
What were wait-states, and why was it only an issue for PCs?
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;
}
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++
New contributor
|
show 2 more comments
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++
New contributor
3
You are passing a copy - that has to have an address
– UnholySheep
6 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
6 hours ago
I can't understand why this isn't eligible for tail-call optimization. The invocation oftest_func
is the last line in the function...
– cyberbisson
5 hours ago
5
@cyberbisson The parameters of the nested invocations oftest_func
must appear to have different addresses per language rules, and because the address ofaddress
was passed tooperator<<
the compiler can't prove that this is unobservable.
– T.C.
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago
|
show 2 more comments
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++
New contributor
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++
c++
New contributor
New contributor
edited 6 hours ago
drescherjm
6,59923553
6,59923553
New contributor
asked 6 hours ago
tears allotears allo
441
441
New contributor
New contributor
3
You are passing a copy - that has to have an address
– UnholySheep
6 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
6 hours ago
I can't understand why this isn't eligible for tail-call optimization. The invocation oftest_func
is the last line in the function...
– cyberbisson
5 hours ago
5
@cyberbisson The parameters of the nested invocations oftest_func
must appear to have different addresses per language rules, and because the address ofaddress
was passed tooperator<<
the compiler can't prove that this is unobservable.
– T.C.
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago
|
show 2 more comments
3
You are passing a copy - that has to have an address
– UnholySheep
6 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
6 hours ago
I can't understand why this isn't eligible for tail-call optimization. The invocation oftest_func
is the last line in the function...
– cyberbisson
5 hours ago
5
@cyberbisson The parameters of the nested invocations oftest_func
must appear to have different addresses per language rules, and because the address ofaddress
was passed tooperator<<
the compiler can't prove that this is unobservable.
– T.C.
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago
3
3
You are passing a copy - that has to have an address
– UnholySheep
6 hours ago
You are passing a copy - that has to have an address
– UnholySheep
6 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
6 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
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
5 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
5 hours ago
5
5
@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.
4 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.
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
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?
– ᆼᆺᆼ
53 mins ago
@ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers iscdecl
, 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
33 mins ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
– ᆼᆺᆼ
53 mins ago
@ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers iscdecl
, 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
33 mins ago
add a comment |
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?
– ᆼᆺᆼ
53 mins ago
@ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers iscdecl
, 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
33 mins ago
add a comment |
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.
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.
answered 6 hours ago
David SchwartzDavid Schwartz
140k14146232
140k14146232
Is it placed on the stack instead of in a register because its address is taken?
– ᆼᆺᆼ
53 mins ago
@ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers iscdecl
, 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
33 mins ago
add a comment |
Is it placed on the stack instead of in a register because its address is taken?
– ᆼᆺᆼ
53 mins ago
@ᆼᆺᆼ no, it is because on 32bit systems, the default calling convention in most C/C++ compilers iscdecl
, 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
33 mins ago
Is it placed on the stack instead of in a register because its address is taken?
– ᆼᆺᆼ
53 mins ago
Is it placed on the stack instead of in a register because its address is taken?
– ᆼᆺᆼ
53 mins 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
33 mins 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
33 mins ago
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
You are passing a copy - that has to have an address
– UnholySheep
6 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
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
5 hours ago
5
@cyberbisson The parameters of the nested invocations of
test_func
must appear to have different addresses per language rules, and because the address ofaddress
was passed tooperator<<
the compiler can't prove that this is unobservable.– T.C.
4 hours ago
@T.C. So, the problem is that the callee might remember and use it still?
– Deduplicator
4 hours ago