By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

swaping vectors & arrays

P: n/a
I have a class that contains an array of integers that hold the state
of my 'system'. The system must be updated periodically. I need to
update the whole system at once, at which point the system is in a new
state. The update process depends upon the entire system state prior
to updating...that is, a single array cannot be serially updated, with
the results stored back into itself.

Just to give an indication of what I'm doing, the system size is circa
6000 bytes, and in the course of running the progam, the system may be
updated from 50,000 to many millions of times. I don't know the
system size until runtime, but once the system size is set, it never
needs to be changed.

What I've been doing is creating 2 pointers in the class definition:

int* array;
int* dup_array;

then, in the class constructor, I create 2 arrays using:

array = new int [a_size];
dup_array = new int [a_size];

Then, when I update the system, I update it into dup_array. After
updating the system, I swap the pointers rather than the contents of
the arrays. So that after the swap, the current system state is
located in array, and the preceeding system state is located in
dup_array.

The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.

I do realize that with vectors I wouldn't need to free the memory in
the class destructor, which is an obvious advantage.

Thanks
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 29 Sep 2003 19:38:16 -0700, ma**********@yahoo.com (J. Campbell) wrote:
I have a class that contains an array of integers that hold the state
of my 'system'. The system must be updated periodically. I need to
update the whole system at once, at which point the system is in a new
state. The update process depends upon the entire system state prior
to updating...that is, a single array cannot be serially updated, with
the results stored back into itself.

Just to give an indication of what I'm doing, the system size is circa
6000 bytes, and in the course of running the progam, the system may be
updated from 50,000 to many millions of times. I don't know the
system size until runtime, but once the system size is set, it never
needs to be changed.

What I've been doing is creating 2 pointers in the class definition:

int* array;
int* dup_array;

then, in the class constructor, I create 2 arrays using:

array = new int [a_size];
dup_array = new int [a_size];

Then, when I update the system, I update it into dup_array. After
updating the system, I swap the pointers rather than the contents of
the arrays. So that after the swap, the current system state is
located in array, and the preceeding system state is located in
dup_array.

The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.

I do realize that with vectors I wouldn't need to free the memory in
the class destructor, which is an obvious advantage.


Uh, have you checked out std::swap? Not to mention the particulars of
the std::vector class?

Jul 19 '05 #2

P: n/a
"J. Campbell" <ma**********@yahoo.com> wrote
The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.


Why would you need 2 pointers as well as the 2 vectors?
Have you considered the swap () member function of vector <>?

Regards,
Buster.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.