LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2007, 07:55 AM   #1
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Rep: Reputation: 22
C++: trying to avoid copies and pointers... no luck


Well, I am writing a code to perform a 2D random walk: there are sites

struct Site {
index;
species;
}

and a lattice

class Lattice {
public:
// ctor, dtor, functions, methods, blah blah blah
int randomWalk(const int position);
private:
// more stuff
vector<Site> _sites;
}

the idea is to move a species from _sites.at(0) to _sites.at(1), like
randomWalk(pos) {_sites.at(pos+1).species = _sites.at(pos).species; _sites.at(pos).species = 0; return pos+1;}
but it is a bit more complicated so I would like to do something similar to
... { Site origin = _sites.at(pos); Site destination = _sites.at(pos+1); // the rest }
this obviously does not work because the species is moved on the copies and not on the lattice.

On the first version, I was using a vector of pointers to "Site"s
vector<Site*> _sites;
and it was working because I was not making a copy but using the address of a site really into the lattice.

Now, I can make it work if I update the lattice, since the copies have the correct index.

So it is not like I cannot fix it.

BUT, the question is, is there an elegant way to do what I want, without making unnecessary copies and using a vector of objects instead of a vector of pointers?

Note that this is a *random* walk, so the destination is actually not at pos+1, but needs to be calculated in the real program. And I have boundary conditions, so the arrival can be at pos+(x dimension of the lattice) - (size of the lattice) or any non trivial shift from the origin.

I'd really appreciate a nice solution before I go back to the vector of pointers...
 
Old 07-17-2007, 08:02 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
to me a vector of pointers is superior to a vector of objects, assuming they are of significant size.
 
Old 07-17-2007, 12:02 PM   #3
_john_i_
Member
 
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52

Rep: Reputation: 15
If you have the boost libraries, using a boost::shared_ptr is perfect for stuff like this.


typedef boost::shared_ptr<Site> SitePtr;


Then make your vector:

vector<SitePtr> _sites;


Now it will work the same as a vector<Site *>, but you don't have to worry about deleting the pointers when you are done with them, and boost::shared_ptr is safe to use in containers unlike auto_ptr.

Just the shared_ptr object get copied. They use reference counting to know when they are no longer used, and delete the objects.
 
Old 07-17-2007, 11:27 PM   #4
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Original Poster
Rep: Reputation: 22
Thank you for your answers, so I will go back to the vector of pointers, then. I'll have a look at boost::shared_ptr too but in principle, the size should not change. And yes, the size will be significant, like 128*128 or 256*256, maybe.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
two copies of login in iptables packets Linux - Security 5 02-05-2007 03:33 AM
Too many copies come out of printer Phrenchax Linux - Hardware 1 07-01-2006 04:12 AM
Two Copies Of XFCE Starting? douceur Linux - Software 1 06-19-2005 01:05 PM
How to burn Simultaneous copies? mcle Linux - General 8 01-04-2005 10:41 AM
Printing multiple copies wjn Linux - Networking 4 12-16-2003 07:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration