Page 1 of 1

Problem with pointers on functions

Posted: Fri Jun 10, 2005 11:09 pm
by BiB
Hi,

I want to use pointer variables who points to functions.

I did this piece of code, compile well, but didn't run it correctly :s

Code: Select all

#include <tamtypes.h>
#include <debug.h>

typedef void &#40;*proc&#41;&#40;int&#41;;

void test&#40;int integer&#41; &#123;
     scr_printf&#40;"%d\n",integer&#41;;
&#125;

int main&#40;&#41; &#123;
    init_scr&#40;&#41;;
    proc fct = &test;
    fct&#40;2&#41;;

    return 0;
&#125;
I expected the value 2 displayed but a strange number (1047008) appared in loop on the screen.

I don't remember exactly but i did such a program in C for linux and this kind of program worked.

Any idea ???

Thx

Posted: Fri Jun 10, 2005 11:15 pm
by cheriff
You shouldn't need to take the address of the function. Try:

Code: Select all

proc fct = test;

Posted: Fri Jun 10, 2005 11:19 pm
by evilo
I would personnally do this :

void (*my pointer_fct)(void);

void myfct()
{
my fancy code...
}

my pointer_fct = myfct;

and then

pointer_fct ();

Posted: Fri Jun 10, 2005 11:58 pm
by BiB
Same result when i don't take the address of the function.

evilo : i also tried your solution but the result is the same as above

Posted: Sat Jun 11, 2005 12:06 am
by evilo
strange...

I have it working in some stuff I made for the ps2 !

Posted: Sat Jun 11, 2005 12:08 am
by BiB
i didn"t say that i tried my compiled elf on PCSX, not directly on the PS2 (which is .... how to say that.... quite dead for the moment !!! )

Posted: Sat Jun 11, 2005 12:24 am
by BiB
Hmmm... it seems to be a "bug" or something strange from the scr_printf function.

When i execute this piece of code :

Code: Select all

int val = 0;

void test&#40;int integer&#41; &#123;
     val = integer;
     //scr_printf&#40;"integer = %d\n",integer&#41;;
&#125;

int main&#40;&#41; &#123;
    init_scr&#40;&#41;;
    void &#40;*proc&#41;&#40;int&#41; = test;
    scr_printf&#40;"Before &#58; val = %d\n",val&#41;;
    proc&#40;2&#41;;
    scr_printf&#40;"After &#58; val = %d\n",val&#41;;
    
return 0;
&#125;
The result is correct :

Before : val = 0
After : val = 2

But when I replace my test function code by 'scr_printf("%d\n",integer), a wrong result appared in loop.

So i can execute correctly my prog but can't debug it :(

Posted: Sun Jun 12, 2005 7:17 am
by dlanor
BiB wrote:Hmmm... it seems to be a "bug" or something strange from the scr_printf function.
Strange perhaps, but no bug. The problem is that your pointer doesn't match the function type.

You first declared the pointer as: void (*proc)(int) = test;
Which I presume you changed to: void (*proc)(int) = scr_printf;
(As that matches your earliest example.)

That would be fine if scr_printf had a matching prototype, but according to debug.h that is declared as:
void scr_printf(const char *, ...) __attribute__((format(printf,1,2)));

As you can see the number of arguments is variable (depends on the string argument), and the main argument is not an 'int' but a 'char *'. In fact you should have received a warning about bad type match when you compiled this, but perhaps you had disabled warnings...

One obvious solution to this problem is to make an intermediate function which matches the 'void (*proc)(int)' type, and call scr_printf directly from that function. This intermediate function can then be used for pointers as you intended.

Best regards: dlanor

Posted: Sun Jun 12, 2005 5:43 pm
by urchin
reading BiB's last post, I understood it to mean that when the scr_print line is uncommented in the test function the result is different (the line is commented out in the example given)

Posted: Mon Jun 13, 2005 1:32 am
by dlanor
urchin wrote:reading BiB's last post, I understood it to mean that when the scr_print line is uncommented in the test function the result is different (the line is commented out in the example given)
You may be right, but I assumed that he'd tried to pass the address of scr_printf directly to a pointer, since that is one of the things that could result in 'strange numbers' being printed, though they were never passed as arguments.

When I do what you say, it works without flaw, so he must have done something different (unless he has a screwed-up ps2dev setup). But of course, he mentioned that he's testing on an emulator rather than a real PS2, so that could also be messing with his results.

Best regards: dlanor

Posted: Mon Jun 13, 2005 5:23 pm
by BiB
You're right urchin. I never tried to pass the adress of scr_printf directly to a pointer.

I pass the adress of a function which contains a scr_printf instruction inside. That's why i find this behaviour very strange.

Posted: Mon Jun 13, 2005 10:58 pm
by urchin
You should try your code on a real PS2, as using a PS2 emulator adds a monstrously huge variable into the equation.

Posted: Tue Jun 14, 2005 6:29 pm
by BiB
I'll try when i repair my ps2 :S