Problem with pointers on functions

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Problem with pointers on functions

Post 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
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

You shouldn't need to take the address of the function. Try:

Code: Select all

proc fct = test;
Damn, I need a decent signature!
User avatar
evilo
Posts: 230
Joined: Thu Apr 22, 2004 8:40 pm
Contact:

Post 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 ();
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Post 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
User avatar
evilo
Posts: 230
Joined: Thu Apr 22, 2004 8:40 pm
Contact:

Post by evilo »

strange...

I have it working in some stuff I made for the ps2 !
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Post 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 !!! )
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Post 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 :(
dlanor
Posts: 258
Joined: Thu Oct 28, 2004 6:28 pm
Location: Stockholm, Sweden

Post 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
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post 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)
dlanor
Posts: 258
Joined: Thu Oct 28, 2004 6:28 pm
Location: Stockholm, Sweden

Post 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
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Post 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.
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

You should try your code on a real PS2, as using a PS2 emulator adds a monstrously huge variable into the equation.
BiB
Posts: 36
Joined: Fri Feb 27, 2004 8:13 am
Location: France

Post by BiB »

I'll try when i repair my ps2 :S
Post Reply