 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ulrich Eckhardt Guest
|
Posted: Tue Nov 01, 2005 3:49 pm Post subject: Raw buffers with shared_ptr |
|
|
Hi!
I sometimes wondered why there wasn't some kind of support for a
shared_ptr-like buffer, representing e.g. packets from the net or somesuch.
The problem with using shared_ptr was mainly that it invoked 'delete' on
the owned pointer, so is not suitable for arrays.
I just got the idea of simply using a custom deleter, making this a pretty
painless thing:
shared_ptr<void> ptr( malloc(length), &free);
The only thing missing now would be the size of the memory, but that's not
even always necessary and if, it would be easy to wrap that in a struct
holding a shared_ptr<void const> and a size_t.
How do you typically handle such packets?
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Wed Nov 02, 2005 11:33 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Ulrich Eckhardt wrote:
| Quote: | Hi!
I sometimes wondered why there wasn't some kind of support for a
shared_ptr-like buffer, representing e.g. packets from the net or somesuch.
The problem with using shared_ptr was mainly that it invoked 'delete' on
the owned pointer, so is not suitable for arrays.
|
http://www.boost.org/libs/smart_ptr/shared_array.htm
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Wed Nov 02, 2005 11:34 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Ulrich Eckhardt wrote:
| Quote: | I sometimes wondered why there wasn't some kind of support for a
shared_ptr-like buffer, representing e.g. packets from the net or somesuch.
The problem with using shared_ptr was mainly that it invoked 'delete' on
the owned pointer, so is not suitable for arrays.
|
For arrays, there's boost::shared_array...
| Quote: | I just got the idea of simply using a custom deleter, making this a pretty
painless thing:
shared_ptr<void> ptr( malloc(length), &free);
The only thing missing now would be the size of the memory, but that's not
even always necessary and if, it would be easy to wrap that in a struct
holding a shared_ptr<void const> and a size_t.
|
What do you mean by "The only thing missing now would be the size of the
memory"? I don't understand what are you trying to achieve.
| Quote: | How do you typically handle such packets?
|
Never had the chance, but if I have to answer, of course I would say "it
depends" You did not tell us enough info on your use case to give a
more precise answer. Please state your requirements more clearly.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Vorbrodt Guest
|
Posted: Wed Nov 02, 2005 11:35 am Post subject: Re: Raw buffers with shared_ptr |
|
|
"Ulrich Eckhardt" <eckhardt (AT) satorlaser (DOT) com> wrote
| Quote: | Hi!
I sometimes wondered why there wasn't some kind of support for a
shared_ptr-like buffer, representing e.g. packets from the net or
somesuch.
The problem with using shared_ptr was mainly that it invoked 'delete' on
the owned pointer, so is not suitable for arrays.
I just got the idea of simply using a custom deleter, making this a pretty
painless thing:
shared_ptr<void> ptr( malloc(length), &free);
The only thing missing now would be the size of the memory, but that's not
even always necessary and if, it would be easy to wrap that in a struct
holding a shared_ptr<void const> and a size_t.
How do you typically handle such packets?
Uli
|
shared_ptr and such are bad for arrays... delete on an array (instead of
delete []) leads to undefined behaviour). instead just do this:
since you already have a great array conteiner, mainly vector<T>, this will
work:
shared_ptr<vector shared_array(new
vector<YOUR_TYPE_GOES_HERE>);
now shared_array is a shard pointer to an array :)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Wed Nov 02, 2005 11:40 am Post subject: Re: Raw buffers with shared_ptr |
|
|
On 1 Nov 2005 10:49:17 -0500, Ulrich Eckhardt
<eckhardt (AT) satorlaser (DOT) com> wrote:
| Quote: | How do you typically handle such packets?
|
vector<char>. The standard requires that memory be contiguous, so just
pass the address of the first element to the function expecting a
char*.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed Nov 02, 2005 11:41 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Ulrich Eckhardt wrote:
| Quote: | I sometimes wondered why there wasn't some kind of support for
a shared_ptr-like buffer, representing e.g. packets from the
net or somesuch. The problem with using shared_ptr was mainly
that it invoked 'delete' on the owned pointer, so is not
suitable for arrays.
|
I believe that there is a boost::shared_array, which uses
delete[] on the pointer, rather than just delete.
| Quote: | I just got the idea of simply using a custom deleter, making
this a pretty painless thing:
shared_ptr<void> ptr( malloc(length), &free);
The only thing missing now would be the size of the memory, but
that's not
even always necessary and if, it would be easy to wrap that in
a struct holding a shared_ptr<void const> and a size_t.
How do you typically handle such packets?
|
std::vector< unsigned char >.
If for some reason, reference counting were relevant, then
shared_ptr< std::vector< unsigned char > > could be used.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ralph Zhang Guest
|
Posted: Thu Nov 03, 2005 10:53 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Well, typically, I guess I use
shared_array<char>
isn't it enough for such usage?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Thu Nov 03, 2005 10:56 am Post subject: Re: Raw buffers with shared_ptr |
|
|
On 2 Nov 2005 06:41:04 -0500, James Kanze <kanze (AT) none (DOT) news.free.fr>
wrote:
| Quote: | How do you typically handle such packets?
std::vector< unsigned char >.
|
unsigned char is conceptually better for binary data, but I find
myself doing an awful lot of casting from unsigned char* to plain
char* when I use such a buffer with functions such as ostream::read or
write. And if the buffer actually contains text data, I cannot create
a std::string with an unsigned char*, so I have to cast to char* then,
too.
Except for the possibility of unintentional misuse (e.g. comparing
char values with unsigned char values when char might be interpreted
as signed), is there any other reason why vector<unsigned char> should
be favored over vector<char>?
Thanks.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Thu Nov 03, 2005 12:59 pm Post subject: Re: Raw buffers with shared_ptr |
|
|
In article <EQO9f.19701$3A4.13008 (AT) news-wrt-01 (DOT) rdc-nyc.rr.com>, Martin
Vorbrodt <mvorbrodt (AT) gmail (DOT) com> wrote:
| Quote: |
shared_ptr and such are bad for arrays... delete on an array (instead of
delete []) leads to undefined behaviour). instead just do this:
|
if shared_ptr is constructed without a custom deleter you are correct.
But shared_ptr can be constructed with a custom deleter that uses
delete[]. or the shared_ptr can be constructed with a malloc call
and a deleter that calls free rather than delete, which is fine for
an large array of char if the data is a buffer, for some C style API.
void free_deleter(char *p) {std::free(p);}
shared_ptr<char> p(std::malloc(n),free_deleter);
and only a small new /delete is performed to hold the counter object.
malloc/free, provide the actual buffer, without intializing the contents
of the buffer.
Is this better than just std::vector<char>? depends on which is
significantly faster, Both are safe approaches both are in std or tr1
and shared_array is not. Making the reliance on boost a temporary
until the compiler supports tr1::shared_ptr. [shared_array is not in
tr1]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kodt Guest
|
Posted: Thu Nov 03, 2005 7:12 pm Post subject: Re: Raw buffers with shared_ptr |
|
|
Yes, shared_ptr seems to be not the best choice for that purpose, but
| Quote: | shared_ptr<void> ptr( malloc(length), &free);
shared_ptr and such are bad for arrays... delete on an array (instead of
delete []) leads to undefined behaviour). instead just do this:
|
please note that there is an explicit deallocator in the shared_ptr
constructor: &free that is a pair for malloc (neither delete nor
delete[] fit to malloc and would lead to UB).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Thu Nov 03, 2005 7:13 pm Post subject: Re: Raw buffers with shared_ptr |
|
|
On 3 Nov 2005 05:56:46 -0500, Bob Hairgrove <invalid (AT) bigfoot (DOT) com>
wrote:
| Quote: | functions such as ostream::read or
write.
|
Should be istream::read or ostream::write, of course.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Nov 03, 2005 7:15 pm Post subject: Re: Raw buffers with shared_ptr |
|
|
Bob Hairgrove wrote:
| Quote: | How do you typically handle such packets?
std::vector< unsigned char >.
is there any [...] reason why vector<unsigned char> should
be favored over vector<char>?
|
I don't use unsigned char for this, I just use "char" - it's shorter.
The reason is that for handling binary data it does not really matter
what interpretation you apply to its elements, because without knowing
the data format every arbitrary interpretation will be wrong anyway.
Let's say that you receive from the network a bunch of data that from
the user's point of view is an MPEG movie. If, at some level in the
program, you don't care about the fact that it's an MPEG movie, then why
care whether it should be composed of chars or unsigned chars? The only
thing that matters is the size of the buffer.
Unsigned does not bring anything here - except, possibly, if you want to
do a hex dump of the buffer or alike, but then you *do* apply some
interpretation to the data. Similarly, one could argue that signed short
(let's say 16 bit on some platform) would be favourable if the buffer is
going to keep audio PCM data. For others, unsigned int (32-bit) might be
preferable for work with bitmaps, but again this is applying a
particular interpretation to the data. In a general case you cannot do
this. That's why I use plain char (or vector<char>, etc.) for general
buffer handling.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Nov 04, 2005 11:09 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Bob Hairgrove wrote:
| Quote: | On 2 Nov 2005 06:41:04 -0500, James Kanze
[email]kanze (AT) none (DOT) news.free.fr[/email]> wrote:
How do you typically handle such packets?
std::vector< unsigned char >.
unsigned char is conceptually better for binary data, but I
find myself doing an awful lot of casting from unsigned char*
to plain char* when I use such a buffer with functions such as
ostream::read or write. And if the buffer actually contains
text data, I cannot create a std::string with an unsigned
char*, so I have to cast to char* then, too.
Except for the possibility of unintentional misuse (e.g.
comparing char values with unsigned char values when char
might be interpreted as signed), is there any other reason why
vector<unsigned char> should be favored over vector<char>?
|
The message to the reader. In my mind, char says character
data, unsigned char says raw memory. (And the problem of
casting for read and write didn't occur to me, because it
generally doesn't occur to me to do binary IO with
iostreams:-).)
In C, unsigned char is the only type which is guaranteed by the
standard to work as raw memory. C++ gives some additional
guarantees for char, but you still have the case that on a one's
complement machine, all bits 1 and all bits 0 will compare equal
if char is signed.
If you're not concerned about porting to really exotic machines,
then of course, char or unsigned char won't make a difference.
Except for the message they give to the reader.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri Nov 04, 2005 11:10 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Maciej Sobczak wrote:
| Quote: | The only
thing that matters is the size of the buffer.
|
Of course, the additional issue is to guarantee that the buffer will
accept the incoming data in the sense that it will not trigger any traps
nor other UB.
char and unsigned char give this guarantee for the general case, because
they allow any combination of bits in their representation.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Fri Nov 04, 2005 11:11 am Post subject: Re: Raw buffers with shared_ptr |
|
|
Maciej Sobczak wrote:
| Quote: | Bob Hairgrove wrote:
How do you typically handle such packets?
std::vector< unsigned char >.
is there any [...] reason why vector<unsigned char> should
be favored over vector<char>?
I don't use unsigned char for this, I just use "char" - it's shorter.
[snip]
Unsigned does not bring anything here
|
Are you sure? I thought that plain char could have trap
representations, whereas unsigned cannot. For example, if "char" is
signed on a sign-and-magnitude machine, then -0 could be a trap
representation. See 3.9.1/1 and /3 in the standard.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|