Function bugs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Unexpected

Function bugs

Post by Unexpected »

I wrote this function...
This function must integer number convert to string.
Example:
Input: int a = 123;
Output: char b[3] = "123";

char *str(int number);

char numeral[9] = "123456798";

char *str(int number)
{
   int m, count = 0;
   char *string;
   while(number != 0)
{
   count++;
if (number < 9)
{
   m = number;
number = 0;
}
else
{
   m = number % 10;
    number = (number - m) / 10;
}
      string = new char[count];
   string[count] = numeral[m];
}
   return string;
}



This function doesn't work... Can you help me?
Thanx!
Tom

Re:Function bugs

Post by Tom »

ok, here is a working itoa for you :)

oh, i'm in windows....be back later to give it to you...just a min...

you also need my putch from my pk0.6. download it.
you also also need my start_textmode() in pk0.6
Tom

Re:Function bugs

Post by Tom »

[attachment deleted by admin]
Unexpected

Re:Function bugs

Post by Unexpected »

Ok, thanx!
I change this function... now it looks like this and work fine:

Code: Select all

unsigned char *str(int num)
{
     int k, i = 0;
     unsigned char tmpstr[10], string[10], negative = 0;

     if (num < 0)
     {
          negative = 1;
          num = - num;
     }
   do {
          k = num % 10;
          num = (num - k) / 10;
          tmpstr[i] = numeral[k];
          i++;
     } while (num != 0);
     if (negative == 1)
          tmpstr[i] = '-';
     for (k = 0; k <= i; k++)
          string[k] = tmpstr[i - k];
     return string;
}
P.S - If you see optimizations or bugs, please say me ;)
Tom

Re:Function bugs

Post by Tom »

there is a bug with my convert int to string...
it can't handel numbers over 256.

but probly this won't be a prob.
Tim

Re:Function bugs

Post by Tim »

Yep, you could write programs like this: :)

[tt]Please enter a number. Don't enter numbers higher than 256 or the computer will crash.
>_[/tt]
Tom

Re:Function bugs

Post by Tom »

hahaha. not - my convertInttoString just does things like -113 ;) ;)

I will add hight number support, I think I found out a way.


It will not crash :)
PlayOS

Re:Function bugs

Post by PlayOS »

Hi,

Maybe I am wrong but in this function, the string[10] is created on the stack for that function, when it returns the variable string[10] is destroyed, or at least in a completely working OS it would be. The reason that it would appear to work is because the stack is not erased, esp is just restored, so the string still exists in memory, however in a fully working OS memory references like this will not stay around for long.

Is this right? Anyone.
Unexpected

Re:Function bugs

Post by Unexpected »

Yeah in my function is still bug...
I will try to correct...
Unexpected

Re:Function bugs

Post by Unexpected »

Now is withuoot any bugs :) (I hope ;D )

Code: Select all

void str(int num, char *string)
{
     int k, i = 0;
     char tmpstr[10], negative = 0;

     if (num < 0)
     {
          negative = 1;
          num = - num;
     }
     do {
          k = num % 10;
          num = (num - k) / 10;
          tmpstr[i] = k + 0x30;
          i++;
     } while (num != 0);
     if (negative == 1)
          tmpstr[i] = '-';
     for (k = 0; k < i; k++)
          string[k] = tmpstr[i - k - 1];
}
Post Reply