473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing block of memory safely between classes

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?
Jun 27 '08 #1
4 1780
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
That's a very good reason, yes.
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?
A way around what? You can make your vector allocate stuff in your
shared memory, you just need to provide your vector with your own
allocator. I be you can find plenty of information in the book
"The C++ Standard Library" by Josuttis.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On Apr 18, 10:19 am, tech <naumansulai...@googlemail.comwrote:
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

Jun 27 '08 #3
On Apr 18, 10:19*am, tech <naumansulai...@googlemail.comwrote:
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
Jun 27 '08 #4
On Apr 18, 9:19 am, tech <naumansulai...@googlemail.comwrote:
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.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
2
by: Jim Cutler | last post by:
I have a C and a CPP program. The C program calls the CPP program. The CPP program needs to pass a new block of memory back to the C program. If I use "new" in the CPP program what do I use in the...
11
by: Dmitry | last post by:
Hi there, Just came across this problem and was wondering if someone can shed a light on it as it somewhat puzzles me. Suppose I have the following classes: Public Class CTest Private...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.