Why is the value of "Instance" whacked in the following code. Note this
only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be
BredthFirst and BredthFirstPrint and make both approriate func ptrs it works
fine.
void print(void *data);
#define R 'r'-'r'
#define S 's'-'r'
#define T 't'-'r'
#define U 'u'-'r'
#define V 'v'-'r'
#define W 'w'-'r'
#define X 'x'-'r'
#define Y 'y'-'r'
int main()
{
int i;
Graph *graph;
char *s="rstuvwxy";
graph=graph->init();
for(i=0;i<8;i++)
graph->addNode(&s[i]);
graph->addEdge(&s[R],&s[S]);
graph->addEdge(&s[R],&s[V]);
graph->addEdge(&s[S],&s[W]);
graph->addEdge(&s[W],&s[S]);
graph->addEdge(&s[W],&s[T]);
graph->addEdge(&s[T],&s[X]);
graph->addEdge(&s[T],&s[u]);
graph->addEdge(&s[u],&s[Y]);
graph->addEdge(&s[X],&s[Y]);
graph->walk();
graph->print(print,&s[R],&s[Y]);
}
void print(void *data)
{
cout<<*RI_CAST<char *>(data)<<endl;
}
class Graph {
public:
Graph *init(unsigned long walkfunc=0,unsigned long printfunc=0);
void addNode(void *data);
void addEdge(void *d1, void *d2);
GNode *findNode(void *data);
void walk();
void print(void (*func)(void *data),void *d1,void *d2);
static void BredthFirstWrapper();
static void
BredthFirstPrintWrapper(void (*func)(void *data),GNode
*node,GNode *node2);
void Dump();
//protected:
Graph(unsigned long walkfunc=0,unsigned long printfunc=0);
private:
void BredthFirst();
void BredthFirstPrint(void (*func)(void *data),GNode *node,GNode
*node2);
void reset();
static Graph *Instance;
LinkedList *Nodes;
unsigned long WalkFunc;
unsigned long PrintFunc;
};
Graph *Graph::Instance=0;
Graph *Graph::init(unsigned long walkfunc,unsigned long printfunc)
{
return new Graph(walkfunc,printfunc);
}
void Graph::addNode(void *data)
{
GNode *node;
node=node->init(data);
Nodes->addNode(node);
}
void Graph::addEdge(void *d1,void *d2)
{
GNode *node;
GNode *node2;
node=findNode(d1);
node2=findNode(d2);
node->addEdge(node2);
node2->addEdge(node);
}
GNode *Graph::findNode(void *data)
{
GNode *node;
Nodes->reset(); // paranoia
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
if(node->getData()==data) {
Nodes->reset();
return node;
}
Nodes->reset();
return 0;
}
void Graph::walk()
{
Instance=this;
(RI_CAST<void (*)(Graph *)>(WalkFunc))(this);
}
void Graph::print(void (*func)(void *data),void *d1,void *d2)
{
GNode *node;
GNode *node2;
node=findNode(d1);
node2=findNode(d2);
if(node==0||node2==0) {
cout<<"Can't find one of the path ends for this graph print"<<endl;
exit(1);
}
Instance=this;
(RI_CAST<void (*)(Graph *,void (*)(void *),GNode *, GNode *)>(PrintFunc))
(this,func,node,node2);
}
void Graph::BredthFirstWrapper()
{
if(Instance==0)
return;
Instance->BredthFirst();
}
void Graph::BredthFirstPrintWrapper(void (*func)(void *data),GNode
*node,GNode *node2)
{
if(Instance==0)
return;
cout<<node<<" "<<node2<<endl;
Instance->BredthFirstPrint(func,node,node2);
}
void Graph::Dump()
{
GNode *node;
cout<<"Nodes: "<<endl;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
node->Dump();
Nodes->reset();
}
Graph::Graph(unsigned long walkfunc,unsigned long printfunc)
{
Nodes=Nodes->init();
if(walkfunc==0)
walkfunc=RI_CAST<unsigned long>(&Graph::BredthFirstWrapper);
WalkFunc=(walkfunc);
if(printfunc==0)
printfunc=RI_CAST<unsigned long>(&Graph::BredthFirstPrintWrapper);
PrintFunc=printfunc;
}
void Graph::BredthFirst()
{
GNode *node;
GNode *tmp;
Queue *queue;
LinkedList *edges;
reset();
node=RI_CAST<GNode *>(Nodes->getNext());
if(node==0)
return;
node->setColor(GNodeColorGrey);
queue=queue->init();
queue->insert(node);
node->setDepth(0);
node->setPrev(0);
do {
node=RI_CAST<GNode *>(queue->peek());
cout<<"processing node "<<node<<endl;
edges=node->getEdges();
while((tmp=RI_CAST<GNode *>(edges->getNext()))!=0)
if(tmp->getColor()==GNodeColorWhite) {
tmp->setColor(GNodeColorGrey);
tmp->setDepth(node->getDepth()+1);
tmp->setPrev(node);
queue->insert(tmp);
}
edges->reset();
node=RI_CAST<GNode *>(queue->get());
if(node==0)
break;
node->setColor(GNodeColorBlack);
} while(queue->getSize()>0);
}
void Graph::BredthFirstPrint(void (*func)(void *data),GNode *node, GNode
*node2)
{
if(node==node2) {
func(node->getData());
return;
}
if(node2->getPrev()==0) {
cout<<"No path from "<<node<<" to "<<node2<<endl;
exit(1);
}
BredthFirstPrint(func,node,node2->getPrev());
func(node2->getData());
}
void Graph::reset()
{
GNode *node;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
node->reset();
Nodes->reset();
} 3 1547
You might want to shorten and simplify your code, to pinpoint what fails.
This way you will either
1. find the bug
2. be able to post a message which more people might go over
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message
news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it
works fine. void print(void *data);
#define R 'r'-'r' #define S 's'-'r' #define T 't'-'r' #define U 'u'-'r' #define V 'v'-'r' #define W 'w'-'r' #define X 'x'-'r' #define Y 'y'-'r'
int main() { int i; Graph *graph; char *s="rstuvwxy";
graph=graph->init();
for(i=0;i<8;i++) graph->addNode(&s[i]);
graph->addEdge(&s[R],&s[S]); graph->addEdge(&s[R],&s[V]); graph->addEdge(&s[S],&s[W]); graph->addEdge(&s[W],&s[S]); graph->addEdge(&s[W],&s[T]); graph->addEdge(&s[T],&s[X]); graph->addEdge(&s[T],&s[u]); graph->addEdge(&s[u],&s[Y]); graph->addEdge(&s[X],&s[Y]);
graph->walk(); graph->print(print,&s[R],&s[Y]); }
void print(void *data) { cout<<*RI_CAST<char *>(data)<<endl; } class Graph { public: Graph *init(unsigned long walkfunc=0,unsigned long printfunc=0);
void addNode(void *data); void addEdge(void *d1, void *d2);
GNode *findNode(void *data); void walk(); void print(void (*func)(void *data),void *d1,void *d2);
static void BredthFirstWrapper(); static void BredthFirstPrintWrapper(void (*func)(void *data),GNode *node,GNode *node2);
void Dump();
//protected: Graph(unsigned long walkfunc=0,unsigned long printfunc=0);
private: void BredthFirst(); void BredthFirstPrint(void (*func)(void *data),GNode *node,GNode *node2); void reset();
static Graph *Instance;
LinkedList *Nodes; unsigned long WalkFunc; unsigned long PrintFunc; };
Graph *Graph::Instance=0;
Graph *Graph::init(unsigned long walkfunc,unsigned long printfunc) { return new Graph(walkfunc,printfunc); }
void Graph::addNode(void *data) { GNode *node;
node=node->init(data); Nodes->addNode(node); }
void Graph::addEdge(void *d1,void *d2) { GNode *node; GNode *node2;
node=findNode(d1); node2=findNode(d2);
node->addEdge(node2); node2->addEdge(node); }
GNode *Graph::findNode(void *data) { GNode *node;
Nodes->reset(); // paranoia while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) if(node->getData()==data) { Nodes->reset();
return node; }
Nodes->reset();
return 0; }
void Graph::walk() { Instance=this; (RI_CAST<void (*)(Graph *)>(WalkFunc))(this); }
void Graph::print(void (*func)(void *data),void *d1,void *d2) { GNode *node; GNode *node2;
node=findNode(d1); node2=findNode(d2);
if(node==0||node2==0) { cout<<"Can't find one of the path ends for this graph print"<<endl; exit(1); }
Instance=this; (RI_CAST<void (*)(Graph *,void (*)(void *),GNode *, GNode *)>(PrintFunc)) (this,func,node,node2); }
void Graph::BredthFirstWrapper() { if(Instance==0) return;
Instance->BredthFirst(); }
void Graph::BredthFirstPrintWrapper(void (*func)(void *data),GNode *node,GNode *node2) { if(Instance==0) return;
cout<<node<<" "<<node2<<endl; Instance->BredthFirstPrint(func,node,node2); }
void Graph::Dump() { GNode *node;
cout<<"Nodes: "<<endl;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) node->Dump();
Nodes->reset(); }
Graph::Graph(unsigned long walkfunc,unsigned long printfunc) { Nodes=Nodes->init();
if(walkfunc==0) walkfunc=RI_CAST<unsigned long>(&Graph::BredthFirstWrapper);
WalkFunc=(walkfunc);
if(printfunc==0) printfunc=RI_CAST<unsigned long>(&Graph::BredthFirstPrintWrapper);
PrintFunc=printfunc; }
void Graph::BredthFirst() { GNode *node; GNode *tmp; Queue *queue; LinkedList *edges;
reset();
node=RI_CAST<GNode *>(Nodes->getNext()); if(node==0) return;
node->setColor(GNodeColorGrey);
queue=queue->init(); queue->insert(node);
node->setDepth(0); node->setPrev(0);
do { node=RI_CAST<GNode *>(queue->peek()); cout<<"processing node "<<node<<endl; edges=node->getEdges();
while((tmp=RI_CAST<GNode *>(edges->getNext()))!=0) if(tmp->getColor()==GNodeColorWhite) { tmp->setColor(GNodeColorGrey); tmp->setDepth(node->getDepth()+1); tmp->setPrev(node);
queue->insert(tmp); }
edges->reset();
node=RI_CAST<GNode *>(queue->get());
if(node==0) break;
node->setColor(GNodeColorBlack); } while(queue->getSize()>0); }
void Graph::BredthFirstPrint(void (*func)(void *data),GNode *node, GNode *node2) { if(node==node2) { func(node->getData()); return; }
if(node2->getPrev()==0) { cout<<"No path from "<<node<<" to "<<node2<<endl; exit(1); }
BredthFirstPrint(func,node,node2->getPrev()); func(node2->getData()); }
void Graph::reset() { GNode *node;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) node->reset();
Nodes->reset(); }
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message
news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it
works fine.
You'd have to ask the programmer why it was done that way. It could be that
one or both static functions were intended to called from somewhere external
that didn't have access to the necessary instance, so the static Instance
was used. Or maybe there's only ever supposed to be one instance. There's a
lot of code and I don't have time to analyse it properly..
It all looks pretty horrible to me. I would want to get rid of 'Instance'
altogether if possible.
DW
"David White" <no@email.provided> wrote in message
news:Ak*****************@nasal.pacific.net.au... "Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it works fine.
You'd have to ask the programmer why it was done that way. It could be that
Ok I will ask myself why I did it this way ;-).... btw I found the issue
later which was
a junk arguement.
one or both static functions were intended to called from somewhere external that didn't have access to the necessary instance, so the static Instance was used. Or maybe there's only ever supposed to be one instance. There's a lot of code and I don't have time to analyse it properly..
It all looks pretty horrible to me. I would want to get rid of 'Instance' altogether if possible.
The reason is if your going to pass a non-default method to the factory
(Graph::init) the compiler barfs unless it is static. And "this" is
avaible from inside a static method. Thus Instance=this then call the
static wrapper and do Instance->real
--Aryeh This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J |
last post by:
I'm wondering why the form created in the code below stays "alive". I would
have thought that because the form variable is local to the method, that
garbage collection would get rid of it. Makes...
|
by: KathyB |
last post by:
Hi,
Using the following in an asp.net procedure. I get the error "The
expression passed to this method should result in a NodeSet".
Dim xDoc As New Document()
The line causing the error is:...
|
by: Marty |
last post by:
Hi,
I have a class that I modified to be static. It is now a public sealed
class and all function are static, no more constructor but a init()
function to do the constructor job.
This class...
|
by: ChrisB |
last post by:
Hello:
An object that is a field in another object has a constructor that requires
a reference to the containing object:
// object fields
ChildObject childObject = new ChildObject(this);
...
|
by: relient |
last post by:
Question: Why can't you access a private inherited field from a base
class in a derived class? I have a *theory* of how this works, of
which, I'm not completely sure of but makes logical sense to...
|
by: dhnriverside |
last post by:
Hi peeps
I'm just following this HOW-TO from MSDN..
http://support.microsoft.com/default.aspx?scid=kb;en-us;306355
But I've got a problem. I've adding the #using System.Diagnostics; line to...
|
by: danny.dion |
last post by:
Hi ! I have a question about JScript :
I have an object class wich dynamically creates a control in the page.
Then it binds an event to that control, pointing on one of its methods
(the...
|
by: psp |
last post by:
I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-to the method being called,
i.e.
class X::methodA()
{
this->methodB();
}
And they...
|
by: DamienS |
last post by:
Hi,
I have a static method in a class and I need to be able to return a
reference to "this". Googling around, I found a heap of discussions of
the pros/cons of "abstract static" etc. It was...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |