return statement and delete

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

    return statement and delete

    Hi,

    consider this:


    char* aClass::printHe llo()
    {
    char* ptr = new char[6];
    ptr = "Hello";
    return ptr;
    delete[] ptr;
    }

    Is it possible to return ptr and then delete[] ptr afterwards. I think the
    delete[] ptr statement will not be executed after the return statement. Any
    workaround for this?


    REgards
    ML


  • Josephine Schafer

    #2
    Re: return statement and delete


    "A" <A@iprimus.com. au> wrote in message news:3f6e7175_1 @news.iprimus.c om.au...[color=blue]
    > Hi,
    >
    > consider this:
    >
    >
    > char* aClass::printHe llo()
    > {
    > char* ptr = new char[6];
    > ptr = "Hello";
    > return ptr;
    > delete[] ptr;
    > }
    >
    > Is it possible to return ptr and then delete[] ptr afterwards. I think the
    > delete[] ptr statement will not be executed after the return statement.[/color]

    That's correct.
    [color=blue]
    >Any workaround for this?
    >[/color]
    call delete from the function that called printHello.

    As a library provider it is not always desirable to write such a code since
    client code then needs to remember to delete the pointer returned.

    HTH,
    J.Schafer





    Comment

    • Gregg

      #3
      Re: return statement and delete

      "A" <A@iprimus.com. au> wrote in message
      news:3f6e7175_1 @news.iprimus.c om.au...[color=blue]
      > Hi,
      >
      > consider this:
      >
      >
      > char* aClass::printHe llo()
      > {
      > char* ptr = new char[6];
      > ptr = "Hello";
      > return ptr;
      > delete[] ptr;
      > }
      >
      > Is it possible to return ptr and then delete[] ptr afterwards. I think the
      > delete[] ptr statement will not be executed after the return statement.[/color]
      Any[color=blue]
      > workaround for this?
      >
      >
      > REgards
      > ML
      >[/color]

      I'm not sure what you want to do, but I think you misunderstand what pointer
      assignment does. First you are allocating memory and setting ptr to point to
      it

      char* ptr = new char[6];

      and then you are setting ptr to point to something else

      ptr = "Hello";

      after which the originally allocated memory is no longer pointed to by
      anything, so you can't delete the memory and it is leaked.

      Aside from this, if you want to automatically delete memory upon returning
      from a function, use either std::vector (for arrays) or std::auto_ptr (for
      single object).

      --
      Gregg


      Comment

      • Josephine Schafer

        #4
        Re: return statement and delete


        "Josephine Schafer" <no_spam_jossch afer@hotmail.co m> wrote in message
        news:bklsb8$35f j1$1@ID-192448.news.uni-berlin.de...[color=blue]
        >
        > "A" <A@iprimus.com. au> wrote in message news:3f6e7175_1 @news.iprimus.c om.au...[color=green]
        > > Hi,
        > >
        > > consider this:
        > >
        > >
        > > char* aClass::printHe llo()
        > > {
        > > char* ptr = new char[6];
        > > ptr = "Hello";
        > > return ptr;
        > > delete[] ptr;
        > > }[/color]
        >[/color]
        Oops..You are doing a memory leak for sure.
        ptr first points to a valid dynamic store address (1st line) and then you change
        it to
        point to a string literal in your second stmt (2nd line).
        Now calling delete on a pointer that doesn't point to a valid dynamic store
        address is not correct.

        --
        J.Schafer






        Comment

        • David White

          #5
          Re: return statement and delete

          A <A@iprimus.com. au> wrote in message news:3f6e7175_1 @news.iprimus.c om.au...[color=blue]
          > Hi,
          >
          > consider this:
          >
          >
          > char* aClass::printHe llo()[/color]

          The function doesn't print anything.
          [color=blue]
          > {
          > char* ptr = new char[6];
          > ptr = "Hello";
          > return ptr;
          > delete[] ptr;
          > }
          >
          > Is it possible to return ptr and then delete[] ptr afterwards.[/color]

          'return' means the function returns, so any delete after that cannot be done
          by the function.
          [color=blue]
          > I think the
          > delete[] ptr statement will not be executed after the return statement.[/color]
          Any[color=blue]
          > workaround for this?[/color]

          You have to decide whether you want the delete to be before or after the
          function returns. If before, you should not return a pointer to deleted
          memory. If after, the delete cannot be done by the function.

          What exactly are you trying to do? Maybe this would do what you want:

          std::string aClass::getHell o() const
          {
          return "Hello";
          }

          DW



          Comment

          • David White

            #6
            Re: return statement and delete

            David White <no@email.provi ded> wrote in message
            news:3Vubb.4257 $d6.159238@nasa l.pacific.net.a u...[color=blue]
            > A <A@iprimus.com. au> wrote in message[/color]
            news:3f6e7175_1 @news.iprimus.c om.au...[color=blue]
            >
            > The function doesn't print anything.
            >[color=green]
            > > {
            > > char* ptr = new char[6];
            > > ptr = "Hello";[/color][/color]

            At least someone (Gregg, at my newsreader so far) noticed the biggest
            problem. It looks as though it was _intended_ that "Hello" be copied to the
            allocated buffer, even though I missed that this isn't what the code
            actually does.

            DW



            Comment

            • jeffc

              #7
              Re: return statement and delete


              "A" <A@iprimus.com. au> wrote in message
              news:3f6e7175_1 @news.iprimus.c om.au...[color=blue]
              > char* aClass::printHe llo()
              > {
              > char* ptr = new char[6];
              > ptr = "Hello";
              > return ptr;
              > delete[] ptr;
              > }
              >
              > Is it possible to return ptr and then delete[] ptr afterwards. I think the
              > delete[] ptr statement will not be executed after the return statement.[/color]

              Of course not.
              [color=blue]
              > Any workaround for this?[/color]

              Well, the "workaround " is obviously to delete the ptr before the function
              call. But obviously, that will delete the ptr before you can use what it's
              pointing to. If you are going to write code like this, then you have to
              think about the responsibility for the memory. As written, the responsible
              party has to be the caller, not the function itself. i.e. the caller would
              be responsible for deleting the storage.


              Comment

              • jeffc

                #8
                Re: return statement and delete


                "jeffc" <nobody@nowhere .com> wrote in message
                news:3f6f045a_1 @news1.prserv.n et...[color=blue]
                >
                > Well, the "workaround " is obviously to delete the ptr before the function
                > call. But obviously, that will delete the ptr before you can use what[/color]
                it's[color=blue]
                > pointing to. If you are going to write code like this, then you have to
                > think about the responsibility for the memory. As written, the[/color]
                responsible[color=blue]
                > party has to be the caller, not the function itself. i.e. the caller[/color]
                would[color=blue]
                > be responsible for deleting the storage.[/color]

                Taking into account the error that Josephine pointed out, too :-)


                Comment

                Working...