gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > sprintf() works on emulator but not hardware?

#137292 - Jeremysr - Thu Aug 09, 2007 11:24 pm

I just got my M3 DS Simply but when I ran my program (which worked perfectly fine on Desmume) it froze while starting. I found out that it froze when I called sprintf() to change numbers into strings. Anyone know why this is happening or how else I should convert ints to strings?

#137304 - tepples - Fri Aug 10, 2007 12:36 am

So you only care about ints, not floats. In this case, you can use siprintf, which is smaller than sprintf but does not handle floats. Does it still crash then? And are you sure that you are allocating a buffer and not overflowing it?

Without seeing your code, I can't make more specific suggestions.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#137308 - Jeremysr - Fri Aug 10, 2007 1:27 am

siprintf() didn't work and I'm not sure what you meant by "allocating a buffer and not overflowing it".

But here's my code (with redundant parts taken out):

Code:
void updateProgram(int *program, int start, int selected) {
   int i = 0, count = 0, y = 0, x = 0;
   char num_str[20];
   char clear_str[6] = {31,31,31,31,31,'\0'};
   
   for (i = start; i < start+6; i++) {
      count++;
      siprintf(num_str, "%s%d", 10, i);
      printString(clear_str, 20, count * 9 + 10, SCREEN_BOTTOM);
      printString(num_str, 20, count * 9 + 10, SCREEN_BOTTOM);
   }
}

#137310 - kusma - Fri Aug 10, 2007 2:02 am

Jeremysr wrote:
Code:
siprintf(num_str, "%s%d", 10, i);

Eh? treating the number 10 as a pointer to a string, and printing it? Sure that's a good idea? It will most likely give you some non-0 data, perhaps more than 20 of them - giving you a nice little stack-corruption. So I guess that's your issue :)

What are you really trying to do with that line?

#137311 - Lordus - Fri Aug 10, 2007 2:04 am

I am not exactly sure what you want to do there, but it cannot work like this.

with "%s%d", siprintf is expecting another string as the first argument, in other words a memory address pointing to the first character of a terminated string.

Do you want to write the number 10 there? Then you can do

Code:

// stupid, but just to show how it would work
siprintf(num_str, "%s%d", "10", i);
// or better
siprintf(num_str, "10%d", i);


But maybe you should better describe, what you really want to do.

Edit: ah too slow ;)

#137313 - Jeremysr - Fri Aug 10, 2007 2:12 am

I'm kind of new to C and just searched Google how to convert an int to a string. I just copied that code without understanding the function and as I said it worked great on Desmume. I thought the 10 meant base-10 actually. :P

All I want to do is take an integer and store it in a string so I can print it with my printString() function.

#137333 - masscat - Fri Aug 10, 2007 9:47 am

To do that simply use:
Code:
siprintf(num_str, "%d", i);

The '%d' conversion specifier converts the integer i into its base 10 string representation.

The reason that the code works under Desmume but not on hardware is that Desmume does not emulate the ARM9 protection unit (yet). On hardware the protection unit is configured, by default, to disallow access to memory from 0x00000000 to 0x01000000.

#137373 - Jeremysr - Fri Aug 10, 2007 11:54 pm

masscat: I tried it but ran into a new problem - my M3 Simply wouldn't load the .nds file. It says "Loading..." but the progress bar is real slow and after a couple minutes it just stops at about 40%. But when I comment out the siprintf() lines, it loads right away and works great (except for displaying the numbers of course.) I also noticed that the .nds file jumps from about 99KB to about 85KB when I commented out those lines.

I'll probably just (try to) write my own function to disply numbers...

#137377 - DragonMinded - Sat Aug 11, 2007 12:38 am

I would recommend against siprintf. In my trying to use it, all it did was increase the binary a few kb and output wrong information.
_________________
Enter the mind of the dragon.

http://dragonminded.blogspot.com

Seriously guys, how hard is it to simply TRY something yourself?

#137380 - wintermute - Sat Aug 11, 2007 2:09 am

DragonMinded wrote:
I would recommend against siprintf. In my trying to use it, all it did was increase the binary a few kb and output wrong information.


Not sure why you feel qualified to judge considering you're still using a toolchain that is spectacularly out of date.

In almost every case where I've had reports of library functions failing it has been a problem with the code. I'm not saying that the latest toolchain is 100% bug free but user error is still the number one cause of problems.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#137391 - DragonMinded - Sat Aug 11, 2007 6:12 am

Well you were the one that is currently updating the code to r20, and may I remind you that it's on infinite hiatus until r21 is out, so there's not much I can do is there?
_________________
Enter the mind of the dragon.

http://dragonminded.blogspot.com

Seriously guys, how hard is it to simply TRY something yourself?

#137393 - wintermute - Sat Aug 11, 2007 6:38 am

DragonMinded wrote:
Well you were the one that is currently updating the code to r20, and may I remind you that it's on infinite hiatus until r21 is out, so there's not much I can do is there?


I offered to make the attempt to get DSO working with a more recent toolchain, I have it building with r21 but unfortunately I need to concentrate on other things right now.

The point I was trying to make here is that your experience of siprintf has no relevance and recommending that people not use it is simply FUD. Given that the code worked on an emulator and not on hardware would imply that the problem is related to memory use rather than a problem with the library code itself.

Masscat nailed the memory problem. The M3 problem is odd but probably something related to patching rather than any fault with the toolchain.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#137397 - phonymike - Sat Aug 11, 2007 7:17 am

call me conservative, but I always reinvent the wheel and write my own stuff :)

Code:

//should be able to do this, it converts a u8 into ascii, so like 0xFF would be "255\0" or 24 would be "024\0" I believe
//printString(hex2dec(number), 20, count * 9 + 10, SCREEN_BOTTOM);
//quick stuff for the gba actually

char dec[3];     //make sure this is global

char* hex2dec(u8 hex){
 u8 tmp=0;
 dec[0] = 0;
 dec[1] = 0;
 tmp = hex;

 while(tmp > 99){
  dec[0]++;             //fill the hundreds
  tmp -= 100;
 }

 while(tmp > 9){
  dec[1]++;           //fill the tens
  tmp -= 10;
 }

 dec[0] += 48;            //hundreds to ASCII
 dec[1] += 48;            //tens to ASCII
 dec[2] = tmp + 48;       //sets the ones into ASCII
 dec[3] = 0;              //sets the '\0' so no string bugs crop up

 return dec;
}