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 

[SOLVED] Problem with Pointers!!! Please Help.

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



Joined: 30 Sep 2006
Posts: 65

PostPosted: Thu Dec 06, 2007 9:53 am    Post subject: [SOLVED] Problem with Pointers!!! Please Help. Reply with quote

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
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Thu Dec 06, 2007 10:46 am    Post subject: Reply with quote

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
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Thu Dec 06, 2007 12:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Thu Dec 06, 2007 1:09 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Thu Dec 06, 2007 7:23 pm    Post subject: Reply with quote

Most likely an alignment issue. Memory access must be naturally aligned on the PSP.
Back to top
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Fri Dec 07, 2007 12:19 am    Post subject: Reply with quote

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
View user's profile Send private message
DoctorRockit



Joined: 10 Dec 2006
Posts: 5

PostPosted: Fri Dec 07, 2007 12:46 am    Post subject: Reply with quote

Actually the solution you posted does not deal with alignment, but rather with little-endian to big-endian conversion (or vice versa).

See:
http://en.wikipedia.org/wiki/Data_structure_alignment

versus

http://en.wikipedia.org/wiki/Endianess
Back to top
View user's profile Send private message
terryxq



Joined: 12 Oct 2005
Posts: 16

PostPosted: Fri Dec 07, 2007 1:44 am    Post subject: Reply with quote

DoctorRockit wrote:
Actually the solution you posted does not deal with alignment, but rather with little-endian to big-endian conversion (or vice versa).

See:
http://en.wikipedia.org/wiki/Data_structure_alignment

versus

http://en.wikipedia.org/wiki/Endianess


I don't think so. macro READU32/16 uses for le, psp is le.
Back to top
View user's profile Send private message
DoctorRockit



Joined: 10 Dec 2006
Posts: 5

PostPosted: Fri Dec 07, 2007 3:07 am    Post subject: Reply with quote

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
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