473,480 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Practice

mc
Hi all,

I've a long experience with C but am just getting up to speed with C++, so
please bear with me if that's obvious.

I've got a class that parses a data file and creates a vector of AESKey, a
vector of Package, GPS data and more. This data is not sequential in the
file so the parsing in the file is cumbersome and out of my control
(provided by customer).

Now, I've got another class that is build based of AESKey objects.
Currently, the constructor is as follows:
CryptoEngine(int numKeys, AESKey * keys);

To make it more elegant -or so I think, I'm thinking of passing a vector of
AESKey to make it like this:
CryptoEngine(std::vector<AESKey>& keys);

so that the CryptoEngine "owns" this vector (it's anyways the only object
using it). This would be best as when the object is deleted, all the data
it needs is deleted as well.

The problem is that this vector of AESKey can be huge (100 of Mb), don't ask
me why, that's how our application is. :-( So when I'd be copying the
argument being passed to the CryptoEngine, the amount of RAM needed for the
argument is duplicated by the copy constructor and there's basically a
"memcpy". Is there a way to work this around in a C++ way? The obvious
would be to parse the file in the CryptoEngine but it's a no-can-do as this
file contains data (non sequential) for mutiple types of objects.

Thank you in advance.

Best regards,
Mike


Jun 27 '08 #1
2 1200
mc wrote:
Hi all,

I've a long experience with C but am just getting up to speed with
C++, so please bear with me if that's obvious.

I've got a class that parses a data file and creates a vector of
AESKey, a vector of Package, GPS data and more. This data is not
sequential in the file so the parsing in the file is cumbersome and
out of my control (provided by customer).

Now, I've got another class that is build based of AESKey objects.
Currently, the constructor is as follows:
CryptoEngine(int numKeys, AESKey * keys);

To make it more elegant -or so I think, I'm thinking of passing a
vector of AESKey to make it like this:
CryptoEngine(std::vector<AESKey>& keys);
Unless the constructor changes the values, you might consider passing
the vector by a reference to const:

CryptoEngine(std::vector<AESKeyconst& keys);

and make your CryptoEngine actually store the reference to const
vector (and initialise in the constructor's initialiser list).
so that the CryptoEngine "owns" this vector (it's anyways the only
object using it). This would be best as when the object is deleted,
all the data it needs is deleted as well.
Without seeing more of your code it is hard to understand what you
mean by that statement.
The problem is that this vector of AESKey can be huge (100 of Mb),
don't ask me why, that's how our application is. :-( So when I'd
be copying the argument being passed to the CryptoEngine, the amount
of RAM needed for the argument is duplicated by the copy constructor
and there's basically a "memcpy". Is there a way to work this
around in a C++ way? The obvious would be to parse the file in the
CryptoEngine but it's a no-can-do as this file contains data (non
sequential) for mutiple types of objects.
Well, you can create an empty vector in your CryptoEngine and swap
it immediately with the vector passed in by a reference:

class CryptoEngine {
std::vector<AESKeymKeys;
public:
CryptoEngine(std::vector<AESKey& keys) {
mKeys.swap(keys);
}
};

That way your object takes possession of all the _contents_ of the
vector and clears the argument out (the data are essentially "moved"
from the argument to the member data).

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
mc
Hi Victor,
Thanks a bunch. swap() is just what I needed.

Regards,

Mike

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fu**********@news.datemas.de...
mc wrote:
>Hi all,

I've a long experience with C but am just getting up to speed with
C++, so please bear with me if that's obvious.

I've got a class that parses a data file and creates a vector of
AESKey, a vector of Package, GPS data and more. This data is not
sequential in the file so the parsing in the file is cumbersome and
out of my control (provided by customer).

Now, I've got another class that is build based of AESKey objects.
Currently, the constructor is as follows:
CryptoEngine(int numKeys, AESKey * keys);

To make it more elegant -or so I think, I'm thinking of passing a
vector of AESKey to make it like this:
CryptoEngine(std::vector<AESKey>& keys);

Unless the constructor changes the values, you might consider passing
the vector by a reference to const:

CryptoEngine(std::vector<AESKeyconst& keys);

and make your CryptoEngine actually store the reference to const
vector (and initialise in the constructor's initialiser list).
>so that the CryptoEngine "owns" this vector (it's anyways the only
object using it). This would be best as when the object is deleted,
all the data it needs is deleted as well.

Without seeing more of your code it is hard to understand what you
mean by that statement.
>The problem is that this vector of AESKey can be huge (100 of Mb),
don't ask me why, that's how our application is. :-( So when I'd
be copying the argument being passed to the CryptoEngine, the amount
of RAM needed for the argument is duplicated by the copy constructor
and there's basically a "memcpy". Is there a way to work this
around in a C++ way? The obvious would be to parse the file in the
CryptoEngine but it's a no-can-do as this file contains data (non
sequential) for mutiple types of objects.

Well, you can create an empty vector in your CryptoEngine and swap
it immediately with the vector passed in by a reference:

class CryptoEngine {
std::vector<AESKeymKeys;
public:
CryptoEngine(std::vector<AESKey& keys) {
mKeys.swap(keys);
}
};

That way your object takes possession of all the _contents_ of the
vector and clears the argument out (the data are essentially "moved"
from the argument to the member data).

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 #3

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

Similar topics

1
2518
by: Xah Lee | last post by:
Dear functional programing comrades, Among the community of automatons of the IT industry, there is a popular quote about "theory vs practice" that goes something along the lines of "in theory...
4
314
by: Chris | last post by:
I have a question on whether or not this is good practice. I have a fairly complex web user control (a datalist embedded in a datalist with lots of controls) that I will call Usercontrol1 and a...
9
2608
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error...
22
1740
by: philipl | last post by:
hi, while reading .net framework sdk, it says that the following is bad practice, then it goes on to give an example with the very same instance of this 'bad practice'. Can someone comment on...
2
3636
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its...
17
7998
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
1
1761
by: trebor | last post by:
I'm learning dotNet, although I first learned programming back in the days when structured programming was all the rage. In both my books and courses, I've seen something like this: Public Class...
8
1766
by: Fredrik Melin | last post by:
I have a "Inventory" Object that contains the product and all its fields. The problem is that I am getting soooooo many functions under main Inventory class so it becames impossible to initalize...
10
6991
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
3
1468
by: Ray | last post by:
OK, maybe I shoot a more general question to the group since there are so many great programmers here: how do you practice your craft? I do it in the following way: 1. Set aside 30 minutes to...
0
7049
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
6912
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
7092
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
6981
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...
1
4790
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...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.