How to pass memory range to functions?

Discussion in 'C++' started by Olaf van der Spek, May 8, 2006.

  1. 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?
     
    Olaf van der Spek, May 8, 2006
    #1
    1. Advertising

  2. Olaf van der Spek

    Puppet_Sock Guest

    Olaf van der Spek wrote:
    > 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
     
    Puppet_Sock, May 8, 2006
    #2
    1. Advertising

  3. On 8 May 2006 07:58:12 -0700, "Puppet_Sock" <>
    wrote:

    >Olaf van der Spek wrote:
    >> 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.


    I know, but that doesn't work if you're doing low-level stuff and the
    functions you're calling except pointers.
     
    Olaf van der Spek, May 8, 2006
    #3
  4. "Olaf van der Spek" <> wrote in message
    news:...
    : 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
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, May 8, 2006
    #4
  5. On Mon, 8 May 2006 22:23:02 +0200, "Ivan Vecerina"
    <> wrote:

    >"Olaf van der Spek" <> wrote in message
    >news:...
    >: 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.
     
    Olaf van der Spek, May 9, 2006
    #5
  6. Olaf van der Spek

    Ian Collins Guest

    Olaf van der Spek wrote:
    > On Mon, 8 May 2006 22:23:02 +0200, "Ivan Vecerina"
    > <> wrote:
    >
    >
    >>"Olaf van der Spek" <> wrote in message
    >>news:...
    >>: 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.


    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.

    --
    Ian Collins.
     
    Ian Collins, May 9, 2006
    #6
  7. On Tue, 09 May 2006 20:08:03 +1200, Ian Collins <>
    wrote:

    >Olaf van der Spek wrote:
    >>>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.

    >
    >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.
     
    Olaf van der Spek, May 9, 2006
    #7
    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. Replies:
    46
    Views:
    996
    Antoon Pardon
    Jul 25, 2006
  2. venkatagmail
    Replies:
    11
    Views:
    713
    James Kanze
    Oct 3, 2007
  3. Lambda
    Replies:
    2
    Views:
    424
    James Kanze
    Jul 16, 2008
  4. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    170
    Tomoyuki Kosimizu
    Nov 27, 2003
  5. David Bird
    Replies:
    1
    Views:
    241
    Tiago Macedo
    Jun 23, 2008
Loading...

Share This Page