Fortran Functions with a pointer result in a normal assignment












2















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?










share|improve this question























  • 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
















2















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?










share|improve this question























  • 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














2












2








2








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









3














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.






share|improve this answer
























  • 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
















3














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.






share|improve this answer
























  • 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














3












3








3







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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%2f53976300%2ffortran-functions-with-a-pointer-result-in-a-normal-assignment%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

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'