0

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<<endl;
     }
  }
return f;
}
gCV
  • 11
  • 2
  • is ptof just a typedef for a pointer to a function? it's helpful if we could see that (and a minimal working example would make things even easier to diagnose) – Flexo Nov 15 '10 at 23:27
  • I'm very suspicious of the c-style cast too - it could be hiding all kinds of evil things you didn't want it to. Taking the address of what the iterator points to is rather odd too. – Flexo Nov 15 '10 at 23:28
  • 1
    Whoa, do you know that you're actually _copying_ those parameters into the function? [How to pass objects to functions in C++?](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254) – sbi Nov 15 '10 at 23:29
  • Can you post the exact error message? – suszterpatt Nov 15 '10 at 23:29
  • 1
    Please: (A) properly indent the code (this hurts to read), (B) add the necessary declarations so we can paste this into our editors and try to compile it. – sbi Nov 15 '10 at 23:32
  • Side note: use a priority_queue to fetch items in some defined order. Also, don't pass containers by value, `deque`, pass references, `const deque&`. – Marcelo Cantos Nov 15 '10 at 23:33

2 Answers2

3
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.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
0

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.

Flexo
  • 87,323
  • 22
  • 191
  • 272