| View previous topic :: View next topic |
| Author |
Message |
R2k
Joined: 17 Mar 2006 Posts: 16 Location: México
|
Posted: Thu Mar 23, 2006 5:06 pm Post subject: an opinion about this code [crash!] |
|
|
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 |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Thu Mar 23, 2006 6:39 pm Post subject: |
|
|
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 |
|
 |
hubevolution
Joined: 17 Mar 2004 Posts: 32
|
Posted: Thu Mar 23, 2006 6:58 pm Post subject: |
|
|
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 |
|
 |
__count
Joined: 23 Mar 2006 Posts: 22
|
Posted: Thu Mar 23, 2006 8:46 pm Post subject: |
|
|
| Raphael wrote: |
char realpath[256];
realpath = '\0'; |
That won't compile.
Either do
*realpath = '\0';
or
realpath[0] = '\0'; |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Fri Mar 24, 2006 2:13 am Post subject: |
|
|
| __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 |
|
 |
R2k
Joined: 17 Mar 2006 Posts: 16 Location: México
|
Posted: Sat Mar 25, 2006 5:34 am Post subject: |
|
|
tnx every1. i solved my prob. hehehehe _________________ PSP FW v2.6 owner |
|
| Back to top |
|
 |
|