Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector as a char buffer ?

Reply
Thread Tools

vector as a char buffer ?

 
 
Asger Joergensen
Guest
Posts: n/a
 
      12-27-2011
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-27-2011
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
 
Reply With Quote
 
 
 
 
Asger Joergensen
Guest
Posts: n/a
 
      12-27-2011
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
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-27-2011
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 .
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-28-2011
On 12/27/2011 11:27 PM, XeCycle wrote:
> "Asger Joergensen"<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
char* gets(char* buffer) Sathyaish C Programming 9 10-05-2004 10:38 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments