|
STL...any help please... |
Don Freeman
Member #5,110
October 2004
|
I can remember how to do this correctly, so any advice would be welcome. I am trying to make a clock manager that will update all the clocks that get registered. The clock manager will continue to update the clocks until one gets to it's time limit. Then the clock manager will signal that a clock needs servicing, and which clock it is. The clock manager does not reset the clock by itself, it is left up to the program to reset the clock...might add a feature that will reset automatically after a certain amount of time. So we have the clock class and a clock manager. Both are listed below:
I guess my question is: How do I return a pointer from the STL list for the FindClock function. I am using the iterators of the list, but it does not allow for a pointer to be returned. I need a pointer, because if there is no match...then I can't use a reference as there would be nothing to reference! Thanks in advance...and HAPPY NEW YEARS EVERYONE! ( I'm not doing so great right now...had this fever of 103F for like 2 days now and hurt all over... ) -- |
CGamesPlay
Member #2,559
July 2002
|
containerType<Clock>::iterator i; Clock* c = &*i; That's a pointer from an iterator. If the iterator become invalid, so will that pointer though. Keep that in mind when choosing your container. -- Ryan Patterson - <http://cgamesplay.com/> |
Don Freeman
Member #5,110
October 2004
|
I had: return (Clock*)*&i; I tryed: return (Clock*)&*i; Still get an error when I try to use it with: I add the clocks using:
I believe that is the right way to add them, right? I still get errors when trying to access any element (clock) other than the first one. -- |
CGamesPlay
Member #2,559
July 2002
|
I have no idea what you're talking about: Quote: I believe that is the right way to add them, right? You're writing the class. How should I know what the correct way to use it is? Furthermore, you so far have not shown one line of STL code. Quote: Still get an error when I try to use it with: YOu'd have better luck telling me what the error is. -- Ryan Patterson - <http://cgamesplay.com/> |
Don Freeman
Member #5,110
October 2004
|
Ok...thanks for the harsh reality... UI.h:
UI.cpp:
Main.cpp:
And the error code: The instruction at "0x00401494" referenced memory at "0x00000010". The memory could not be "read". -- |
IonBlade
Member #3,521
May 2003
|
Put the clocks in structs, and put a string in the struct for the clock's name, then search for the name.
and using it... //main.cpp cClock* myclock = new cClock(); cClockManager* MyClockManager = new cClockManager(); MyClockManager->AddClock(myclock,"clock_1"); cClock* otherclock = MyClockManager->GetClock("clock_1"); //voila I have not tested this code, but I use something similar for various data types in my game. ------ "I've got to choose my words carefully so that I'm not misunderstood, but I think there are fewer developers around today who think about making a good game. As a creator, I find that sad..." - Hideo Kojima |
Wil Renczes
Member #5,921
June 2005
|
Instead of stashing naked Clock pointers in your container, what you could do instead is stash smart pointers - basically, some kind of reference countrf wrapper to your object (although I've never used it, I hear boost should do the trick). I think you'll find that'll solve all your problems, since you can simply return the object itself, ie *iter, and you don't need to worry about the iterator pointer becoming invalid, etc. |
CGamesPlay
Member #2,559
July 2002
|
IonBlade's method is unnecessary. If you want to give clocks a name, make them hold the name, don't make a seperate struct for holding a name and a reference to a clock. Wil's method is unnecessary because this application has no need for pointers at all. Your method has some problems, but you're on the right track Quote: typedef list<Clock> ClockList; This means that your container is a std::list. Quote: extern ClockList *pClockList; You don't need this global variable, get rid of it. Quote:
class ClockManager { // ... ClockList *pClockList; };
There is no need for this variable to be a pointer. Making it a pointer is just going to complicate things. Quote:
delete pClock; pClock = NULL; pClock = new Clock;
This code isn't actually necessary, because RegisterClock makes a copy of the Clock that it stores. You can remove all 3 of these lines and your code will still work. -- Ryan Patterson - <http://cgamesplay.com/> |
Don Freeman
Member #5,110
October 2004
|
Thanks all...can anyone test this on their machine? I want to make sure it's my fault and not windows xp or my hardware. I've seen this stupid error message before from other programs...tested my memory for like 3 hours and everything seemed fine. Did a quick google search for "The memory could not be "read"" and it looks like it might be a problem with xp that microsoft doesn't acknowledge yet. Edit: -- |
Carrus85
Member #2,633
August 2002
|
Um, CGames, the way his code is currently implemented, that delete, setting of the pointer to null, IS necessary. The code generates a copy of the object, NOT the pointer, because the container he has is of Clock objects, not Clock Pointers. Now, if his code was something like this, the delete call would be unnecessary: std::list<Clock*> clockList; Clock* myClock = new Clock( 1, 2, 3 ); clockList.push_back(myClock); // the pointer itself is copied, thus, the clock object is being refrenced by the pointer in the container, avoiding a memory leak // In other words, myClock == *i, where i is an iterator pointing to the new object just added // Of course, you'll have to do something like this at the end std::list<Clock*>::iterator i; for ( i = clockList.begin(); i != clockList.end(); ++i ) delete *i; // Delete the pointer the iterator is pointing to. However, since his code is this: std::list<Clock> clockList; // Note, this is not a list of pointers, it is a list of clock objects. Clock* myClock = new Clock ( 1, 2, 3 ); clockList.push_back(*myClock); // This calls the COPY CONSTRUCTOR to generate the object in the list // in other words, myClock != &*i, where i is an iterator pointing to the new object. // If you don't delete here, you're going to have a memory leak in the myClock object In other words, since RegisterClock makes a copy of the Clock itself, not the Clock pointer, you must delete the Clock*. EDIT: Clarified some things
|
HoHo
Member #4,534
April 2004
|
Quote: Anyone have a good link for STL docs?
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html __________ |
kentl
Member #2,905
November 2002
|
For a reference I use http://cppreference.com but it might not prove to be a good source to learn how/when to use STL. |
CGamesPlay
Member #2,559
July 2002
|
Quote: Um, CGames, the way his code is currently implemented, that delete, setting of the pointer to null, IS necessary. The code generates a copy of the object, NOT the pointer, because the container he has is of Clock objects, not Clock Pointers. Therefore, he will modify one single object and generate multiple copies of it. The lines are not necessary. -- Ryan Patterson - <http://cgamesplay.com/> |
Don Freeman
Member #5,110
October 2004
|
CGamesPlay: I tryed a new quick program to test if I did it right or whatever...and it seems to work, so the problem is somewhere else it seems. Here is the new code that I tested and it does just what I need my other one to do...
Sorry for the crappy code...was just really wanting to see if it works or if I was doing the FindObject code wrong. I may have missed something somewhere, but it seems like the same code to me...but it works on this and not the other. At least I know why I've been feeling so bad lately...I've got Strep Throat! Sucks balls! Can't wait to go see the doc tomarrow. I can deal with just a sore throat or a fever...but man, Strep makes you feel like real Sh.t! Hurt all over, tired but can't sleep, just all together sucks! -- |
CGamesPlay
Member #2,559
July 2002
|
Quote: Yes...I do need to delete those objects. They are being created by new and are copyed seperately into the stl::list...therefore, without being deleted, I would have created a memory leak since the stl::list copy is a completely seperate copy than the original. Sorry, you misunderstood me. I said the deleteing and making a new one is not necessary. If you make 1 new clock object and modify it and delete it once, it will have the same effect as what you do now. I'm going to wai tuntil you try doing what I've suggested before I post again. -- Ryan Patterson - <http://cgamesplay.com/> |
Don Freeman
Member #5,110
October 2004
|
Ok....I found the stupid error! I was missing: pClock->iID = iNumClocks; This was meant to be in the pClockMGR->Register() function. Obviously since all of the IDs where the same...it couldn't return the correct one! Stupid and easy to forget...but hard to find. I type to fast for my own good some times! CGamesPlay: So here is the updated code for those who care:
UI.cpp:
Main.cpp:
That's all of it and it works now! I kinda thought it was something simple an stupid that I forgot! Thanks for everyone's help on this! Still got a lot of work for this planned...but now that I got this fixed, it should be easy from there. -- |
Carrus85
Member #2,633
August 2002
|
Quote:
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html HAH! Cover of Volume Two said: Chuck Allision
Chuck Allision is one of my Computer Science Professors this year! w00t! EDIT: Actually, make that two classes: Computational Theory and C++ Software Development
|
|