ROBOTC.net forums
http://robotc.net/forums/

char array maximum size???
http://robotc.net/forums/viewtopic.php?f=63&t=6326
Page 1 of 1

Author:  MHTS [ Fri Jul 19, 2013 8:56 pm ]
Post subject:  char array maximum size???

I have defined an initialized char array of 430 but apparently RobotC decided to store only about 220 characters. Is there really a maximum limit of 220 character in a char array? If that's true, the compiler should really give me a warning instead of keeping quiet about it. Without warning, I was expecting the code to work but instead I had to spend time debugging it and finally realize the string was terminated at 220 characters.

Author:  mightor [ Sat Jul 20, 2013 1:13 am ]
Post subject:  Re: char array maximum size???

Do you have a simple piece of code that demonstrates this?

= Xander

Author:  MHTS [ Sat Jul 20, 2013 4:22 am ]
Post subject:  Re: char array maximum size???

Well, I don't have time to simplify the program. Here is the original code I was debugging and found the issue. The code is for a summer class I am teaching the kids. This eventually will be used to teach them how to deal with multi-tasking. For example, doing all the robot control while having music playing in the background.
BTW, when debugging this, run the program. The program is supposed to play the song repeatedly (i.e. the song will loop back to the beginning after playing the last note in the array). When the song started to repeat at a point that sounded abrupt, stop the program immediately and examine the debug stream output. The program printed the remaining score after playing every note. As you can see, the last note in the array was an "F5.3" which is not really the end. "F5.3" is in the middle of the 10th line of the array initialization. I counted the number of characters from the beginning to "F5.3" and it's about 220 characters.
Code:
#define NUM_NOTES               89
#define DURATION_SCALE          8

string g_NoteNames[NUM_NOTES] =
{
    "R",   "A0",  "A#0", "B0",  "C1",  "C#1", "D1",  "D#1", "E1",  "F1",
    "F#1", "G1",  "G#1", "A1",  "A#1", "B1",  "C2",  "C#2", "D2",  "D#2",
    "E2",  "F2",  "F#2", "G2",  "G#2", "A2",  "A#2", "B2",  "C3",  "C#3",
    "D3",  "D#3", "E3",  "F3",  "F#3", "G3",  "G#3", "A3",  "A#3", "B3",
    "C4",  "C#4", "D4",  "D#4", "E4",  "F4",  "F#4", "G4",  "G#4", "A4",
    "A#4", "B4",  "C5",  "C#5", "D5",  "D#5", "E5",  "F5",  "F#5", "G5",
    "G#5", "A5",  "A#5", "B5",  "C6",  "C#6", "D6",  "D#6", "E6",  "F6",
    "F#6", "G6",  "G#6", "A6",  "A#6", "B6",  "C7",  "C#7", "D7",  "D#7",
    "E7",  "F7",  "F#7", "G7",  "G#7", "A7",  "A#7", "B7",  "C8"
};

int g_NoteFreq[NUM_NOTES] =
{
    0,    28,   29,   31,   33,   35,   37,   39,   41,   44,
    46,   49,   52,   55,   58,   62,   65,   69,   73,   78,
    82,   87,   92,   98,   104,  110,  117,  123,  131,  139,
    147,  156,  165,  175,  185,  196,  208,  220,  233,  247,
    262,  277,  294,  311,  330,  349,  370,  392,  415,  440,
    466,  494,  523,  554,  587,  622,  659,  698,  740,  784,
    831,  880,  932,  988,  1047, 1109, 1175, 1245, 1319, 1397,
    1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489,
    2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186
};

char g_StarWars[436] =
    "G4.2,G4.2,G4.2,"
    "C5.12,G5.12,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3,"
    "C5.12,G5.12,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "C5.2,D5.2,E5.2,D5.6,B4.6,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "G5.6,D5.12,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "C5.2,D5.2,E5.2,D5.6,B4.6,G5.3,G5.3,"
    "C6.3,A#5.3,G#5.3,G5.3,F5.3,D#5.3,D5.3,C5.3,"
    "G5.18,";

int FindNoteFreq(char *pszNote, int len)
{
    int freq = -1;

    for (int i = 0; i < NUM_NOTES; i++)
    {
        if (strncmp(pszNote, g_NoteNames[i], len) == 0)
        {
            freq = g_NoteFreq[i];
            break;
        }
    }

    return freq;
}   //FindNoteFreq

int ParseNote(char *pszNote, int *freq, int *duration)
{
    int n;

    n = StringFind(pszNote, ".");
    if (n <= 0)
    {
        n = -1;
    }
    else
    {
        long data;

        *freq = FindNoteFreq(pszNote, n);
        writeDebugStreamLine("%p: F=%d, %s", pszNote, *freq, pszNote);
        pszNote += n + 1;
        if (*freq == -1)
        {
            nxtDisplayTextLine(2, "Note not found!");
            n = -1;
        }
        else if (!sscanf(pszNote, "%d", &data))
        {
            nxtDisplayTextLine(2, "Invalid duration!");
            n = -1;
        }
        else
        {
            int n1;
            *duration = data;
            n1 = StringFind(pszNote, ",");
            if (n1 > 0)
            {
                n += n1 + 2;
            }
            else
            {
                nxtDisplayTextLine(2, "No next note!");
                n = -1;
            }
        }
    }

    return n;
}   //ParseNote

task main()
{
    char *pszSong = g_StarWars;

    clearDebugStream();
    while (true)
    {
        if (!bSoundActive)
        {
           int freq, duration;
           int n = ParseNote(pszSong, &freq, &duration);

           if (n == -1)
           {
               nxtDisplayTextLine(0, "Parse Error!");
               writeDebugStreamLine(pszSong);
               break;
           }
           else
           {
               nxtDisplayTextLine(1, "F=%04d,Dur=%d", freq, duration);
               PlayTone(freq, duration*DURATION_SCALE);
               pszSong += n;
               if (*pszSong == '\0')
               {
                   pszSong = &g_StarWars[0];
               }
           }
       }

        EndTimeSlice();
   }
}

Author:  MHTS [ Sat Jul 20, 2013 5:11 am ]
Post subject:  Re: char array maximum size???

Alright, just thought of a simple way to repro it.
Code:
char g_StarWars[436] =
    "G4.2,G4.2,G4.2,"
    "C5.12,G5.12,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3,"
    "C5.12,G5.12,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,D5.2,C6.12,G5.6,"
    "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "C5.2,D5.2,E5.2,D5.6,B4.6,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "G5.6,D5.12,G4.3,G4.3,"
    "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3,"
    "C5.2,D5.2,E5.2,D5.6,B4.6,G5.3,G5.3,"
    "C6.3,A#5.3,G#5.3,G5.3,F5.3,D#5.3,D5.3,C5.3,"
    "G5.18,";

task main()
{
    char *psz = &g_StarWars[200];
    clearDebugStream();
    writeDebugStreamLine("%p: %s", psz, psz);
    writeDebugStreamLine("Len=%d", strlen(g_StarWars));
}

Author:  mightor [ Sat Jul 20, 2013 8:27 am ]
Post subject:  Re: char array maximum size???

I can confirm this bug in the latest version of ROBOTC.

= Xander

Author:  tfriez [ Mon Jul 22, 2013 3:53 pm ]
Post subject:  Re: char array maximum size???

We're currently knee deep in 4.0/IQ/EV3 development at the moment, but we're adding these to our internal bugs list.

If you find anything else, let us know! Thanks as always!

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/