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