Question of function return value

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
picobsd
Posts: 18
Joined: Thu Apr 19, 2007 7:24 pm

Question of function return value

Post by picobsd »

Hi, I am reading ModuleTurorialv1.pdf, and testing it's code on my psp develop platform.

I am a unix C programmer, and I have never developed on portable device such as PDA or PSP, Now I am confused with a function's C code in this PDF document.

-------------------- code fragement start here ------------
const char*
sv_check_permissions (int client)
{
const char* p;
sceKernelDelayThread (10000);
if (client == 123)
p = "yes";
else
p = "no";
return p;
}
-------------------- code fragement end here ------------

the above is a simple pure export module in that doc, As a C programmer, I know that the pointer p is a AUTO variable, It will be destroied when the function returned, I mean the p will point a Invaild area after the function returned, because p is nether a static pointer nor a heap poniter (use malloc), I worried this may cause the application crash?
why do this is safe in PSP, could someone tell me the reason, thank you!

do I make myself clear?
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

You are not returning the address of P, you're returning what P points to. In this case, you are returning the address of a static string (in the text segment).
picobsd
Posts: 18
Joined: Thu Apr 19, 2007 7:24 pm

Post by picobsd »

jimparis wrote:You are not returning the address of P, you're returning what P points to. In this case, you are returning the address of a static string (in the text segment).
Oh, thank you! It's const. I make a big mistake.
Post Reply