Initialising a pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mail1779205@bupkiss.net

    Initialising a pointer


    I (certainly) hope I know what this function does:

    char *fun(void){
    char *ptr = "Hello World";

    return ptr;
    }

    It returns a pointer to a string stored somewhere in the memory and is
    read-only. Does the following function do the same thing and is the
    code legal at all, that is, is the pointer
    initialised to point to some string that is read-only?

    char *fun(void){
    char *ptr;

    ptr = "Hello World";

    return ptr;
    }

    I have looked in the FAQ, apologising if it's there somewhere. BTW,
    Steve Summit thanks for this very useful resource.

    Jesper

  • Ian Collins

    #2
    Re: Initialising a pointer

    mail1779205@bup kiss.net wrote:
    I (certainly) hope I know what this function does:
    >
    char *fun(void){
    char *ptr = "Hello World";
    >
    return ptr;
    }
    >
    It would be much safer and clearer to make that

    const char *fun(void)
    It returns a pointer to a string stored somewhere in the memory and is
    read-only. Does the following function do the same thing and is the
    code legal at all, that is, is the pointer
    initialised to point to some string that is read-only?
    >
    char *fun(void){
    char *ptr;
    >
    ptr = "Hello World";
    >
    return ptr;
    }
    >
    Yes, the there isn't any difference.

    --
    Ian Collins.

    Comment

    • Richard Heathfield

      #3
      Re: Initialising a pointer

      mail1779205@bup kiss.net said:
      >
      I (certainly) hope I know what this function does:
      >
      char *fun(void){
      char *ptr = "Hello World";
      >
      return ptr;
      }
      >
      It returns a pointer to a string stored somewhere in the memory and is
      read-only.
      Yes. An improvement:

      const char *fun(void){
      const char *ptr = "Hello World";

      return ptr;
      }

      This will discourage callers from trying to modify the string to which
      the function returns a pointer.

      Does the following function do the same thing and is the
      code legal at all, that is, is the pointer
      initialised to point to some string that is read-only?
      >
      char *fun(void){
      char *ptr;
      >
      ptr = "Hello World";
      >
      return ptr;
      }
      Yes, that function does the same thing, yes, the code is legal, (yes, it
      is capable of being improved in the same way as the previous one), and
      no, the pointer is not initialised. Initialisation is the providing of
      a value to an object at the time it is defined. In your example, this
      doesn't happen - rather, the object is /assigned/ a value in the
      following statement. But yes, it still ends up pointing to a read-only
      string.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at the above domain, - www.

      Comment

      • google.bo@olive-it.ch

        #4
        Re: Initialising a pointer

        On Apr 19, 1:24 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
        mail1779...@bup kiss.net said:
        >
        >
        >
        I (certainly) hope I know what this function does:
        >
        char *fun(void){
        char *ptr = "Hello World";
        >
        return ptr;
        }
        >
        It returns a pointer to a string stored somewhere in the memory and is
        read-only.
        >
        I think this will deliver a pointer to the stack segment as ptr is a
        local variable. Pointers to stack segment are not really a good idea
        as the content may be overwritten at any time (i.e. with next function
        call)
        Yes. An improvement:
        >
        const char *fun(void){
        const char *ptr = "Hello World";
        >
        return ptr;
        >
        }
        >
        This will discourage callers from trying to modify the string to which
        the function returns a pointer.
        >
        But does not change the fact that the value pointed to may be
        overwritten at any moment, as the content lies on the stack.
        Did you check the code with gdb (or any debugger?)

        Comment

        • Ian Collins

          #5
          Re: Initialising a pointer

          google.bo@olive-it.ch wrote:
          On Apr 19, 1:24 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
          >
          >>mail1779...@b upkiss.net said:
          >>
          >>
          >>
          >>
          >>>I (certainly) hope I know what this function does:
          >>
          >>>char *fun(void){
          >> char *ptr = "Hello World";
          >>
          >> return ptr;
          >>>}
          >>
          >>>It returns a pointer to a string stored somewhere in the memory and is
          >>>read-only.
          >>
          I think this will deliver a pointer to the stack segment as ptr is a
          local variable. Pointers to stack segment are not really a good idea
          as the content may be overwritten at any time (i.e. with next function
          call)
          >
          No, it returns a pointer to a string literal, which isn't an automatic
          variable. Had it been written as

          char *fun(void){
          char ptr[] = "Hello World";

          return ptr;
          }

          Your observation would be correct.

          --
          Ian Collins.

          Comment

          • Keith Thompson

            #6
            Re: Initialising a pointer

            mail1779205@bup kiss.net writes:
            I (certainly) hope I know what this function does:
            >
            char *fun(void){
            char *ptr = "Hello World";
            >
            return ptr;
            }
            >
            It returns a pointer to a string stored somewhere in the memory and is
            read-only. Does the following function do the same thing and is the
            code legal at all, that is, is the pointer
            initialised to point to some string that is read-only?
            >
            char *fun(void){
            char *ptr;
            >
            ptr = "Hello World";
            >
            return ptr;
            }
            As others have said, they both do the same thing.

            Note that the memory used to store the string may or may not be
            "read-only" (depending, in part, on what you mean by that phrase). A
            compiler is *allowed* to make string literals writable, and in fact
            some compilers do this. On the other hand, a compiler is also allowed
            to use OS-level memory protection to make string literals read-only
            (or, most likely in an embedded system, even to store them physically
            in ROM). Attempting to modify a string literal invokes undefined
            behavior.

            But thinking of it as "read-only" is a very good habit -- as long as
            you don't depend on the compiler to enforce it for you.

            Declaring the pointer as "const" is also a good idea. If you do that,
            the compiler *will* enforce the "read-only-ness" of the string
            literal, at least if you access it via that pointer.
            I have looked in the FAQ, apologising if it's there somewhere. BTW,
            Steve Summit thanks for this very useful resource.
            Thanks for mentioning that; too many posters don't bother.

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

            Comment

            • Jens Thoms Toerring

              #7
              Re: Initialising a pointer

              google.bo@olive-it.ch wrote:
              On Apr 19, 1:24 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
              mail1779...@bup kiss.net said:


              I (certainly) hope I know what this function does:
              char *fun(void){
              char *ptr = "Hello World";
              return ptr;
              }
              It returns a pointer to a string stored somewhere in the memory and is
              read-only.
              I think this will deliver a pointer to the stack segment as ptr is a
              local variable.
              What the hell is a "stack segment"? In C there's no notion of a stack,
              which is an implemention detail. But while 'ptr' might be living on the
              stack (or as well somewhere else as well) the return value of the func-
              tion is the _value_ stored in 'ptr', not 'ptr'. so it doesn't matter a
              thing where 'ptr' was living, the caller will receive the address 'ptr'
              was pointing to, no matter if 'ptr' still exists after the function re-
              turned. And the address that was formerly stored in 'ptr' is the address
              of some read-only memory as the OP wrote.
              Pointers to stack segment are not really a good idea
              as the content may be overwritten at any time (i.e. with next function
              call)
              Would you care to elaborate what this is supposed to mean?
              Yes. An improvement:

              const char *fun(void){
              const char *ptr = "Hello World";

              return ptr;

              }

              This will discourage callers from trying to modify the string to which
              the function returns a pointer.
              But does not change the fact that the value pointed to may be
              overwritten at any moment, as the content lies on the stack.
              Did you check the code with gdb (or any debugger?)
              The value pointed to will not be overwritten, just maybe the pointer
              that temporarily held the address to the string literal "Hello
              world". But 'ptr'doesn't get returned but just what was its value.
              Would you be more happy with

              const char *fun( void ) {
              return "Hello World";
              }

              which does exactly the same job as the slightly longer version
              above?
              Regards, Jens
              --
              \ Jens Thoms Toerring ___ jt@toerring.de
              \______________ ____________ http://toerring.de

              Comment

              • Old Wolf

                #8
                Re: Initialising a pointer

                On Apr 19, 11:35 am, google...@olive-it.ch wrote:
                Richard Heathfield wrote:
                const char *fun(void){
                const char *ptr = "Hello World";
                >
                return ptr;
                }
                >
                >
                But does not change the fact that the value pointed to may be
                overwritten at any moment, as the content lies on the stack.
                Did you check the code with gdb (or any debugger?)
                Did *you* check it with a debugger?
                Perhaps you could paste your debugger output.

                Comment

                • lovecreatesbea...@gmail.com

                  #9
                  Re: Initialising a pointer

                  On Apr 19, 7:45 am, Ian Collins <ian-n...@hotmail.co mwrote:
                  google...@olive-it.ch wrote:
                  On Apr 19, 1:24 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  >
                  >mail1779...@bu pkiss.net said:
                  >
                  >>I (certainly) hope I know what this function does:
                  >
                  >>char *fun(void){
                  > char *ptr = "Hello World";
                  >
                  > return ptr;
                  >>}
                  >
                  >>It returns a pointer to a string stored somewhere in the memory and is
                  >>read-only.
                  >
                  I think this will deliver a pointer to the stack segment as ptr is a
                  local variable. Pointers to stack segment are not really a good idea
                  as the content may be overwritten at any time (i.e. with next function
                  call)
                  >
                  No, it returns a pointer to a string literal, which isn't an automatic
                  variable. Had it been written as
                  >
                  char *fun(void){
                  char ptr[] = "Hello World";
                  >
                  return ptr;
                  >
                  }
                  I don't know if a string literal live all the life cycle of the
                  program? Or does the standard gurantee this. Does static make this
                  clear?

                  char *fun(void){
                  static char *ptr = "Hello World";
                  /*or even: static char ptr[] = "Hello World";*/
                  return ptr;
                  }

                  Comment

                  • Ian Collins

                    #10
                    Re: Initialising a pointer

                    lovecreatesbea. ..@gmail.com wrote:
                    On Apr 19, 7:45 am, Ian Collins <ian-n...@hotmail.co mwrote:
                    >>
                    >>No, it returns a pointer to a string literal, which isn't an automatic
                    >>variable. Had it been written as
                    >>
                    >>char *fun(void){
                    > char ptr[] = "Hello World";
                    >>
                    > return ptr;
                    >>
                    >>}
                    >
                    I don't know if a string literal live all the life cycle of the
                    program? Or does the standard gurantee this. Does static make this
                    clear?
                    >
                    They do, although I can't remember off hand where this is explicitly
                    stated in the standard. The static keyword is unnecessary.

                    --
                    Ian Collins.

                    Comment

                    • Keith Thompson

                      #11
                      Re: Initialising a pointer

                      "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ites:
                      [...]
                      I don't know if a string literal live all the life cycle of the
                      program? Or does the standard gurantee this. Does static make this
                      clear?
                      Yes, a the data for a string literal is statically allocated.
                      C99 6.4.5p5:

                      In translation phase 7, a byte or code of value zero is appended
                      to each multibyte character sequence that results from a string
                      literal or literals.66) The multibyte character sequence is then
                      used to initialize an array of static storage duration and length
                      just sufficient to contain the sequence.
                      char *fun(void){
                      static char *ptr = "Hello World";
                      /*or even: static char ptr[] = "Hello World";*/
                      return ptr;
                      }
                      No, declaring "static char *ptr" just causes the pointer object to be
                      statically allocated; it doesn't affect the string.

                      Given:

                      static char ptr[] = "Hello World";

                      the array ("ptr" is a bad name for an array) is statically allocated,
                      but here the string literal is used to specify the initialization for
                      the array. If you omitted the "static" keyword, the array would have
                      automatic allocation.

                      String literals are already static. There's no way to specify a
                      storage class for a string literal, since it's not an object with a
                      name. You can use the "static" keyword in various places, but it
                      always applies to something other than the string literal.

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

                      Comment

                      • Chris Dollin

                        #12
                        Re: Initialising a pointer

                        mail1779205@bup kiss.net wrote:
                        >
                        I (certainly) hope I know what this function does:
                        >
                        char *fun(void){
                        char *ptr = "Hello World";
                        >
                        return ptr;
                        }
                        >
                        It returns a pointer to a string stored somewhere in the memory and is
                        read-only. Does the following function do the same thing and is the
                        code legal at all, that is, is the pointer
                        initialised to point to some string that is read-only?
                        >
                        char *fun(void){
                        char *ptr;
                        >
                        ptr = "Hello World";
                        >
                        return ptr;
                        }
                        Others have said "yes"; I'd just like to add that

                        char *fun(void) { return "Hello World"; }

                        does the same as well.

                        --
                        There' no hortage of vowel on Uenet.

                        Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                        registered no: 690597 England Berks RG12 1HN

                        Comment

                        • Ian Collins

                          #13
                          Re: Initialising a pointer

                          Chris Dollin wrote:
                          mail1779205@bup kiss.net wrote:
                          >
                          >
                          >>I (certainly) hope I know what this function does:
                          >>
                          >>char *fun(void){
                          > char *ptr = "Hello World";
                          >>
                          > return ptr;
                          >>}
                          >>
                          >>It returns a pointer to a string stored somewhere in the memory and is
                          >>read-only. Does the following function do the same thing and is the
                          >>code legal at all, that is, is the pointer
                          >>initialised to point to some string that is read-only?
                          >>
                          >>char *fun(void){
                          > char *ptr;
                          >>
                          > ptr = "Hello World";
                          >>
                          > return ptr;
                          >>}
                          >
                          >
                          Others have said "yes"; I'd just like to add that
                          >
                          char *fun(void) { return "Hello World"; }
                          >
                          does the same as well.
                          >
                          But

                          const char *fun(void) { return "Hello World"; }

                          would be better.

                          --
                          Ian Collins.

                          Comment

                          • Chris Dollin

                            #14
                            Re: Initialising a pointer

                            Ian Collins wrote:
                            Chris Dollin wrote:
                            >Others have said "yes"; I'd just like to add that
                            >>
                            > char *fun(void) { return "Hello World"; }
                            >>
                            >does the same as well.
                            >>
                            But
                            >
                            const char *fun(void) { return "Hello World"; }
                            >
                            would be better.
                            I'll not dispute that.

                            --
                            "It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/

                            Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                            registered no: 690597 England Berks RG12 1HN

                            Comment

                            • mail1779205@bupkiss.net

                              #15
                              Re: Initialising a pointer


                              Thanks everyone for all the comments. Naturally, the 'const' qualifier
                              should be there!

                              Cheers, Jesper


                              Comment

                              Working...