View unanswered posts | View active topics It is currently Fri Apr 18, 2014 4:09 am






Reply to topic  [ 6 posts ] 
char array maximum size??? 
Author Message
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post 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.


Fri Jul 19, 2013 8:56 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: char array maximum size???
Do you have a simple piece of code that demonstrates this?

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jul 20, 2013 1:13 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post 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();
   }
}


Sat Jul 20, 2013 4:22 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post 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));
}


Last edited by MHTS on Sat Jul 20, 2013 2:56 pm, edited 1 time in total.



Sat Jul 20, 2013 5:11 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: char array maximum size???
I can confirm this bug in the latest version of ROBOTC.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jul 20, 2013 8:27 am
Profile WWW
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 591
Post 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!

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Jul 22, 2013 3:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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 post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.