A list of objects with a template member shares address, why ???

D

Dennis

Hi

I have a little problem as stated above (haven't found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :eek:(
When I used an array instead of a template there where no problem. How
do I initialise the template vector so each particle has it's own
template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?

Thanks for your help

Dennis
 
J

John Harrison

Dennis said:
Hi

I have a little problem as stated above (haven't found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :eek:(
When I used an array instead of a template there where no problem. How
do I initialise the template vector so each particle has it's own
template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?

Here are a list of things that are undefined in the code you posted.

TNT::Array1D<double>
noParticles
emitNoParticles
emit_iterator

I cannot answer this question because these things are undefined, therefore
I cannot understand your code any better than you can.

Please read this, these are some guidelines about how to post code. Follow
these guidelines and you will get your question answered quickly.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

john
 
J

John Harrison

On second thoughts maybe the answer is that TNT::Array1D is rubbish.

Why not use the *standard* vector class std::vector rather than dubious
third party stuff.

typedef std::vector<double> dVector3;

I can promise you that a std::vector does not share memory with another
std::vector. Another advantage of using standard classes is that you can
post code here and everyone will know what you are using.

john
 
D

Dennis

Dennis said:
Hi

I have a little problem as stated above (haven't found any solution
on the www). I have a list of objects (particles) where I have a
member of a template vector (array1d from TNT). I initialise these
members in the constructor, but when I write different vector values
to different particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :eek:(
When I used an array instead of a template there where no problem.
How do I initialise the template vector so each particle has it's
own template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?

Here are a list of things that are undefined in the code you posted.

TNT::Array1D<double>
noParticles
emitNoParticles
emit_iterator

I cannot answer this question because these things are undefined,
therefore I cannot understand your code any better than you can.

Well I thought it would be of no interrest to know the data types for
the above, but the noParticles, emitNoParticle are integers and
emit_iterator is an iterator for the list. But it isn't the iterator
that is a problem as it works when i'm using the array.

Regarding the use of a 3'rd party template is that I don't have to
implement all the functionality needed to make vector and matrix
calculations as that is done in advance.

Dennis
 
J

John Harrison

Dennis said:
Dennis said:
Hi

I have a little problem as stated above (haven't found any solution
on the www). I have a list of objects (particles) where I have a
member of a template vector (array1d from TNT). I initialise these
members in the constructor, but when I write different vector values
to different particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :eek:(
When I used an array instead of a template there where no problem.
How do I initialise the template vector so each particle has it's
own template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?

Here are a list of things that are undefined in the code you posted.

TNT::Array1D<double>
noParticles
emitNoParticles
emit_iterator

I cannot answer this question because these things are undefined,
therefore I cannot understand your code any better than you can.

Well I thought it would be of no interrest to know the data types for
the above, but the noParticles, emitNoParticle are integers and
emit_iterator is an iterator for the list. But it isn't the iterator
that is a problem as it works when i'm using the array.

Regarding the use of a 3'rd party template is that I don't have to
implement all the functionality needed to make vector and matrix
calculations as that is done in advance.

Dennis

Perhaps there is a bug in TNT::Array1D, perhaps there isn't a bug and you
are just using it incorrectly, perhaps its something completely different.
Taking a wild guess perhaps TNT::Array1D uses reference semantics and you
have to define a suitable copy constructor for sParticle, consult your
documentation.

I don't understand how you expect anyone to be able to solve your problem
with the amount of information given.

john
 
D

David Harmon

On 04 Apr 2004 12:26:06 +0200 in comp.lang.c++, Dennis
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

I second John's objections, and add that it seems unlikely that you
really want to increment k twice in this loop.
 
D

Dennis

John Harrison said:
Perhaps there is a bug in TNT::Array1D, perhaps there isn't a bug and you
are just using it incorrectly, perhaps its something completely different.
Taking a wild guess perhaps TNT::Array1D uses reference semantics and you
have to define a suitable copy constructor for sParticle, consult your
documentation.

Either it is a bug or I have to make a suitable copy constructor,
because it works with the STL::vector
I don't understand how you expect anyone to be able to solve your problem
with the amount of information given.

I'm so sorry, I just hoped that somebody that were using the TNT
package were able to help me. But thanks for your time anyway.

Dennis
 
K

Kevin Goodsell

Dennis said:
I'm so sorry, I just hoped that somebody that were using the TNT
package were able to help me. But thanks for your time anyway.

I've never even heard of TNT. This is usually not the best place to find
help with particular non-standard libraries. Aside from being off-topic
here, the people qualified to help with them are usually elsewhere. The
library vendor may have a forum or mailing list you can try -- that's
usually the best place to look.

-Kevin
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top