Passing block of memory safely between classes

Discussion in 'C++' started by tech, Apr 18, 2008.

  1. tech

    tech Guest

    Hi, I need to pass a block of say 320 bytes memory between some
    classes
    which do various processing on it. The app needs to be quick so i
    can't
    keep copying.

    The simplest way is via pointer say:

    class A
    {

    void Transmitdata(const short* data, size_t Insize, char* output,
    size_t OutSize)
    {
    // do some processing on data

    }

    };

    alternative which seems to be a lot safer is
    void Transmitdata(const std::vector<short>& buf, std::vector<char>&
    outBuf)
    {

    }
    The vectors know there size so each class doesn't need to worry about
    the size
    of data the pointers are pointing to


    Its just that the block of memory i want to pass around is in some
    shared memory
    area and i can't overlay a vector onto a block of memory as it makes a
    copy so at present
    i'm just stuck with the pointers. Anyone know a way round this?
    tech, Apr 18, 2008
    #1
    1. Advertising

  2. tech

    SeanW Guest

    On Apr 18, 10:19 am, tech <> wrote:
    > Hi, I need to pass a block of say 320 bytes memory between some
    > classes
    > which do various processing on it. The app needs to be quick so i
    > can't
    > keep copying.
    >
    > The simplest way is via pointer say:
    >
    > class A
    > {
    >
    > void Transmitdata(const short* data, size_t Insize, char* output,
    > size_t OutSize)
    > {
    > // do some processing on data
    >
    > }
    > };
    >
    > alternative which seems to be a lot safer is
    > void Transmitdata(const std::vector<short>& buf, std::vector<char>&
    > outBuf)
    > {
    >
    > }
    >
    > The vectors know there size so each class doesn't need to worry about
    > the size
    > of data the pointers are pointing to
    >
    > Its just that the block of memory i want to pass around is in some
    > shared memory
    > area and i can't overlay a vector onto a block of memory as it makes a
    > copy so at present
    > i'm just stuck with the pointers. Anyone know a way round this?


    You might be interested in this project, "A C++ Pooled,
    Shared Memory Allocator For The Standard Template Library":

    http://allocator.sourceforge.net/

    As always with C++, it's not as easy as you'd like (for
    one compiler, at least):

    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21251

    You might also find this page on writing your own
    allocator interesting:

    http://www.codeproject.com/KB/cpp/allocator.aspx

    Sean
    SeanW, Apr 18, 2008
    #2
    1. Advertising

  3. tech

    Puppet_Sock Guest

    On Apr 18, 10:19 am, tech <> wrote:
    > Hi, I need to pass a block of say 320 bytes memory between some
    > classes
    > which do various processing on it. The app needs to be quick so i
    > can't
    > keep copying.
    >
    > The simplest way is via pointer say:
    >
    > class A
    > {
    >
    > void Transmitdata(const short*  data, size_t Insize, char* output,
    > size_t OutSize)
    > {
    >    // do some processing on data
    >
    > }
    > };
    >
    > alternative which seems to be a lot safer is
    > void Transmitdata(const std::vector<short>& buf, std::vector<char>&
    > outBuf)
    > {
    >
    > }
    >
    > The vectors know there size so each class doesn't need to worry about
    > the size
    > of data the pointers are pointing to
    >
    > Its just that the block of memory i want to pass around is in some
    > shared memory
    > area and i can't overlay a vector onto a block of memory as it makes a
    > copy so at present
    > i'm just stuck with the pointers. Anyone know a way round this?


    That's easy. You want to pass a reference to an instance
    of a class that can safely encapsulate a shared memory block.

    But you don't have a class that can safely encapsulate
    your shared memory block.

    So you write a class that can safely encapsulate your
    shared memory block.

    Lots of ways to do that, depending on the specifics of the
    memory block. If it is a bunch of integers, as your example
    suggests, it's way easy. For example, you can just have
    a pointer in the class and initialize an instance to point
    to the memory block. And have it carry around the size
    info for the block as well. After that it depends on how
    much you want to pack into the class, and how much you
    want to have as routines outside the class. Chances are
    good you will want to look at a singleton pattern as well.
    Then pretty much all your tasks on the memory block are
    going to be member functions of the class.
    Socks
    Puppet_Sock, Apr 18, 2008
    #3
  4. tech

    Guest

    On Apr 18, 9:19 am, tech <> wrote:
    > Hi, I need to pass a block of say 320 bytes memory between some
    > classes
    > which do various processing on it. The app needs to be quick so i
    > can't
    > keep copying.
    >
    > The simplest way is via pointer say:
    >
    > class A
    > {
    >
    > void Transmitdata(const short* data, size_t Insize, char* output,
    > size_t OutSize)
    > {
    > // do some processing on data
    >
    > }
    > };
    >
    > alternative which seems to be a lot safer is
    > void Transmitdata(const std::vector<short>& buf, std::vector<char>&
    > outBuf)
    > {
    >
    > }
    >
    > The vectors know there size so each class doesn't need to worry about
    > the size
    > of data the pointers are pointing to
    >
    > Its just that the block of memory i want to pass around is in some
    > shared memory
    > area and i can't overlay a vector onto a block of memory as it makes a
    > copy so at present
    > i'm just stuck with the pointers. Anyone know a way round this?


    you may want to look at boost interprocess(www.boost.org).

    Thanks,
    Balaji.
    , Apr 19, 2008
    #4
    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. Razvan
    Replies:
    5
    Views:
    11,301
    Dale King
    Jul 27, 2004
  2. Replies:
    7
    Views:
    556
    Alex Hunsley
    Aug 12, 2006
  3. morrell
    Replies:
    1
    Views:
    949
    roy axenov
    Oct 10, 2006
  4. Just Another Victim of the Ambient Morality

    What's the difference between these two block passing strategies?

    Just Another Victim of the Ambient Morality, Jul 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    86
    Erik Veenstra
    Jul 20, 2006
  5. Brian Candler

    Passing a block to a block

    Brian Candler, Nov 4, 2008, in forum: Ruby
    Replies:
    2
    Views:
    106
    Brian Candler
    Nov 4, 2008
Loading...

Share This Page