Modify the contents of the memory address of the return of a function
Is it possible to modify the contents of the memory address of the return value of a function? functions return the value of a locally defined variable.
In the following example, compiled for my machine (x86-64) without warnings:
#include <stdio.h>
int get_val1()
{
int ret = 1;
return ret;
}
int get_val2()
{
int ret = 2;
return ret;
}
int get_val3()
{
int ret = 3;
return ret;
}
void redefine_ints(int *val1, int *val2, int *val3) {
*val1 = 10;
*val2 = 11;
*val3 = 12;
}
void print_and_redefine_ints(int val1, int val2, int val3) {
printf("val1 %d val2 %d val3 %dn", val1, val2, val3);
redefine_ints(&val1, &val2, &val3);
printf("rval1 %d rval2 %d rval3 %dn", val1, val2, val3);
}
int main()
{
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
return 0;
}
I get the next output:
val1 1 val2 2 val3 3
rval1 10 rval2 11 rval3 12
This is the expected output, but how is it possible? Where are these variables stored?
c return-value
|
show 1 more comment
Is it possible to modify the contents of the memory address of the return value of a function? functions return the value of a locally defined variable.
In the following example, compiled for my machine (x86-64) without warnings:
#include <stdio.h>
int get_val1()
{
int ret = 1;
return ret;
}
int get_val2()
{
int ret = 2;
return ret;
}
int get_val3()
{
int ret = 3;
return ret;
}
void redefine_ints(int *val1, int *val2, int *val3) {
*val1 = 10;
*val2 = 11;
*val3 = 12;
}
void print_and_redefine_ints(int val1, int val2, int val3) {
printf("val1 %d val2 %d val3 %dn", val1, val2, val3);
redefine_ints(&val1, &val2, &val3);
printf("rval1 %d rval2 %d rval3 %dn", val1, val2, val3);
}
int main()
{
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
return 0;
}
I get the next output:
val1 1 val2 2 val3 3
rval1 10 rval2 11 rval3 12
This is the expected output, but how is it possible? Where are these variables stored?
c return-value
1
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.
– Federico klez Culloca
7 hours ago
2
You are not modifying the return value of a function directly.print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.
– Osiris
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
You are not modifying anything that are inget_val1()
/2/3. But, inprint_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way :print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps
– Cid
7 hours ago
1
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago
|
show 1 more comment
Is it possible to modify the contents of the memory address of the return value of a function? functions return the value of a locally defined variable.
In the following example, compiled for my machine (x86-64) without warnings:
#include <stdio.h>
int get_val1()
{
int ret = 1;
return ret;
}
int get_val2()
{
int ret = 2;
return ret;
}
int get_val3()
{
int ret = 3;
return ret;
}
void redefine_ints(int *val1, int *val2, int *val3) {
*val1 = 10;
*val2 = 11;
*val3 = 12;
}
void print_and_redefine_ints(int val1, int val2, int val3) {
printf("val1 %d val2 %d val3 %dn", val1, val2, val3);
redefine_ints(&val1, &val2, &val3);
printf("rval1 %d rval2 %d rval3 %dn", val1, val2, val3);
}
int main()
{
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
return 0;
}
I get the next output:
val1 1 val2 2 val3 3
rval1 10 rval2 11 rval3 12
This is the expected output, but how is it possible? Where are these variables stored?
c return-value
Is it possible to modify the contents of the memory address of the return value of a function? functions return the value of a locally defined variable.
In the following example, compiled for my machine (x86-64) without warnings:
#include <stdio.h>
int get_val1()
{
int ret = 1;
return ret;
}
int get_val2()
{
int ret = 2;
return ret;
}
int get_val3()
{
int ret = 3;
return ret;
}
void redefine_ints(int *val1, int *val2, int *val3) {
*val1 = 10;
*val2 = 11;
*val3 = 12;
}
void print_and_redefine_ints(int val1, int val2, int val3) {
printf("val1 %d val2 %d val3 %dn", val1, val2, val3);
redefine_ints(&val1, &val2, &val3);
printf("rval1 %d rval2 %d rval3 %dn", val1, val2, val3);
}
int main()
{
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
return 0;
}
I get the next output:
val1 1 val2 2 val3 3
rval1 10 rval2 11 rval3 12
This is the expected output, but how is it possible? Where are these variables stored?
c return-value
c return-value
edited 4 hours ago
Sourav Ghosh
109k14130188
109k14130188
asked 7 hours ago
HenryGiraldoHenryGiraldo
17111
17111
1
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.
– Federico klez Culloca
7 hours ago
2
You are not modifying the return value of a function directly.print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.
– Osiris
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
You are not modifying anything that are inget_val1()
/2/3. But, inprint_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way :print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps
– Cid
7 hours ago
1
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago
|
show 1 more comment
1
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.
– Federico klez Culloca
7 hours ago
2
You are not modifying the return value of a function directly.print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.
– Osiris
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
You are not modifying anything that are inget_val1()
/2/3. But, inprint_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way :print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps
– Cid
7 hours ago
1
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago
1
1
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.– Federico klez Culloca
7 hours ago
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.– Federico klez Culloca
7 hours ago
2
2
You are not modifying the return value of a function directly.
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.– Osiris
7 hours ago
You are not modifying the return value of a function directly.
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.– Osiris
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
You are not modifying anything that are in
get_val1()
/2/3. But, in print_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way : print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps– Cid
7 hours ago
You are not modifying anything that are in
get_val1()
/2/3. But, in print_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way : print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps– Cid
7 hours ago
1
1
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
Yes this is well-defined C.
The anonymous temporary int
s created by get_val...()
have a lifetime contemporaneous with the entire statement in which they are created.
But note that you take a value copy of each of these int
s when you call print_and_redefine_ints
so there's nothing particularly special going on here.
(Note that you would not be able to bind pointers to the anonymous temporary int
s to int*
function parameters though.)
add a comment |
A draw may explain more than some text. I'll use only 1 get_val1()
in that example.
print_and_redefine_ints(get_val1());
|
|
[CALL]
|
|
V
int get_val1()
{
int ret = 1;<----------------------------------------------------+
return ret; |
} | |
| |
[COPY OF VALUE] |
| |
| |
+---+ |
| |
| |
V |
void print_and_redefine_ints(int val1) { |
printf("val1 %dn"); ^ |
redefine_ints(&val1); | |
| +--------------------------------------------+ |
| | |
[POINTER AKA REFERENCE] | |
| | |
| | |
V | |
void redefine_ints(int *val1) { | |
*val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
} |
|
+---+
|
[VALUE CHANGED]
|
|
V
printf("rval1 %dn", val1);
printf("original val1 %dn", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
add a comment |
Is it possible to modify the contents of the memory address of the return (value) of a function?
No, it is not.
However, that is not the case here. In your code, the return values of get_val<n>()
function calls are stored in the function parameters int val1
, int val2
, int val3
. They are local to the called function. The lifetime of those variables are the function execution period.
Quoting C11
, chapter §6.2.1,
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [....]
and, from §6.9.1, Function definition,
Each parameter has automatic storage duration; its identifier is an lvalue
Thus, just like any other local variable, you can modify the content of those variables using their address.
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
});
}
});
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%2f54367396%2fmodify-the-contents-of-the-memory-address-of-the-return-of-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes this is well-defined C.
The anonymous temporary int
s created by get_val...()
have a lifetime contemporaneous with the entire statement in which they are created.
But note that you take a value copy of each of these int
s when you call print_and_redefine_ints
so there's nothing particularly special going on here.
(Note that you would not be able to bind pointers to the anonymous temporary int
s to int*
function parameters though.)
add a comment |
Yes this is well-defined C.
The anonymous temporary int
s created by get_val...()
have a lifetime contemporaneous with the entire statement in which they are created.
But note that you take a value copy of each of these int
s when you call print_and_redefine_ints
so there's nothing particularly special going on here.
(Note that you would not be able to bind pointers to the anonymous temporary int
s to int*
function parameters though.)
add a comment |
Yes this is well-defined C.
The anonymous temporary int
s created by get_val...()
have a lifetime contemporaneous with the entire statement in which they are created.
But note that you take a value copy of each of these int
s when you call print_and_redefine_ints
so there's nothing particularly special going on here.
(Note that you would not be able to bind pointers to the anonymous temporary int
s to int*
function parameters though.)
Yes this is well-defined C.
The anonymous temporary int
s created by get_val...()
have a lifetime contemporaneous with the entire statement in which they are created.
But note that you take a value copy of each of these int
s when you call print_and_redefine_ints
so there's nothing particularly special going on here.
(Note that you would not be able to bind pointers to the anonymous temporary int
s to int*
function parameters though.)
edited 7 hours ago
answered 7 hours ago
BathshebaBathsheba
177k27253376
177k27253376
add a comment |
add a comment |
A draw may explain more than some text. I'll use only 1 get_val1()
in that example.
print_and_redefine_ints(get_val1());
|
|
[CALL]
|
|
V
int get_val1()
{
int ret = 1;<----------------------------------------------------+
return ret; |
} | |
| |
[COPY OF VALUE] |
| |
| |
+---+ |
| |
| |
V |
void print_and_redefine_ints(int val1) { |
printf("val1 %dn"); ^ |
redefine_ints(&val1); | |
| +--------------------------------------------+ |
| | |
[POINTER AKA REFERENCE] | |
| | |
| | |
V | |
void redefine_ints(int *val1) { | |
*val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
} |
|
+---+
|
[VALUE CHANGED]
|
|
V
printf("rval1 %dn", val1);
printf("original val1 %dn", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
add a comment |
A draw may explain more than some text. I'll use only 1 get_val1()
in that example.
print_and_redefine_ints(get_val1());
|
|
[CALL]
|
|
V
int get_val1()
{
int ret = 1;<----------------------------------------------------+
return ret; |
} | |
| |
[COPY OF VALUE] |
| |
| |
+---+ |
| |
| |
V |
void print_and_redefine_ints(int val1) { |
printf("val1 %dn"); ^ |
redefine_ints(&val1); | |
| +--------------------------------------------+ |
| | |
[POINTER AKA REFERENCE] | |
| | |
| | |
V | |
void redefine_ints(int *val1) { | |
*val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
} |
|
+---+
|
[VALUE CHANGED]
|
|
V
printf("rval1 %dn", val1);
printf("original val1 %dn", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
add a comment |
A draw may explain more than some text. I'll use only 1 get_val1()
in that example.
print_and_redefine_ints(get_val1());
|
|
[CALL]
|
|
V
int get_val1()
{
int ret = 1;<----------------------------------------------------+
return ret; |
} | |
| |
[COPY OF VALUE] |
| |
| |
+---+ |
| |
| |
V |
void print_and_redefine_ints(int val1) { |
printf("val1 %dn"); ^ |
redefine_ints(&val1); | |
| +--------------------------------------------+ |
| | |
[POINTER AKA REFERENCE] | |
| | |
| | |
V | |
void redefine_ints(int *val1) { | |
*val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
} |
|
+---+
|
[VALUE CHANGED]
|
|
V
printf("rval1 %dn", val1);
printf("original val1 %dn", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
A draw may explain more than some text. I'll use only 1 get_val1()
in that example.
print_and_redefine_ints(get_val1());
|
|
[CALL]
|
|
V
int get_val1()
{
int ret = 1;<----------------------------------------------------+
return ret; |
} | |
| |
[COPY OF VALUE] |
| |
| |
+---+ |
| |
| |
V |
void print_and_redefine_ints(int val1) { |
printf("val1 %dn"); ^ |
redefine_ints(&val1); | |
| +--------------------------------------------+ |
| | |
[POINTER AKA REFERENCE] | |
| | |
| | |
V | |
void redefine_ints(int *val1) { | |
*val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
} |
|
+---+
|
[VALUE CHANGED]
|
|
V
printf("rval1 %dn", val1);
printf("original val1 %dn", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
edited 7 hours ago
answered 7 hours ago
CidCid
3,65021027
3,65021027
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
add a comment |
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
3
3
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
It's considered rude not to upvote an answer with ASCII art.
– Bathsheba
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
haha thank you @Bathsheba that was a pain to write
– Cid
7 hours ago
add a comment |
Is it possible to modify the contents of the memory address of the return (value) of a function?
No, it is not.
However, that is not the case here. In your code, the return values of get_val<n>()
function calls are stored in the function parameters int val1
, int val2
, int val3
. They are local to the called function. The lifetime of those variables are the function execution period.
Quoting C11
, chapter §6.2.1,
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [....]
and, from §6.9.1, Function definition,
Each parameter has automatic storage duration; its identifier is an lvalue
Thus, just like any other local variable, you can modify the content of those variables using their address.
add a comment |
Is it possible to modify the contents of the memory address of the return (value) of a function?
No, it is not.
However, that is not the case here. In your code, the return values of get_val<n>()
function calls are stored in the function parameters int val1
, int val2
, int val3
. They are local to the called function. The lifetime of those variables are the function execution period.
Quoting C11
, chapter §6.2.1,
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [....]
and, from §6.9.1, Function definition,
Each parameter has automatic storage duration; its identifier is an lvalue
Thus, just like any other local variable, you can modify the content of those variables using their address.
add a comment |
Is it possible to modify the contents of the memory address of the return (value) of a function?
No, it is not.
However, that is not the case here. In your code, the return values of get_val<n>()
function calls are stored in the function parameters int val1
, int val2
, int val3
. They are local to the called function. The lifetime of those variables are the function execution period.
Quoting C11
, chapter §6.2.1,
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [....]
and, from §6.9.1, Function definition,
Each parameter has automatic storage duration; its identifier is an lvalue
Thus, just like any other local variable, you can modify the content of those variables using their address.
Is it possible to modify the contents of the memory address of the return (value) of a function?
No, it is not.
However, that is not the case here. In your code, the return values of get_val<n>()
function calls are stored in the function parameters int val1
, int val2
, int val3
. They are local to the called function. The lifetime of those variables are the function execution period.
Quoting C11
, chapter §6.2.1,
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [....]
and, from §6.9.1, Function definition,
Each parameter has automatic storage duration; its identifier is an lvalue
Thus, just like any other local variable, you can modify the content of those variables using their address.
edited 4 hours ago
answered 7 hours ago
Sourav GhoshSourav Ghosh
109k14130188
109k14130188
add a comment |
add a comment |
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%2f54367396%2fmodify-the-contents-of-the-memory-address-of-the-return-of-a-function%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
1
redefine_ints(&val1, &val2, &val3);
here you're passing references to the variables, that's the only point where they get changed.– Federico klez Culloca
7 hours ago
2
You are not modifying the return value of a function directly.
print_and_redefine_ints(get_val1(), get_val2(), get_val3());
Here you pass the return values to another function by value.– Osiris
7 hours ago
I'm going to rephrase the question. Give me a few minutes to edit it.
– HenryGiraldo
7 hours ago
You are not modifying anything that are in
get_val1()
/2/3. But, inprint_and_redefine_ints(int val1, int val2, int val3) { ... }
you are copying the return values of the functions (because you are calling this way :print_and_redefine_ints(get_val1(), get_val2(), get_val3());
) I hope it helps– Cid
7 hours ago
1
The question title and the first sentence of the question don't make much sense
– Jabberwocky
7 hours ago