Error in debugging code

Programming, for all ages and all languages.
Post Reply
pranavappu007
Member
Member
Posts: 91
Joined: Mon Apr 20, 2020 11:02 am

Error in debugging code

Post by pranavappu007 »

IN the middle of implementing a low level BASIC like "Thing"(not sure what to call it, for now I call it executer), when I encountered this error. I was trying to implement negative number support in the code. In the "source code" it executes(not the source of the executer itself) negative numbers are simply number prefixed by a negative sign. My "executer" should convert this into actual negative numbers and store in arrays. There was some problem with that, so I wrote a bunch of debug-print code to check the issue.
In this code I am just printing an integer array to check if it contains the value it should contain..(array should not contain any negative value at this point)

Code: Select all

    int test=0;char val[5]={};
    int l=0;
    for(int k=0;exp[k]!=0;k++)
    {
        test=exp[k];                                             //taking in value
        int_to_ascii(test, val);                              //converting to ASCII  
        print(val,2*l,0xd);                                   //my print function-takes a pointer, an offset, and a color value
        for(int m=0;val[m]!='\0';m++)             //clearing out val for next iteration
            val[m]='\0';
        l=k*0xA0;                                           //next line
    }
I tried to debug using this code, tried changing lot of stuff, but nothing. Soon I realized that I already fixed the main code but the debug print was actually buggy! It doesn't print exp[0], no matter what I try.For example, when I expect the output to be:
  • 20480
    20530
    5
but it always do
20530 ;omits the first value


5
If I try to force some other value into exp[0], then
20530P\. ;invalid values, there is no way this can happen


5

This is my int_to_ascii code:

Code: Select all

    if(src==0)
    {
        stack[i++]='\0';                      //pushing NULL and 0 to stack
        stack[i++]='0';
    }
    else
    {
        while(src!=0)                       //converting last digit and pushing to stack
        {
            stack[i]=(0x30+src%10);
            src/=10;
            i++;
        }
    }
    i--;
    len=i;
    while(i>=0)
    {
        dest[len-i]=stack[i];                     //popping and placing from left to right
        i--;                                             //inorder not to get reversed.
    }
I don't see a way from these code snippets that it should omit exp[0] or effect exp[1] when value of exp[0] is changed, when actually exp contains all desired values and my main code works. As it's just a debug-prints it's not super important, but still I would like to know the cause of this behavior.
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: Error in debugging code

Post by Octocontrabass »

Code: Select all

char val[5]={};
If the numbers you're printing have five digits, where is your null terminator?

Code: Select all

l=k*0xA0; //next line
That sets it to the current line, not the next line. So if you print entry 0, then you move the cursor to line 0... where will entry 1 be printed?
pranavappu007
Member
Member
Posts: 91
Joined: Mon Apr 20, 2020 11:02 am

Re: Error in debugging code

Post by pranavappu007 »

Octocontrabass wrote: That sets it to the current line, not the next line. So if you print entry 0, then you move the cursor to line 0... where will entry 1 be printed?
hey that makes sense! I think the second entry overwrites the first entry, has to check that next day... I mainly use while loops and in that I increment before setting values... maybe habit got me
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
Post Reply