function return pointer of int?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Davy

    function return pointer of int?

    Hi all,

    I am writing a function, which return the pointer of the int. But it
    seems to be wrong. Any suggestion?

    int * get_p_t(int t) {
    return &t;
    }

    int main()
    {
    printf("v1\n");
    int t = 5;
    int * p_t[2];

    p_t[0] = &t; // right
    p_t[1] = get_p_t(t); //wrong

    return 0;
    }

    Best regards,
    Davy
  • Ian Collins

    #2
    Re: function return pointer of int?

    Davy wrote:
    Hi all,
    >
    I am writing a function, which return the pointer of the int. But it
    seems to be wrong. Any suggestion?
    >
    int * get_p_t(int t) {
    return &t;
    }
    >
    t is effectively a local variable in get_p_t. So the address you return
    will be invalid after the function returns.

    --
    Ian Collins

    Comment

    • Davy

      #3
      Re: function return pointer of int?

      On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      Davy wrote:
      Hi all,
      >
      I am writing a function, which return the pointer of the int. But it
      seems to be wrong. Any suggestion?
      >
      int * get_p_t(int t) {
          return &t;
      }
      >
      t is effectively a local variable in get_p_t.  So the address you return
      will be invalid after the function returns.
      Hi Ian, thank you,

      But how can I get the address of t in the main() scope, if I want to
      use function?
      >
      --
      Ian Collins

      Comment

      • Ian Collins

        #4
        Re: function return pointer of int?

        Davy wrote:
        On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >Davy wrote:
        >>Hi all,
        >>I am writing a function, which return the pointer of the int. But it
        >>seems to be wrong. Any suggestion?
        >>int * get_p_t(int t) {
        >> return &t;
        >>}
        >t is effectively a local variable in get_p_t. So the address you return
        >will be invalid after the function returns.
        >
        Hi Ian, thank you,
        >
        But how can I get the address of t in the main() scope, if I want to
        use function?
        You can't. You get the address of where t was.

        --
        Ian Collins

        Comment

        • DJ Dharme

          #5
          Re: function return pointer of int?

          On Nov 5, 12:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          Davy wrote:
          On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          Davy wrote:
          >Hi all,
          >I am writing a function, which return the pointer of the int. But it
          >seems to be wrong. Any suggestion?
          >int * get_p_t(int t) {
          >    return &t;
          >}
          t is effectively a local variable in get_p_t.  So the address you return
          will be invalid after the function returns.
          >
          Hi Ian, thank you,
          >
          But how can I get the address of t in the main() scope, if I want to
          use function?
          >
          You can't.  You get the address of where t was.
          >
          --
          Ian Collins
          Still he can get a reference or a pointer to t as the input parameter
          and return a valid pointer.

          Comment

          • Ian Collins

            #6
            Re: function return pointer of int?

            DJ Dharme wrote:
            On Nov 5, 12:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >Davy wrote:
            >>On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >>>Davy wrote:
            >>>>Hi all,
            >>>>I am writing a function, which return the pointer of the int. But it
            >>>>seems to be wrong. Any suggestion?
            >>>>int * get_p_t(int t) {
            >>>> return &t;
            >>>>}
            >>>t is effectively a local variable in get_p_t. So the address you return
            >>>will be invalid after the function returns.
            >>Hi Ian, thank you,
            >>But how can I get the address of t in the main() scope, if I want to
            >>use function?
            >You can't. You get the address of where t was.
            >>
            >
            Still he can get a reference or a pointer to t as the input parameter
            and return a valid pointer.
            Do what?

            --
            Ian Collins

            Comment

            • Nick Keighley

              #7
              Re: function return pointer of int?

              On 5 Nov, 07:06, Davy <zhushe...@gmai l.comwrote:
              On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
              >
              Davy wrote:
              Hi all,
              >
              I am writing a function, which return the pointer of the int. But it
              seems to be wrong. Any suggestion?
              >
              int * get_p_t(int t) {
                  return &t;
              }
              >
              t is effectively a local variable in get_p_t.  So the address you return
              will be invalid after the function returns.
              >
              Hi Ian, thank you,
              >
              But how can I get the address of t in the main() scope, if I want to
              use function?
              as Ian Collins said "you can't".

              Why do you want to do this? I'm not being awkward if you explain
              your larger problem, the context in which you want the address of t
              we might be able to suggest a workaround.

              int main (void)
              {
              printf("v1\n");
              int t = 5;
              int * p_t[2];

              p_t[0] = &t;
              p_t[1] = get_p_t(t);

              /*** you have the address of t by using &t.
              why do you want a function to do this? */

              return 0;
              }



              --
              Nick Keighley


              Comment

              • vippstar@gmail.com

                #8
                Re: function return pointer of int?

                On Nov 5, 9:31 am, DJ Dharme <donjuandharmap ...@gmail.comwr ote:
                On Nov 5, 12:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                >
                >
                >
                Davy wrote:
                On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                >Davy wrote:
                >>Hi all,
                >>I am writing a function, which return the pointer of the int. But it
                >>seems to be wrong. Any suggestion?
                >>int * get_p_t(int t) {
                >> return &t;
                >>}
                >t is effectively a local variable in get_p_t. So the address you return
                >will be invalid after the function returns.
                >
                Hi Ian, thank you,
                >
                But how can I get the address of t in the main() scope, if I want to
                use function?
                >
                You can't. You get the address of where t was.
                >
                --
                Ian Collins
                >
                Still he can get a reference or a pointer to t as the input parameter
                and return a valid pointer.
                Please don't quote signatures. (the text after --)
                Also, no, he can't. I think his function is valid, ie this code is
                valid:

                int *foo(int i) { return &i; }

                However even evaluating foo(anything) invokes undefined behavior,
                since the object pointed to by the pointer is over its lifetime, and
                the pointer becomes indeterminate. (either unspecified value or trap
                representation)

                Comment

                • James Kuyper

                  #9
                  Re: function return pointer of int?

                  Davy wrote:
                  On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                  >Davy wrote:
                  >>Hi all,
                  >>I am writing a function, which return the pointer of the int. But it
                  >>seems to be wrong. Any suggestion?
                  >>int * get_p_t(int t) {
                  >> return &t;
                  >>}
                  >t is effectively a local variable in get_p_t. So the address you return
                  >will be invalid after the function returns.
                  >
                  Hi Ian, thank you,
                  >
                  But how can I get the address of t in the main() scope, if I want to
                  use function?
                  int * get_p_int(int *pt) { return pt;}

                  int main(void)
                  {
                  int t;
                  int *p = get_p_int(&t);


                  Of course, this is pretty pointless; get_p_int(&t) doesn't get you
                  anything that &t doesn't already get you. So you might as well writer

                  int *p = &t;

                  So, what is the real problem you're trying to solve?

                  Comment

                  • Richard Herring

                    #10
                    Re: function return pointer of int?

                    In message <0pfQk.3184$225 .2847@nwrddc02. gnilink.net>, James Kuyper
                    <jameskuyper@ve rizon.netwrites
                    >Davy wrote:
                    >On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                    >>Davy wrote:
                    >>>Hi all,
                    >>>I am writing a function, which return the pointer of the int. But it
                    >>>seems to be wrong. Any suggestion?
                    >>>int * get_p_t(int t) {
                    >>> return &t;
                    >>>}
                    >>t is effectively a local variable in get_p_t. So the address you return
                    >>will be invalid after the function returns.
                    > Hi Ian, thank you,
                    > But how can I get the address of t in the main() scope, if I want to
                    >use function?
                    >
                    >int * get_p_int(int *pt) { return pt;}
                    >
                    >int main(void)
                    >{
                    int t;
                    int *p = get_p_int(&t);
                    >
                    >
                    >Of course, this is pretty pointless; get_p_int(&t) doesn't get you
                    >anything that &t doesn't already get you. So you might as well writer
                    >
                    int *p = &t;
                    >
                    >So, what is the real problem you're trying to solve?
                    Understanding the difference between C++ and C? Note the crosspost.

                    --
                    Richard Herring

                    Comment

                    • James Kanze

                      #11
                      Re: function return pointer of int?

                      On Nov 5, 8:06 am, Davy <zhushe...@gmai l.comwrote:
                      On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                      Davy wrote:
                      I am writing a function, which return the pointer of the
                      int. But it seems to be wrong. Any suggestion?
                      int * get_p_t(int t) {
                      return &t;
                      }
                      t is effectively a local variable in get_p_t. So the
                      address you return will be invalid after the function
                      returns.
                      But how can I get the address of t in the main() scope, if I
                      want to use function?
                      Didn't you read what Ian wrote? t doesn't exist except when
                      you're executing get_p_t. And there's obviously no way to get
                      the address of something which doesn't exist.

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      • James Kuyper

                        #12
                        Re: function return pointer of int?

                        Richard Herring wrote:
                        In message <0pfQk.3184$225 .2847@nwrddc02. gnilink.net>, James Kuyper
                        <jameskuyper@ve rizon.netwrites
                        >Davy wrote:
                        >>On Nov 5, 2:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                        >>>Davy wrote:
                        >>>>Hi all,
                        >>>>I am writing a function, which return the pointer of the int. But it
                        >>>>seems to be wrong. Any suggestion?
                        >>>>int * get_p_t(int t) {
                        >>>> return &t;
                        >>>>}
                        >>>t is effectively a local variable in get_p_t. So the address you
                        >>>return
                        >>>will be invalid after the function returns.
                        >> Hi Ian, thank you,
                        >> But how can I get the address of t in the main() scope, if I want to
                        >>use function?
                        >>
                        >int * get_p_int(int *pt) { return pt;}
                        >>
                        >int main(void)
                        >{
                        > int t;
                        > int *p = get_p_int(&t);
                        >>
                        >>
                        >Of course, this is pretty pointless; get_p_int(&t) doesn't get you
                        >anything that &t doesn't already get you. So you might as well writer
                        >>
                        > int *p = &t;
                        >>
                        >So, what is the real problem you're trying to solve?
                        >
                        Understanding the difference between C++ and C? Note the crosspost.
                        You're right, I didn't notice the cross-post. There is indeed a C++
                        solution using references:

                        int *get_p_int(int &t) { return &t;}

                        Because of the syntactic sugar of references, the C++ version appears
                        marginally less pointless than the C version, but I think that's mostly
                        an illusion - the machine code generated for either version should be
                        functionally equivalent. However, precisely because of the cross-post, I
                        think that use of C++ references would be inappropriate; the cross-post
                        implies a desire for a solution that works in either language.

                        Comment

                        • Kenny McCormack

                          #13
                          Re: function return pointer of int?

                          In article <b9b1c923-1dcd-4765-8e7e-7f223a9db922@p3 1g2000prf.googl egroups.com>,
                          James Kanze <james.kanze@gm ail.comwrote:
                          ....
                          >Didn't you read what Ian wrote? t doesn't exist except when
                          >you're executing get_p_t.
                          True.
                          >And there's obviously no way to get
                          >the address of something which doesn't exist.
                          Not true. The Munsters don't exist, but their address is "1313
                          Mockingbird Lane". So, it is possible to have the address of something
                          that doesn't exist. De-referencing such an address does, of course,
                          lead to undefined behavior.


                          Comment

                          • Keith Thompson

                            #14
                            Re: function return pointer of int?

                            vippstar@gmail. com writes:
                            [...]
                            Also, no, he can't. I think his function is valid, ie this code is
                            valid:
                            >
                            int *foo(int i) { return &i; }
                            >
                            However even evaluating foo(anything) invokes undefined behavior,
                            since the object pointed to by the pointer is over its lifetime, and
                            the pointer becomes indeterminate. (either unspecified value or trap
                            representation)
                            It's "valid" only in the narrow sense that it doesn't violate any
                            constraint or syntax rule. But "valid" isn't a word I'd apply to a
                            function that can't be called without invoking undefined behavior.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • James Kanze

                              #15
                              Re: function return pointer of int?

                              On Nov 5, 3:00 pm, gaze...@shell.x mission.com (Kenny McCormack) wrote:
                              In article
                              <b9b1c923-1dcd-4765-8e7e-7f223a9db...@p3 1g2000prf.googl egroups.com>,
                              James Kanze <james.ka...@gm ail.comwrote:
                              ...
                              Didn't you read what Ian wrote? t doesn't exist except when
                              you're executing get_p_t.
                              True.
                              And there's obviously no way to get the address of something
                              which doesn't exist.
                              Not true. The Munsters don't exist, but their address is
                              "1313 Mockingbird Lane".
                              Only in the imaginary world where they exist.
                              So, it is possible to have the address of something that
                              doesn't exist. De-referencing such an address does, of
                              course, lead to undefined behavior.
                              First, you can't have an address of something that doesn't
                              exist. You can perhaps have a pointer whose bit pattern
                              corresponds to the address where it was, but it's no longer a
                              valid address, and even copying it is undefined behavior.

                              --
                              James Kanze (GABI Software) email:james.kan ze@gmail.com
                              Conseils en informatique orientée objet/
                              Beratung in objektorientier ter Datenverarbeitu ng
                              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                              Comment

                              Working...