forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with pointers on functions

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Fri Jun 10, 2005 11:09 pm    Post subject: Problem with pointers on functions Reply with quote

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:

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

typedef void (*proc)(int);

void test(int integer) {
     scr_printf("%d\n",integer);
}

int main() {
    init_scr();
    proc fct = &test;
    fct(2);

    return 0;
}


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
Back to top
View user's profile Send private message
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Fri Jun 10, 2005 11:15 pm    Post subject: Reply with quote

You shouldn't need to take the address of the function. Try:
Code:
proc fct = test;

_________________
Damn, I need a decent signature!
Back to top
View user's profile Send private message
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Fri Jun 10, 2005 11:19 pm    Post subject: Reply with quote

I would personnally do this :

void (*my pointer_fct)(void);

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

my pointer_fct = myfct;

and then

pointer_fct ();
Back to top
View user's profile Send private message Visit poster's website
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Fri Jun 10, 2005 11:58 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Sat Jun 11, 2005 12:06 am    Post subject: Reply with quote

strange...

I have it working in some stuff I made for the ps2 !
Back to top
View user's profile Send private message Visit poster's website
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Sat Jun 11, 2005 12:08 am    Post subject: Reply with quote

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 !!! )
Back to top
View user's profile Send private message
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Sat Jun 11, 2005 12:24 am    Post subject: Reply with quote

Hmmm... it seems to be a "bug" or something strange from the scr_printf function.

When i execute this piece of code :
Code:
int val = 0;

void test(int integer) {
     val = integer;
     //scr_printf("integer = %d\n",integer);
}

int main() {
    init_scr();
    void (*proc)(int) = test;
    scr_printf("Before : val = %d\n",val);
    proc(2);
    scr_printf("After : val = %d\n",val);
   
return 0;
}


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 :(
Back to top
View user's profile Send private message
dlanor



Joined: 28 Oct 2004
Posts: 269
Location: Stockholm, Sweden

PostPosted: Sun Jun 12, 2005 7:17 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Sun Jun 12, 2005 5:43 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
dlanor



Joined: 28 Oct 2004
Posts: 269
Location: Stockholm, Sweden

PostPosted: Mon Jun 13, 2005 1:32 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Mon Jun 13, 2005 5:23 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Mon Jun 13, 2005 10:58 pm    Post subject: Reply with quote

You should try your code on a real PS2, as using a PS2 emulator adds a monstrously huge variable into the equation.
Back to top
View user's profile Send private message
BiB



Joined: 27 Feb 2004
Posts: 36
Location: France

PostPosted: Tue Jun 14, 2005 6:29 pm    Post subject: Reply with quote

I'll try when i repair my ps2 :S
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group