vector as a char buffer ?

Discussion in 'C++' started by Asger Joergensen, Dec 27, 2011.

  1. Hi

    I'm not that used to the stl so I ask to be on the safe side:

    typedef std::vector<char> TMembuf;

    TMembuf buf(1000);
    char* p = &buf[0];

    I have tested it and it seem to work fine just like:

    char* p = new char[1000];

    and without the delete[];

    But is there any downside to doing like this ?

    Thanks in advance
    Best regards
    Asger-P
     
    Asger Joergensen, Dec 27, 2011
    #1
    1. Advertising

  2. On 12/27/2011 8:26 AM, Asger Joergensen wrote:
    > I'm not that used to the stl so I ask to be on the safe side:
    >
    > typedef std::vector<char> TMembuf;
    >
    > TMembuf buf(1000);
    > char* p =&buf[0];
    >
    > I have tested it and it seem to work fine just like:
    >
    > char* p = new char[1000];
    >
    > and without the delete[];
    >
    > But is there any downside to doing like this ?


    Not really. Just remember that 'p' is not owned by you and that its
    validity is tightly linked to the lifetime of 'buf'.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 27, 2011
    #2
    1. Advertising

  3. Hi Victor

    Victor Bazarov wrote:

    > On 12/27/2011 8:26 AM, Asger Joergensen wrote:
    > > I'm not that used to the stl so I ask to be on the safe side:
    > >
    > >typedef std::vector<char> TMembuf;
    > >
    > > TMembuf buf(1000);
    > > char* p =&buf[0];
    > >
    > > I have tested it and it seem to work fine just like:
    > >
    > > char* p = new char[1000];
    > >
    > > and without the delete[];
    > >
    > > But is there any downside to doing like this ?

    >
    > Not really. Just remember that 'p' is not owned by you and that its validity is
    > tightly linked to the lifetime of 'buf'.


    Thanks, and yes I know that I can only use p withing the scoope of buf.

    Best regards
    Asger-P
     
    Asger Joergensen, Dec 27, 2011
    #3
  4. Asger Joergensen

    Jorgen Grahn Guest

    On Tue, 2011-12-27, Victor Bazarov wrote:
    > On 12/27/2011 8:26 AM, Asger Joergensen wrote:
    >> I'm not that used to the stl so I ask to be on the safe side:
    >>
    >> typedef std::vector<char> TMembuf;
    >>
    >> TMembuf buf(1000);
    >> char* p =&buf[0];
    >>
    >> I have tested it and it seem to work fine just like:
    >>
    >> char* p = new char[1000];
    >>
    >> and without the delete[];
    >>
    >> But is there any downside to doing like this ?

    >
    > Not really. Just remember that 'p' is not owned by you and that its
    > validity is tightly linked to the lifetime of 'buf'.


    And to 'buf' not being resized, swapped etc. But it's fairly easy to
    manage such things -- easier than to deal with new[] IMHO.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Dec 27, 2011
    #4
  5. On 12/27/2011 11:27 PM, XeCycle wrote:
    > "Asger Joergensen"<> writes:
    >
    >> Hi

    >
    > Hello,
    >
    >> I'm not that used to the stl so I ask to be on the safe side:
    >>
    >> typedef std::vector<char> TMembuf;
    >>
    >> TMembuf buf(1000);
    >> char* p =&buf[0];
    >>
    >> I have tested it and it seem to work fine just like:
    >>
    >> char* p = new char[1000];
    >>
    >> and without the delete[];
    >>
    >> But is there any downside to doing like this ?

    >
    > This is not recommended, of course. You didn't even use any of
    > the vector facilities, in this case. Vectors provide the method
    > reserve().


    WHAT is not recommended? And why the hell not?

    And what use would Asger have for 'reserve()'? std::vector has a proper
    constructor that allocates the buffer and makes it ready to use.

    > However if you want a buffer, there's circular buffer in boost
    > library.


    Asger needed something analogous to an array allocated by 'new[]'. A
    standard vector is just about what the library can offer. Boost does
    not exist on every platform, while std::vector does. There is no sense
    in using a non-portable library when the Standard library suffices.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 28, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,829
  2. Sathyaish

    char* gets(char* buffer)

    Sathyaish, Oct 5, 2004, in forum: C Programming
    Replies:
    9
    Views:
    430
    CBFalconer
    Oct 5, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,064
    Ian Collins
    May 9, 2006
  4. Replies:
    8
    Views:
    1,932
    Csaba
    Feb 18, 2006
  5. Javier
    Replies:
    2
    Views:
    570
    James Kanze
    Sep 4, 2007
Loading...

Share This Page