Obtaining a function pointer from an iterator over a function deque
I'm trying to build a simple deficit round robin scheduler, and right now I'm trying to get the function that has had the least time running. I'm getting a "Bus Error" returned to me when I try to convert an iterator to a pointer. Below is my code, ptof is simply a typedef pointer to the kind of functions I have in my deque and State has the info for each process. What am I doing wrong? Your help would be greatly appreciated.
ptof leastTime(deque<ptof> fnc, map<ptof, State *> s){
double leastElapsed= 100000000;
ptof f;
deque<ptof>::iterator it;
for (it=fnc.begin() ; it < fnc.end(); it++ ){
if(s[*it]->elapsed<leastElapsed){
f = (ptof)(&(*it));
cout<< s[f]->name<&l开发者_StackOverflowt;endl;
}
}
return f;
}
f = (ptof)(&(*it));
That would be taking the address of the function pointer. Try
f = *it;
In any case, avoid C-style casts. If you tried a static_cast
instead, the compiler would tell you that the cast is invalid.
Taking the address of what the iterator points to is rather odd too, it looks like you're casting a pointer to a function pointer to a function pointer, which isn't what you wanted I don't think.
The c-style cast doesn't help because it could be doing any kind of cast, even one you didn't want.
For something like this in C++ it might be cleaner to define an interface with a virtual function to avoid the mess that can be function pointers.
精彩评论