Changing object pointed to in an array (C++)

Started by
5 comments, last by DangerDave 17 years, 9 months ago
I'm having a bit of troble getting my head around memory pointers. OK, I have an array storing pointers to objects. The objects themselves have a couple of direct pointers to other objects (also stored in the array). I want to change the object pointed to by both by just changing the pointers in the objects, maybe by dereferencing or something, so I dont have to search the array for a match. A simple question for someone who knows how, but I dont know how well this explains it... Cheers,
Dave.
Advertisement
Quote:Original post by DangerDave
OK, I have an array storing pointers to objects. The objects themselves have a couple of direct pointers to other objects (also stored in the array). I want to change the object pointed to by both by just changing the pointers in the objects, maybe by dereferencing or something, so I dont have to search the array for a match.
It sounds like you're just asking for trouble here :-) In any case I think some more info would be helpful, such as what language and container type you're using. Perhaps you could also post some of the code associated with your class, and a code example or more detailed description of what you're trying to do.

[Edit: Also perhaps clarify what you mean by 'search the array for a match'.]
OK, looking back at my code, I lied a bit. I'm using STL vector, not an array.


My, ahem, array...
vector <Tri*> m_vpAgentsTriList;


My objects storing 2 references to Tri objects, that are also referenced somewhere in the vector:
Tri* pAdjTris[2];pAdjTris[0] = m_vpAgentsEdgeList[iCurrEdge]->pAdjTris[0];pAdjTris[1] = m_vpAgentsEdgeList[iCurrEdge]->pAdjTris[1];


So I want to change pAdjTris[0], and that will then also update the relevant element in the vector. Right now I'm searching the entire vector for 'pAdjTris[x] == m_vpAgentsTriList[y]' then setting m_vpAgentsTriList[y] as the new value.
Dave.
It looks like you're managing mesh connectivity, correct?

How are the 'Tri' objects themselves being stored? Keep in mind that if they themselves are stored in a vector<>, all of your Tri pointers are in constant danger of being invalidated if the memory management is not handled exactly right. This may be an indication that there's room for improvement in the overall design.

I don't know that this is necessarily the way to go, but in my mesh class I decided to handle all connectivity info via indexing rather than pointers or smart pointers. It can make the syntax a little more verbose, but (with proper bounds checking) eliminates the many pitfalls of the pointer-based approach.

It may be that a robust pointer-based implementation is possible and practical, but would, I think, take some care to implement correctly. In any case, you may want to look into the Boost smart pointer family (in particular, use of 'weak reference' pointers might be appropriate here).

I didn't actually address your particular question (partly because I don't understand it completely), but again, if a problem doesn't have an obvious solution in a given architecture it can be an indication that a change of architecture would be appropriate. In this case that might mean using a different STL container or set of containers, or a change in the relationships between the various objects and components.

Just my $.02.
jyk: Yes, its to do with a mesh. The problem with storing indices is that my mesh is adadaptive, so things change over time, plus there are a few other issues that would make pointers much more useful.
Dave.
Correct.

pointer to pointer

Kuphryn
Quote:Original post by kuphryn
Correct.

pointer to pointer

Kuphryn


Thats what I was thinking, a pointer to a pointer. But I'm having problems implementing and not sure what I'm doing wrong. A simple example code snippet would be most useful...

Cheers,

Dave.

This topic is closed to new replies.

Advertisement