| View previous topic :: View next topic |
| Author |
Message |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Thu Dec 06, 2007 9:53 am Post subject: [SOLVED] Problem with Pointers!!! Please Help. |
|
|
Hi i'm having some problem trying to port a windows app to the PSP.
Doing some debugging I have found the problem, and it is something like this:
I'm reading the data and size of a file with this func:
| Code: |
char *getFileData(char *filename, long *size)
{
FILE *f;
char *data;
size_t readbytes;
f = fopen(filename, "rb");
if (f == NULL) {
return NULL;
}
fseek(f, 0L, SEEK_END);
(*size) = ftell(f);
rewind(f);
data = new char [*size];
readbytes = fread(data, *size, sizeof(char), f);
fclose(f);
return(data);
} |
Now I pass that data and size to this function:
| Code: |
int load_xm( const unsigned char *data, unsigned int size )
{
...
}
|
And there it reads the data doing a lot of casting:
| Code: |
unsigned int dwHdrSize = *((unsigned long *)(data + 60));
unsighed short rows =*((unsigned short *)(data+336)); |
Now this is taken from the libmodplug code, i'm using Visual Studio 2005 and was able to compile and run the app fine in windows, but the PSP crashes when doing some of the castings from above. Can anyone can help?
Last edited by theHobbit on Fri Dec 07, 2007 12:13 am; edited 1 time in total |
|
| Back to top |
|
 |
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Thu Dec 06, 2007 10:46 am Post subject: |
|
|
Are you sure that you have at least 336 char in data ? ( as you are accessing data+336 ...).
If it's not the case, sometime it can give you something coeherent sometines not ...
You can check as well readbytes _________________ --pspZorba--
NO to K1.5 ! |
|
| Back to top |
|
 |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Thu Dec 06, 2007 12:06 pm Post subject: |
|
|
| yep, the file size is about 500 kb. What bothers me is that it works fine in windows. I think the problem is with the type casting. thanks for the reply! |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Thu Dec 06, 2007 1:09 pm Post subject: |
|
|
The code you have posted looks correct, so you need to post more. The casting is fine.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Thu Dec 06, 2007 7:23 pm Post subject: |
|
|
| Most likely an alignment issue. Memory access must be naturally aligned on the PSP. |
|
| Back to top |
|
 |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Fri Dec 07, 2007 12:19 am Post subject: |
|
|
Thanks!, yep it was a memory alignment issue. I didn't know a lot about it but doing some research I found a some code to fix it.
Now i replaced all the pointer casting with this:
| Code: |
#define READU16(X) ((((unsigned short)((X)[0]))<<0) | \
(((unsigned short)((X)[1]))<<8) )
#define READU32(X) ((((unsigned int)((X)[0]))<<0) | \
(((unsigned int)((X)[1]))<<8) | \
(((unsigned int)((X)[2]))<<16) | \
(((unsigned int)((X)[3]))<<24))
unsigned int dwHdrSize = READU32(data + 60);
unsighed short rows = READU6(data+336);
|
And now it's working fine. Thanks everyone. |
|
| Back to top |
|
 |
DoctorRockit
Joined: 10 Dec 2006 Posts: 5
|
|
| Back to top |
|
 |
terryxq
Joined: 12 Oct 2005 Posts: 16
|
Posted: Fri Dec 07, 2007 1:44 am Post subject: |
|
|
I don't think so. macro READU32/16 uses for le, psp is le. |
|
| Back to top |
|
 |
DoctorRockit
Joined: 10 Dec 2006 Posts: 5
|
Posted: Fri Dec 07, 2007 3:07 am Post subject: |
|
|
| terryxq wrote: | | DoctorRockit wrote: | Actually the solution you posted does not deal with alignment, but rather with little-endian to big-endian conversion (or vice versa).
|
I don't think so. macro READU32/16 uses for le, psp is le. |
Pardon me, you're right. The byte-wise access solves the alignment problem. |
|
| Back to top |
|
 |
|