auto_ptr to char[ size_t]?

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

    auto_ptr to char[ size_t]?

    I want to use an auto_ptr to a buffer:

    auto_ptr<char> buffer = new char[ size];

    But this does not work, as auto_ptr calls
    delete instead of delete[], right?

    What I can do? Is there a version of auto_ptr calling delete[]?

    thanks,
    marc

  • tf

    #2
    Re: auto_ptr to char[ size_t]?

    Marc Schellens wrote:[color=blue]
    > I want to use an auto_ptr to a buffer:
    >
    > auto_ptr<char> buffer = new char[ size];
    >
    > But this does not work, as auto_ptr calls
    > delete instead of delete[], right?[/color]

    Right
    [color=blue]
    > What I can do? Is there a version of auto_ptr calling delete[]?
    >
    > thanks,
    > marc[/color]

    Use std::vector or std::string instead.
    If you insist on using a smart pointer look
    into the boost smart pointers www.boost.org


    Comment

    • John Harrison

      #3
      Re: auto_ptr to char[ size_t]?


      "Marc Schellens" <m_schellens@ho tmail.com> wrote in message
      news:3F1558F8.9 050706@hotmail. com...[color=blue]
      > I want to use an auto_ptr to a buffer:
      >
      > auto_ptr<char> buffer = new char[ size];
      >
      > But this does not work, as auto_ptr calls
      > delete instead of delete[], right?
      >
      > What I can do? Is there a version of auto_ptr calling delete[]?
      >
      > thanks,
      > marc
      >[/color]

      You could go to your auto_ptr header (its in <memory> if memory serves!),
      cut and paste the code into your editor (don't forget the copyright
      message). Rename the class, and replace delete with delete[].

      john


      Comment

      • Marc Schellens

        #4
        Re: auto_ptr to char[ size_t]?

        >>I want to use an auto_ptr to a buffer:[color=blue][color=green]
        >>
        >>auto_ptr<char > buffer = new char[ size];
        >>
        >>But this does not work, as auto_ptr calls
        >>delete instead of delete[], right?[/color]
        >
        >
        > Right
        >
        >[color=green]
        >>What I can do? Is there a version of auto_ptr calling delete[]?
        >>
        >>thanks,
        >>marc[/color]
        >
        >
        > Use std::vector or std::string instead.
        > If you insist on using a smart pointer look
        > into the boost smart pointers www.boost.org[/color]

        I want to do ostream.write(c har*,count) for the string
        (unformatted), therefore vector or string are no options here.

        Comment

        • Marc Schellens

          #5
          Re: auto_ptr to char[ size_t]?

          >>>>I want to use an auto_ptr to a buffer:[color=blue][color=green][color=darkred]
          >>>>
          >>>>auto_ptr<ch ar> buffer = new char[ size];
          >>>>
          >>>>But this does not work, as auto_ptr calls
          >>>>delete instead of delete[], right?
          >>>
          >>>
          >>>Right
          >>>
          >>>
          >>>
          >>>>What I can do? Is there a version of auto_ptr calling delete[]?
          >>>>
          >>>>thanks,
          >>>>marc
          >>>
          >>>
          >>>Use std::vector or std::string instead.
          >>>If you insist on using a smart pointer look
          >>>into the boost smart pointers www.boost.org[/color]
          >>
          >>I want to do ostream.write(c har*,count) for the string
          >>(unformatted) , therefore vector or string are no options here.[/color]
          >
          >
          > Why is a std::vector<cha r> no option?
          > You can provide its constructor the initial size,
          > and use the pointer to its first element as the parameter
          > in your function call.
          >[/color]
          Ok, I will do that.
          Thanks,
          marc

          Comment

          • DarkSpy

            #6
            Re: auto_ptr to char[ size_t]?

            Marc Schellens <m_schellens@ho tmail.com> wrote in message news:<3F1558F8. 9050706@hotmail .com>...[color=blue]
            > I want to use an auto_ptr to a buffer:
            >
            > auto_ptr<char> buffer = new char[ size];
            > But this does not work, as auto_ptr calls[/color]

            auto_ptr<char> buffer(new char[size]);
            the ctor is explicit to force to write it.
            [color=blue]
            > delete instead of delete[], right?
            >
            > What I can do? Is there a version of auto_ptr calling delete[]?
            >[/color]
            delete included in dtor of auto_ptr.
            [color=blue]
            > thanks,
            > marc[/color]

            Comment

            • John Harrison

              #7
              Re: auto_ptr to char[ size_t]?


              "DarkSpy" <coneos@21cn.co m> wrote in message
              news:aacb0456.0 307170049.56856 bcc@posting.goo gle.com...[color=blue]
              > Marc Schellens <m_schellens@ho tmail.com> wrote in message[/color]
              news:<3F1558F8. 9050706@hotmail .com>...[color=blue][color=green]
              > > I want to use an auto_ptr to a buffer:
              > >
              > > auto_ptr<char> buffer = new char[ size];
              > > But this does not work, as auto_ptr calls[/color]
              >
              > auto_ptr<char> buffer(new char[size]);
              > the ctor is explicit to force to write it.
              >[color=green]
              > > delete instead of delete[], right?
              > >
              > > What I can do? Is there a version of auto_ptr calling delete[]?
              > >[/color]
              > delete included in dtor of auto_ptr.
              >[/color]

              Yes but the point is that using delete on a pointer that you created with
              new[] invokes undefined behaviour. auto_ptr cannot be used with arrays.

              john


              Comment

              • Russell Hanneken

                #8
                Re: auto_ptr to char[ size_t]?

                "tf" <abc@abc.com> wrote in message
                news:bf5k51$b7h jj$1@ID-57289.news.uni-berlin.de...[color=blue]
                > Marc Schellens wrote:[color=green]
                > > I want to do ostream.write(c har*,count) for the string
                > > (unformatted), therefore vector or string are no options here.[/color]
                >
                > Why is a std::vector<cha r> no option?
                > You can provide its constructor the initial size,
                > and use the pointer to its first element as the parameter
                > in your function call.[/color]

                If I recall correctly, there's no guarantee that std::vector stores its
                elements in an array. So in theory, that solution might not work. I admit
                that, realistically, every implementation of std::vector probably does use
                an array.

                In any case, wouldn't the more natural solution be to store the characters
                in a std::string, and then invoke std::string's data() member function to
                get a character array when it's needed?

                Regards,

                Russell Hanneken
                rhanneken@pobox .com


                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: auto_ptr to char[ size_t]?



                  Russell Hanneken wrote:[color=blue]
                  >
                  > "tf" <abc@abc.com> wrote in message
                  > news:bf5k51$b7h jj$1@ID-57289.news.uni-berlin.de...[color=green]
                  > > Marc Schellens wrote:[color=darkred]
                  > > > I want to do ostream.write(c har*,count) for the string
                  > > > (unformatted), therefore vector or string are no options here.[/color]
                  > >
                  > > Why is a std::vector<cha r> no option?
                  > > You can provide its constructor the initial size,
                  > > and use the pointer to its first element as the parameter
                  > > in your function call.[/color]
                  >
                  > If I recall correctly, there's no guarantee that std::vector stores its
                  > elements in an array. So in theory, that solution might not work. I admit
                  > that, realistically, every implementation of std::vector probably does use
                  > an array.[/color]

                  The concensus is this:

                  * There is no guarantee
                  * This has probably been an oversight while comming up with the standard
                  * The next version of the standard will guarantee this
                  * It is hard or impossible to fullfill the requirements of std::vector
                  if the data is not stored contigous
                  * There is no known version which does not store the data contigous.
                  [color=blue]
                  >
                  > In any case, wouldn't the more natural solution be to store the characters
                  > in a std::string, and then invoke std::string's data() member function to
                  > get a character array when it's needed?[/color]

                  Could be. But a character pointer is often used to denote simply
                  a sequence of bytes. Not necessarly text.

                  --
                  Karl Heinz Buchegger
                  kbuchegg@gascad .at

                  Comment

                  Working...