vector as a char buffer ?

A

Asger Joergensen

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
 
V

Victor Bazarov

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
 
A

Asger Joergensen

Hi Victor

Victor said:
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
 
J

Jorgen Grahn

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
 
V

Victor Bazarov

Asger Joergensen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top