swaping vectors & arrays

J

J. Campbell

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
 
A

Alf P. Steinbach

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?
 
B

Buster

J. Campbell said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top