const

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

    const

    Sometimes I've seen something like:

    const char* ReturnNull()
    {
    return "";
    }

    Is this legal? Why? Is this because the string is a constant?

    And... Is this legal:

    const int* ReturnZero()
    {
    const int i = 0;
    return &i;
    }


  • Massimiliano Alberti

    #2
    Re: const

    > const char* ReturnNull()[color=blue]
    > {
    > return "";
    > }[/color]

    Wrong example:

    const char* ReturnNull()
    {
    const char *p = "";
    return p;
    }


    Comment

    • Ron Natalie

      #3
      Re: const


      "Massimilia no Alberti" <xanatos@geocit ies.com> wrote in message news:2M5%b.2109 4$gk.899378@new s3.tin.it...[color=blue]
      > Sometimes I've seen something like:
      >
      > const char* ReturnNull()
      > {
      > return "";
      > }[/color]

      This is legal because a string literal is persistant. It's a bunch of const chars
      at an undetermined location. They are not local to the block.
      [color=blue]
      > const int* ReturnZero()
      > {
      > const int i = 0;
      > return &i;
      > }[/color]

      This will compile, but any attempt to use the returned pointer will be problematic
      as in this case the value is a pointer to an object that doesn't exist anymore.

      Comment

      • Rolf Magnus

        #4
        Re: const

        Massimiliano Alberti wrote:
        [color=blue]
        > Sometimes I've seen something like:
        >
        > const char* ReturnNull()
        > {
        > return "";
        > }
        >
        > Is this legal?[/color]

        Yes.
        [color=blue]
        > Why?[/color]

        What makes you think it's not?
        [color=blue]
        > Is this because the string is a constant?[/color]

        No, it's because it's a literal, and string literals exist through the
        whole life time of the program.
        [color=blue]
        > And... Is this legal:
        >
        > const int* ReturnZero()
        > {
        > const int i = 0;
        > return &i;
        > }[/color]

        No. You're returning a pointer to a local variable that gets destroyed
        when the function returns.

        Comment

        • Leor Zolman

          #5
          Re: const

          On Wed, 25 Feb 2004 13:42:35 -0500, "Ron Natalie" <ron@sensor.com > wrote:[color=blue]
          >[color=green]
          >> const int* ReturnZero()
          >> {
          >> const int i = 0;
          >> return &i;
          >> }[/color]
          >
          >This will compile, but any attempt to use the returned pointer will be problematic
          >as in this case the value is a pointer to an object that doesn't exist anymore.[/color]

          It will compile, but every self-respecting compiler will at least issue a
          warning about it. If you don't see a warning, consider getting a newer
          compiler (or tightening up the warning level options).
          -leor


          Leor Zolman
          BD Software
          leor@bdsoft.com
          www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
          C++ users: Download BD Software's free STL Error Message
          Decryptor at www.bdsoft.com/tools/stlfilt.html

          Comment

          • Nick Hounsome

            #6
            Re: const


            "Massimilia no Alberti" <xanatos@geocit ies.com> wrote in message
            news:8Q5%b.2111 2$gk.900093@new s3.tin.it...[color=blue][color=green]
            > > const char* ReturnNull()
            > > {
            > > return "";
            > > }[/color]
            >
            > Wrong example:
            >
            > const char* ReturnNull()
            > {
            > const char *p = "";
            > return p;
            > }[/color]

            No - his example is fine - yours just wastes a bit of stack space to acheive
            exactly
            the same thing.

            Actually according to the principle of calling it what it is it should be

            const char* ReturnNull() { return NULL; }
            or
            const char* ReturnEmptyStri ng() { return ""; }
            [color=blue]
            >
            >[/color]


            Comment

            • Rolf Magnus

              #7
              Re: const

              Nick Hounsome wrote:
              [color=blue]
              >
              > "Massimilia no Alberti" <xanatos@geocit ies.com> wrote in message
              > news:8Q5%b.2111 2$gk.900093@new s3.tin.it...[color=green][color=darkred]
              >> > const char* ReturnNull()
              >> > {
              >> > return "";
              >> > }[/color]
              >>
              >> Wrong example:
              >>
              >> const char* ReturnNull()
              >> {
              >> const char *p = "";
              >> return p;
              >> }[/color]
              >
              > No - his example is fine - yours just wastes a bit of stack space to
              > acheive exactly the same thing.[/color]

              The "his" and the "yours" you are talking about are the same person.

              Comment

              • Roger Leigh

                #8
                Re: const

                "Massimilia no Alberti" <xanatos@geocit ies.com> writes:
                [color=blue]
                > Sometimes I've seen something like:
                >
                > const char* ReturnNull()
                > {
                > return "";
                > }
                >
                > Is this legal? Why? Is this because the string is a constant?[/color]

                Yes.

                [OT: On a UNIX-like OS (e.g. Linux/ELF), this will probably be put in
                the .rodata (read-only data) section of the executable. This part is
                mapped into memory with read-only permissions, so if you actually
                attempt to modify it, you'll get a SEGV.]
                [color=blue]
                > And... Is this legal:
                >
                > const int* ReturnZero()
                > {
                > const int i = 0;
                > return &i;
                > }[/color]

                It's "legal" in that it will compile. However, it's wrong because i
                will be destroyed when it goes out of scope when the function returns.
                This will leave you with a pointer to a nonexistent object, and
                probably a crash.


                --
                Roger Leigh

                Printing on GNU/Linux? http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.


                -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                Comment

                Working...