C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

swaping vectors & arrays

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
J. Campbell
Guest





PostPosted: Tue Sep 30, 2003 2:38 am    Post subject: swaping vectors & arrays Reply with quote



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
Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Sep 30, 2003 3:07 am    Post subject: Re: swaping vectors & arrays Reply with quote



On 29 Sep 2003 19:38:16 -0700, [email]mango_maniac (AT) yahoo (DOT) com[/email] (J. Campbell) wrote:

Quote:
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?


Back to top
Buster
Guest





PostPosted: Tue Sep 30, 2003 9:20 am    Post subject: Re: swaping vectors & arrays Reply with quote



"J. Campbell" <mango_maniac (AT) yahoo (DOT) com> wrote

Quote:
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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.