How to pass memory range to functions?

  • Thread starter Olaf van der Spek
  • Start date
O

Olaf van der Spek

Hi,

I've got a lot of functions (compression, encryption, hashing,
encoding, etc) that work on a memory range.
I wrote my own class that has constructors for
void*, void*
void*, size_t
std::string&
and that automatically reinterpret_cast to unsigned char*.

But I'm wondering, is there a standard C++ or Boost class that already
does this?
Or is there a better way to do this?
 
P

Puppet_Sock

Olaf said:
I've got a lot of functions (compression, encryption, hashing,
encoding, etc) that work on a memory range.
I wrote my own class that has constructors for
void*, void*
void*, size_t
std::string&
and that automatically reinterpret_cast to unsigned char*.

But I'm wondering, is there a standard C++ or Boost class that already
does this?
Or is there a better way to do this?

The standard C++ way is to use containers like list, vector,
and such, and not to do the reinterpret_cast thing.
Socks
 
O

Olaf van der Spek

The standard C++ way is to use containers like list, vector,
and such, and not to do the reinterpret_cast thing.

I know, but that doesn't work if you're doing low-level stuff and the
functions you're calling except pointers.
 
I

Ivan Vecerina

: I've got a lot of functions (compression, encryption, hashing,
: encoding, etc) that work on a memory range.
: I wrote my own class that has constructors for
: void*, void*
: void*, size_t
: std::string&
: and that automatically reinterpret_cast to unsigned char*.
:
: But I'm wondering, is there a standard C++ or Boost class that already
: does this?

The usual approach is to pass a pair of poiters or iterators
that define the bounds of data to be processed (begin-end).

: Or is there a better way to do this?

The standard/generic approach is to write template functions
that can process different kinds of data ranges. It might
also be desirable to separate the input and output buffers.

For example, consider std::transform:
void transform( srcBegin, srcEnd, dstBegin, operation );

Of course, many low-level utilities prefer to process
memory ranges, or memory chunks.


Another standard approach would be to use streams. But then
again, standard classes are geared towards text-based i/o.
The binary-level streambuf does not provide separate input-only
and output-only classes. Plus locale and facet handling impair
efficiency.


So, IMO, when one wants to do low-level binary processing, one
should not count too much on standard library support.


Ivan
 
O

Olaf van der Spek

: I've got a lot of functions (compression, encryption, hashing,
: encoding, etc) that work on a memory range.
: I wrote my own class that has constructors for
: void*, void*
: void*, size_t
: std::string&
: and that automatically reinterpret_cast to unsigned char*.
:
: But I'm wondering, is there a standard C++ or Boost class that already
: does this?

The usual approach is to pass a pair of poiters or iterators
that define the bounds of data to be processed (begin-end).

But using two arguments is a disadvantage, see also Boost.Range.
For example, you can't use automatic conversion.
So, IMO, when one wants to do low-level binary processing, one
should not count too much on standard library support.

That's a shame, as I think it's a frequent task.
 
I

Ian Collins

Olaf said:
But using two arguments is a disadvantage, see also Boost.Range.
For example, you can't use automatic conversion.




That's a shame, as I think it's a frequent task.

What would you expect a library to do in this case? You have a block of
data to process, which requires two parameters to define it, unless it
has an end marker.
 
O

Olaf van der Spek

What would you expect a library to do in this case? You have a block of
data to process, which requires two parameters to define it, unless it
has an end marker.

I expected it to have a class like pair<unsigned char*, unsigned
char*> with constructors that take (void*, void*), (void*, size_t),
(const string&), etc.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top