Pointers behaving strange!?
I am working on a mesh class and has encountered some weird problems.
The vertices are stored in a vector and the triangles in another. Each triangle has three pointers to vertex objects in the first vector. Each vertex object has a vector containing pointers to all triangles that makes use of it.
The problem occurs when I try to do this, vertices[24].parenttriangels[4]->A
The Triangle''s first pointer is completely bogus while the two others are right. Whats really confusing though is that that pointer actually is right earlier in the program. And I don''t touch these pointers between these two occasions.
What seems to have happened is that some pointers actually has changed sponataneously without me touching them.
Have checked my code at least 25 times the last two weeks and I just can''t find the problem... HEEEEELP, I''m getting desperate
Snale
+--My humble and superior homepage
Snale+--My humble and superior homepage
it would help if you post some code, but i can take a shot at it. Is it possible that the pointers are being passed into a function, modified there (and they are right when you check them there), then when the function exits they aren''t right anymore?
Here is an example of this problem. The char *pig is modified in the function, but the modification is lost due to the pass by value nature of C.
i hope this helps.
int main() {
char *pig;
makestring(pig);
// line below won''t print what you want
printf("After the call, the string is: %s\n", pig);
}
makestring(char *astring) {
astring = (char *) malloc(sizeof(char)*8);
sprintf(astring, "the_pig");
printf("Here is the string: %s\n", astring);
}
Here is an example of this problem. The char *pig is modified in the function, but the modification is lost due to the pass by value nature of C.
i hope this helps.
int main() {
char *pig;
makestring(pig);
// line below won''t print what you want
printf("After the call, the string is: %s\n", pig);
}
makestring(char *astring) {
astring = (char *) malloc(sizeof(char)*8);
sprintf(astring, "the_pig");
printf("Here is the string: %s\n", astring);
}
Just to let anyone know who cant figure out why kiev_class''s code in his post wont work like you want is because the function should use double indirection in the paramater list. Which means makestring( char **astring ). Then in makestring every time you access astringm like when you do the malloc and sprintf, you should put a * before astring.
Just thought id share that.
-SirKnight
![](smile.gif)
-SirKnight
Well... the problem isn''t something like that. The problem is that one pointer changes, while the others remain intact but I do the same thing with all of them...
some source, don''t know if this helps. Everything needed to understand the problem should be there, but I can post some more if needed.
some source, don''t know if this helps. Everything needed to understand the problem should be there, but I can post some more if needed.
|
Snale+--My humble and superior homepage
The vectors you show have only pointers on them, but do you have other vectors that have objects on them instead of pointers? When you insert a new item into a vector, it reshuffles the contents to make room for the new item. If your objects themselves contain pointers, when it gets copied to the new location (uses the assignment operator or copy constructor), weird things happen for the pointer members. The pointer gets deleted by the destructor at the old location, but it just copies the pointer value to the new location, so you end up with an invalid pointer. This one bites me alot. Don''t know if that is your problem or not but based on the symptoms you describe it certainly sounds like it. Just a thought
Wait. I think I see it.
You have this code:
triangles.push_back( T );
Triangle *ptr = triangles.end();
You should use triangles.back() instead. the end() is just a placeholder and isn''t a real object with a real pointer that you can use like that.
Hope that helps
You have this code:
triangles.push_back( T );
Triangle
You should use triangles.back() instead. the end() is just a placeholder and isn''t a real object with a real pointer that you can use like that.
Hope that helps
Thanks
Have to test that...
Started to convert my code into using indexes instead, but that would require an almost complete redesign of everything so I''m more or less stuck with what I have
Snale
+--My humble and superior homepage
Have to test that...
Started to convert my code into using indexes instead, but that would require an almost complete redesign of everything so I''m more or less stuck with what I have
Snale
+--My humble and superior homepage
Snale+--My humble and superior homepage
Whoohoo..
That solved 9 of 10 problems..
Thanks a lot.
vector::end() returns a iterator, right?
should have thought about that..
Snale
+--My humble and superior homepage
Edited by - snale on May 4, 2001 11:10:28 AM
That solved 9 of 10 problems..
Thanks a lot.
vector::end() returns a iterator, right?
should have thought about that..
![](sad.gif)
Snale
+--My humble and superior homepage
Edited by - snale on May 4, 2001 11:10:28 AM
Snale+--My humble and superior homepage
I noticed that the vector class sometimes moves data around when you add things to it, even with push_back, to avoid fragmentation and ensure faster access (I suppose).
So I solved the last few problems by simply adding everything before retrieving the pointers.
Snale
+--My humble and superior homepage
So I solved the last few problems by simply adding everything before retrieving the pointers.
Snale
+--My humble and superior homepage
Snale+--My humble and superior homepage
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement