Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Using a function that doesn't exist =\

Reply
Thread Tools

Using a function that doesn't exist =\

 
 
Itay
Guest
Posts: n/a
 
      03-23-2005
While going through the code of the open source project PCSX2
(www.pcsx2.net - PS2 emulator for windows and linux. source code can be
d/led from here: http://tinyurl.com/6242p), I saw a strange (at least
for me...) thing:
in the R5900.h header (hmm..emulating a CPU...), there's a struct which
looks like this:
typedef struct {
int (*Init)();
void (*Reset)();
void (*Step)();
void (*Execute)();
void (*ExecuteBlock)();
void (*ExecuteVU0Block)();
void (*ExecuteVU1Block)();
void (*EnableVU0micro)(int enable);
void (*EnableVU1micro)(int enable);
void (*Clear)(u32 Addr, u32 Size);
void (*ClearVU0)(u32 Addr, u32 Size);
void (*ClearVU1)(u32 Addr, u32 Size);
void (*Shutdown)();
} R5900cpu;

and then

R5900cpu *Cpu;

and somewhere in R5900.c:

Cpu->ExecuteBlock();

ExecuteBlock is not defined anywhere. I did a "search in files" in the
whole project, and the whole project dir...Only references to
ExecuteBlock found..no definition. What's going on here?!

It must be found somehwere, because it's in a function that's - CMIIW -
always called (indirectly) from WinMain ...and it doesn't make sense
that it's accidently there...
Any explanation?

thanks,

Itay
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-25-2005


Itay wrote:
> While going through the code of the open source project PCSX2
> (www.pcsx2.net - PS2 emulator for windows and linux. source code can be
> d/led from here: http://tinyurl.com/6242p), I saw a strange (at least
> for me...) thing:
> in the R5900.h header (hmm..emulating a CPU...), there's a struct which
> looks like this:
> typedef struct {
> int (*Init)();
> void (*Reset)();
> void (*Step)();
> void (*Execute)();
> void (*ExecuteBlock)();
> void (*ExecuteVU0Block)();
> void (*ExecuteVU1Block)();
> void (*EnableVU0micro)(int enable);
> void (*EnableVU1micro)(int enable);
> void (*Clear)(u32 Addr, u32 Size);
> void (*ClearVU0)(u32 Addr, u32 Size);
> void (*ClearVU1)(u32 Addr, u32 Size);
> void (*Shutdown)();
> } R5900cpu;
>
> and then
>
> R5900cpu *Cpu;
>
> and somewhere in R5900.c:
>
> Cpu->ExecuteBlock();
>
> ExecuteBlock is not defined anywhere. I did a "search in files" in the
> whole project, and the whole project dir...Only references to
> ExecuteBlock found..no definition. What's going on here?!
>
> It must be found somehwere, because it's in a function that's - CMIIW -
> always called (indirectly) from WinMain ...and it doesn't make sense
> that it's accidently there...
> Any explanation?


The things in the struct are *pointers* to functions,
not function names. Somewhere you should find something
like

void frammis();
...
Cpu->ExecuteBlock = frammis;

.... and once this has been done, `Cpu->ExecuteBlock()'
behaves as if you had written `frammis()' instead. What
you need to do is find the assignment or initialization,
make note of the name assigned ("frammis"), and then search
for the function with that name.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
ItayP
Guest
Posts: n/a
 
      04-17-2005
Can't find it anywhere!
What's going on here? =\

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-17-2005
ItayP wrote:
>
> Can't find it anywhere!
> What's going on here? =\


/= ?

--
pete
 
Reply With Quote
 
ItayP
Guest
Posts: n/a
 
      04-17-2005
wtf?

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-17-2005
"ItayP" <(E-Mail Removed)> writes:
> Can't find it anywhere!
> What's going on here? =\


Sorry, I have no idea what you're talking about, since you didn't
provide any context (and my news server doesn't have the parent
article).

As we've said here many many times:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers."

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
ItayP
Guest
Posts: n/a
 
      04-17-2005
Ok...Sorry for not knowing about the broken reply link.
I was told that there must be somewhere something similar to that:
void frammis();
...
Cpu->ExecuteBlock = frammis;
(shouldn't it be &frammis?)
anyway, I couldn't find anything similar..


Itay wrote:
> While going through the code of the open source project PCSX2
> (www.pcsx2.net - PS2 emulator for windows and linux. source code can

be
> d/led from here: http://tinyurl.com/6242p), I saw a strange (at least


> for me...) thing:
> in the R5900.h header (hmm..emulating a CPU...), there's a struct

which
> looks like this:
> typedef struct {
> int (*Init)();
> void (*Reset)();
> void (*Step)();
> void (*Execute)();
> void (*ExecuteBlock)();
> void (*ExecuteVU0Block)();
> void (*ExecuteVU1Block)();
> void (*EnableVU0micro)(int enable);
> void (*EnableVU1micro)(int enable);
> void (*Clear)(u32 Addr, u32 Size);
> void (*ClearVU0)(u32 Addr, u32 Size);
> void (*ClearVU1)(u32 Addr, u32 Size);
> void (*Shutdown)();
> } R5900cpu;
>
> and then
>
> R5900cpu *Cpu;
>
> and somewhere in R5900.c:
>
> Cpu->ExecuteBlock();
>
> ExecuteBlock is not defined anywhere. I did a "search in files" in

the
> whole project, and the whole project dir...Only references to
> ExecuteBlock found..no definition. What's going on here?!
>
> It must be found somehwere, because it's in a function that's - CMIIW

-
> always called (indirectly) from WinMain ...and it doesn't make sense
> that it's accidently there...
> Any explanation?
>
> thanks,
>
> Itay


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-17-2005
Please don't top-post. Your reply should be after, or interspersed
with, any quoted text, which should be trimmed to what's necessary for
context.

"ItayP" <(E-Mail Removed)> writes:
> Itay wrote:

[...]
>> and somewhere in R5900.c:
>>
>> Cpu->ExecuteBlock();
>>
>> ExecuteBlock is not defined anywhere. I did a "search in files" in
>> the whole project, and the whole project dir...Only references to
>> ExecuteBlock found..no definition. What's going on here?!


[...]

> Ok...Sorry for not knowing about the broken reply link.
> I was told that there must be somewhere something similar to that:
> void frammis();
> ...
> Cpu->ExecuteBlock = frammis;
> (shouldn't it be &frammis?)
> anyway, I couldn't find anything similar..


Right. Cpu is a pointer to an object of some struct type. One of the
members of the struct, a pointer-to-function, is named ExecuteBlock.
So ExecuteBlock is the name of the struct member, not the name of a
function.

If frammis is a function compatible with the declaration of
ExecuteBlock, then assigning

Cpu->ExecuteBlock = frammis;

allows you to call frammis indirectly as:

Cpu->ExecuteBlock();

The assignment is the most straightforward way to set the value of
Cpu->ExecuteBlock, but it's not the only way. I would exect a global
search for ExecuteBlock to find the assignment unless something ugly
is being done (like setting it in an assembly language routine,
perhaps?).

The assignment does not require the "&" operator. A reference to a
function name is implicitly converted to a pointer to the function
(unless it's the operand of a sizeof or "&" operator; in the case of
sizeof the result is an illegal expression). So you can use &frammis
if you like, but just frammis is ok.

Note that a function call is one of the contexts in which a function
name is implicitly converted to a pointer. A function call is
effectively an operator whose first operad is a value of an
appropriate pointer-to-function type, so even an ordinary function
call:
myfunc(1, 2);
is really an indirect call through a pointer. (Of course an
implementation is free to optimize this to a more direct form in the
generated code as long as the result is the same.)

Here's a (rather silly) illustration:

#include <stdio.h>
void myfunc(int n)
{
printf("myfunc(%d)\n", n);
}

int main(void)
{
myfunc(1);
(&myfunc)(2);
(*&myfunc)(3);
(*myfunc)(4);
(**myfunc)(5);
(***myfunc)(6);
(*&*&*myfunc)(7);
return 0;
}

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      04-17-2005
On 17 Apr 2005 12:24:24 -0700, in comp.lang.c , "ItayP"
<(E-Mail Removed)> wrote:

>wtf?


Pete meant "wtf r u babln abt?"

Or alternatively, post a meaningful question, don't post gibberish


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-17-2005
Itay wrote:
> typedef struct {
> int (*Init)();
> void (*Reset)();
> void (*Step)();
> void (*Execute)();
> void (*ExecuteBlock)();
> void (*ExecuteVU0Block)();
> void (*ExecuteVU1Block)();
> void (*EnableVU0micro)(int enable);
> void (*EnableVU1micro)(int enable);
> void (*Clear)(u32 Addr, u32 Size);
> void (*ClearVU0)(u32 Addr, u32 Size);
> void (*ClearVU1)(u32 Addr, u32 Size);
> void (*Shutdown)();
> } R5900cpu;
>
> and then
>
> R5900cpu *Cpu;
>
> and somewhere in R5900.c:
>
> Cpu->ExecuteBlock();
>
> ExecuteBlock is not defined anywhere. I did a "search in files"
> in the whole project, and the whole project dir...
> Only references to ExecuteBlock found..no definition.


Perhaps it is initialized rather than assigned. If you
search for 'Cpu' you should see where it's assigned to
something (obviously you can't write Cpu-> ..... without
first setting Cpu to point to something).

For example the code might be something like:

R5900cpu foo =
{ &foo_Init, &foo_Reset, &foo_Stop, ............ };

and then

Cpu = &foo;
Cpu->Init();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control) LT C++ 7 07-25-2004 07:08 PM
Can the Favourites Sidebar (IE, Opera, etc.) be addressed selectively (exist or non-exist)? Markus Mohr Javascript 7 11-28-2003 12:20 AM
In Schema, how to say "If one element exist, another element must exist"? Y.S. XML 3 09-17-2003 02:51 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments