Fortran Functions with a pointer result in a normal assignment
After some discussion on the question found here Correct execution of Final routine in Fortran
I thought it will be useful to know when a function with a pointer result is appropriate to use with a normal or a pointer assignment. For example, given this simple function
function pointer_result(this)
implicit none
type(test_type),intent(in) pointer :: this
type(test_type), pointer :: pointer_result
allocate(pointer_result)
end function
I would normally do test=>pointer_result(test), where test has been declared with the pointer attribute. While the normal assignment test=pointer_result(test) is legal it means something different.
What does the normal assignment imply compared to the pointer assignment?
When does it make sense to use one or the other assignment?
oop fortran function-pointers intel-fortran
add a comment |
After some discussion on the question found here Correct execution of Final routine in Fortran
I thought it will be useful to know when a function with a pointer result is appropriate to use with a normal or a pointer assignment. For example, given this simple function
function pointer_result(this)
implicit none
type(test_type),intent(in) pointer :: this
type(test_type), pointer :: pointer_result
allocate(pointer_result)
end function
I would normally do test=>pointer_result(test), where test has been declared with the pointer attribute. While the normal assignment test=pointer_result(test) is legal it means something different.
What does the normal assignment imply compared to the pointer assignment?
When does it make sense to use one or the other assignment?
oop fortran function-pointers intel-fortran
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13
add a comment |
After some discussion on the question found here Correct execution of Final routine in Fortran
I thought it will be useful to know when a function with a pointer result is appropriate to use with a normal or a pointer assignment. For example, given this simple function
function pointer_result(this)
implicit none
type(test_type),intent(in) pointer :: this
type(test_type), pointer :: pointer_result
allocate(pointer_result)
end function
I would normally do test=>pointer_result(test), where test has been declared with the pointer attribute. While the normal assignment test=pointer_result(test) is legal it means something different.
What does the normal assignment imply compared to the pointer assignment?
When does it make sense to use one or the other assignment?
oop fortran function-pointers intel-fortran
After some discussion on the question found here Correct execution of Final routine in Fortran
I thought it will be useful to know when a function with a pointer result is appropriate to use with a normal or a pointer assignment. For example, given this simple function
function pointer_result(this)
implicit none
type(test_type),intent(in) pointer :: this
type(test_type), pointer :: pointer_result
allocate(pointer_result)
end function
I would normally do test=>pointer_result(test), where test has been declared with the pointer attribute. While the normal assignment test=pointer_result(test) is legal it means something different.
What does the normal assignment imply compared to the pointer assignment?
When does it make sense to use one or the other assignment?
oop fortran function-pointers intel-fortran
oop fortran function-pointers intel-fortran
asked Dec 30 '18 at 8:47
A2LBKA2LBK
1186
1186
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13
add a comment |
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13
add a comment |
1 Answer
1
active
oldest
votes
A normal assignment
test = pointer_result()
means that the value of the current target of test will be overwritten by the value pointed to by the resulting pointer. If test points to some invalid address (is undefined or null) the program will crash or produce undefined results. The anonymous target allocated by the function will have no pointer to it any more and the memory will be leaked.
There is hardly any legitimate use for this, but it is likely to happen when one makes a typo and writes = instead of =>. It is a very easy one to make and several style guides recommend to never use pointer functions.
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
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%2f53976300%2ffortran-functions-with-a-pointer-result-in-a-normal-assignment%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
A normal assignment
test = pointer_result()
means that the value of the current target of test will be overwritten by the value pointed to by the resulting pointer. If test points to some invalid address (is undefined or null) the program will crash or produce undefined results. The anonymous target allocated by the function will have no pointer to it any more and the memory will be leaked.
There is hardly any legitimate use for this, but it is likely to happen when one makes a typo and writes = instead of =>. It is a very easy one to make and several style guides recommend to never use pointer functions.
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
add a comment |
A normal assignment
test = pointer_result()
means that the value of the current target of test will be overwritten by the value pointed to by the resulting pointer. If test points to some invalid address (is undefined or null) the program will crash or produce undefined results. The anonymous target allocated by the function will have no pointer to it any more and the memory will be leaked.
There is hardly any legitimate use for this, but it is likely to happen when one makes a typo and writes = instead of =>. It is a very easy one to make and several style guides recommend to never use pointer functions.
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
add a comment |
A normal assignment
test = pointer_result()
means that the value of the current target of test will be overwritten by the value pointed to by the resulting pointer. If test points to some invalid address (is undefined or null) the program will crash or produce undefined results. The anonymous target allocated by the function will have no pointer to it any more and the memory will be leaked.
There is hardly any legitimate use for this, but it is likely to happen when one makes a typo and writes = instead of =>. It is a very easy one to make and several style guides recommend to never use pointer functions.
A normal assignment
test = pointer_result()
means that the value of the current target of test will be overwritten by the value pointed to by the resulting pointer. If test points to some invalid address (is undefined or null) the program will crash or produce undefined results. The anonymous target allocated by the function will have no pointer to it any more and the memory will be leaked.
There is hardly any legitimate use for this, but it is likely to happen when one makes a typo and writes = instead of =>. It is a very easy one to make and several style guides recommend to never use pointer functions.
answered Dec 30 '18 at 9:18
Vladimir FVladimir F
40.2k44071
40.2k44071
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
add a comment |
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
Thanks that made it quite more clearer regarding the differences. But I was told that there could be some cases where the normal assignment could be useful, Don't know what they could be, though.
– A2LBK
Dec 30 '18 at 17:03
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
@A2LBK Perhaps between pointer variables, to copy the value. I can't imagine it for a pointer returning function.
– Vladimir F
Dec 31 '18 at 9:46
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%2f53976300%2ffortran-functions-with-a-pointer-result-in-a-normal-assignment%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
Do you know the difference between assignment and pointer assignment? If you do, can you explain more about what your final question is wanting?
– francescalus
Dec 30 '18 at 8:58
I think, a pointer assignment is when you associate a pointer to the right hand side, which could be an expression, routine, variable etc. But not sure what it really means when you do normal assignment. What I want is to know what the normal assignment really mean, what it implies and when is it useful to use it - and here specifically with application to functions with pointer results.
– A2LBK
Dec 30 '18 at 9:13