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 

an opinion about this code [crash!]

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



Joined: 17 Mar 2006
Posts: 16
Location: México

PostPosted: Thu Mar 23, 2006 5:06 pm    Post subject: an opinion about this code [crash!] Reply with quote

well ... i was trying and trying and trying and well ..

Code:

int createConsole(char* origin){
    int result=0;
    char* realpath='\0';
    sprintf(realpath,origin); <------------------------------Crash Here!
    strcat(realpath,"/img/console_bg.png"); <----------------and here
    printf("path: %s", realpath);
    //printf("Helloo!!!!!!!!");
    result=readPNG(realpath);
    //printf("result=%i",result);
    return(result);
}


compiles and links OK, but when i run it crash in the sprintf but is the same with strcat , strcpy, snprintf ( i'm using newlib ).

i want to know your opinion about this ....

i know a solution for this !!! but ... it's odd ...
_________________
PSP FW v2.6 owner
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Thu Mar 23, 2006 6:39 pm    Post subject: Reply with quote

You can't initialize a string like this

char* realpath='\0';

What this does, is just initialize a pointer to a char struct and by setting it to '\0' you make it null pointer. Accessing this will no matter what crash your program.
Use this instead:

char realpath[256];
realpath = '\0';
Back to top
View user's profile Send private message Visit poster's website
hubevolution



Joined: 17 Mar 2004
Posts: 32

PostPosted: Thu Mar 23, 2006 6:58 pm    Post subject: Reply with quote

in general ... you need to allocate enough memory for the realpath variable to contain the maximum lenght possible expected for origin variable.

and realpath must be defined like :

Code:
 char realpath[MAX_ORIGIN_LENGTH];


in this case a path can be 255 max as raphael correctly stated :)
Back to top
View user's profile Send private message
__count



Joined: 23 Mar 2006
Posts: 22

PostPosted: Thu Mar 23, 2006 8:46 pm    Post subject: Reply with quote

Raphael wrote:

char realpath[256];
realpath = '\0';


That won't compile.
Either do

*realpath = '\0';

or

realpath[0] = '\0';
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Fri Mar 24, 2006 2:13 am    Post subject: Reply with quote

__count wrote:
Raphael wrote:

char realpath[256];
realpath = '\0';


That won't compile.
Either do

*realpath = '\0';

or

realpath[0] = '\0';


Jup, right, forgot that [0] in a hurry :) thanks
Back to top
View user's profile Send private message Visit poster's website
R2k



Joined: 17 Mar 2006
Posts: 16
Location: México

PostPosted: Sat Mar 25, 2006 5:34 am    Post subject: Reply with quote

tnx every1. i solved my prob. hehehehe
_________________
PSP FW v2.6 owner
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 -> PSP 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