View unanswered posts | View active topics It is currently Thu Oct 23, 2014 2:37 pm






Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Slightly Big Arrays == RobotC Not Happy? 
Author Message
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Slightly Big Arrays == RobotC Not Happy?
Hey Guys!

I made this code in ROBOTC 3.05 for Cortex & PIC:
Code:
task main()
{
  int ArrayX[50][100];
}


If you set the Compiler Target to PC-Based Emulator and download it that code, it behaves normally. The array initializes to all 0's even before you start the program.
However, when you download to the Physical Robot (Cortex), then open up the Global Variables panel (under Robot, Debugger Windows) and look at the values for the last half of the array, they are filled with gobbledygook (even before you Start the program)! I believe that the array indices are actually pointing to values outside of the array, and so create a dangerous issue if they are accessed! (On the Cortex, I also ran my VEX Team code (which is so far secret, and I can't post here :-( ) that had a similarly sized array, and the strings that held the values for the LCD menu system were getting corrupted (the problem went away when I shrank the array size); when I ran the test on the PC Emulator, it worked just fine, even with the big array)

So the code works fine on the PC-based Emulator, but fails on the actual Cortex

So, is this an issue with ROBOTC 3.05? Arrays should be able to be 20 kilobytes, right? Is there a limit, and if so, what is it?

Here are some screenshots of the errors (the first two are from the ArrayTest; I had to leave from the VEX meeting in a hurry, and I didn't get screenshots of the gobbledygook at the end, but it looked very similar to the last screen shot, which is from our team's code running on the robot (with parts redacted))
PC Emulator:
Image

Cortex: (Sorry it got disconnected before I could get a good screenshot, but the values are still the same)
Image

Team's code (it is an array of structs, which is why you see multiple values for one array indice):
Image


Tue Jan 31, 2012 1:53 am
Profile
Rookie

Joined: Sun Nov 20, 2011 12:06 pm
Posts: 14
Post Re: Slightly Big Arrays == RobotC Not Happy?
I had similar problems when I went to 3.05. Some of my programs which used arrays failed (got filled with weird characters) on 3.05 but worked fine on the previous robotc version. I had to downsize my arrays to get them working again.


Tue Jan 31, 2012 8:38 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Slightly Big Arrays == RobotC Not Happy?
Does it work if you manually initialize the array to a default value? ANSI C doesn't do this for you either.

_________________
sudo rm -rf /


Tue Jan 31, 2012 11:11 am
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Slightly Big Arrays == RobotC Not Happy?
I haven't tried that. In the past on the cortex, and in 3.05 in the simulator, ROBOTC has initialized all the array elements to 0 (I have checked this)
Also, I'm pretty sure that certain parts of the array are pointing to something they shouldn't, because when I ran my teams code, certain other arrays that held values for displaying text on the LCD screen got corrupted as well, although that could have just been a fluke in my testing. When I get access to the cortex tonight, I can do more tests; any suggestions?


Tue Jan 31, 2012 5:00 pm
Profile
Rookie

Joined: Sun Nov 20, 2011 12:06 pm
Posts: 14
Post Re: Slightly Big Arrays == RobotC Not Happy?
I think mine are getting stomped on.
I was using the arrays to store time series gyroscope and accelerometer data for testing my balancing robot. After a test was over, I had a for loop that would write the arrays to the debug stream so I could analyze it with excel. (I wish there was a better way of collecting robot data) Anyway, it worked fine with the previous version of robotc.
After I updated to 3.05, parts of the arrays would end up with numbers that didn't make sense. So, I ended up having to shorten the arrays to make it work again.


Wed Feb 01, 2012 12:04 am
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Slightly Big Arrays == RobotC Not Happy?
Ok, so tonight I tried different combinations of running my teams current code, the broken code and that array test code along with ROBOTC 3.05, ROBOTC 3.04, the Cortex usercode firmware for ROBOTC 3.05, and the Cortex usercode firmware for ROBOTC 3.04. Surprisingly, they all seemed to have some sort of problem or another, which leads me to believe that maybe it is the new Master Code (3.20) that comes with ROBOTC 3.05 that is causing the issue (though I didn't downgrade back the master code to test this theory).

On the other hand, if this is an issue with ROBOTC, would the nice people who make ROBOTC please look into this (since it seems to be kind of a bad thing if arrays are overflowing), and fix it in the next version, or at least tell us what's causing it and/or how we can get around it? Pretty please + a cherry on top :-D?

Thanks!


Wed Feb 01, 2012 1:03 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Slightly Big Arrays == RobotC Not Happy?
Send a mail to support@robotc.net to report this bug. Be as descriptive as you can and include a link to this thread.

- 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]


Wed Feb 01, 2012 3:11 am
Profile WWW
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Slightly Big Arrays == RobotC Not Happy?
Thanks mightor! I have sent an email to support@robotc.net, which apparently opened a new support ticket. I did have one open already (but it didn't really have much information in it; this new one is a lot more detailed). Still, I'm sorry I opened two tickets for one issue!

If the problem is simply that I need to manually initialize my arrays before using them, how would I go about doing that in ROBOTC? I have an array of structs, so it looks (something) like this:
Code:
typedef enum
{
   ONEFISH,
   TWOFISH,
   REDFISH,
   BLUEFISH,
} TFishes;

typedef enum
{
  ANALOG1 = -1,
  NULLCONDITION = 0,
  ANALOG2 = 1,
  ANALOG3 = 2,
  ANALOG4 = 3,
  ANALOG5 = 4,
  ANALOG6 = 5,
  ANALOG7 = 6,
  ANALOG8 = 7,
  DIGITAL1 = 8,
  DIGITAL2 = 9,
  DIGITAL3 = 10,
  DIGITAL4 = 11,
  DIGITAL5 = 12,
  DIGITAL6 = 13,
  DIGITAL7 = 14,
  DIGITAL8 = 15,
  DIGITAL9 = 16,
  DIGITAL10 = 17,
  DIGITAL11 = 18,
  DIGITAL12 = 19,
  SOMEBUTTON = 20,
  SOMEOTHERBUTTON = 21,
  ACOOLBUTTON = 22,
  NOWAITYESIMABUTTON = 23
} TDifferentInputs;

typedef struct
{
   string OfCharacters;
   int TweedleDee;
   int TweedleDumb;
   TFishes Fish;
   int RoadsAManMustWalk;
   //Trip values
   int TimerValue;
   int AngelsOnPinHead;
   int ShoeSize;
   //Functions
   TDifferentInputs Inputs;
   int StreetNumber;
} TAmazingStruct;

TAmazingStruct TastyArray[3][21];


This is basically my team's code with conspicuously changed variable names :-D
When I run it on the PC Emulator, it initializes everything just fine (but maybe that is because it creates a brand new zero-ed out simulation environment every time I download? I would think not, because I can download other files to the virtual robot, then redownload my code, and those other files are still there in memory.)
Here's a screenshot:
Image

So, how would I go about manually initializing an array like TastyArray in ROBOTC?


Thu Feb 02, 2012 1:05 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3227
Location: Rotterdam, The Netherlands
Post Re: Slightly Big Arrays == RobotC Not Happy?
For arrays I tend to use memset(), like this:
Code:
memset(array, 0, sizeof(array));


- 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]


Thu Feb 02, 2012 3:01 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Slightly Big Arrays == RobotC Not Happy?
I am not aware of any issues in ROBOTC with large arrays and

One thing that might be the root cause is that due to legacy support reasons there is an option on all platforms except the VEX PIC to either zero or not zero uninitialized global variables on program startup. The legacy issue is that the very early versions of ROBOTC for the LEGO RCX platform did not zero memory on program start to mimic the behavior of LEGO firmware; some people took advantage of this to "chain" multiple program execution where the "N+1" program executed shared global variables with the "Nth" program executing.

The ANSI C language standard is that all static (and extern) variables that are not explictly initialized should be zero on program startup. Stack based variables -- e.g. local procedure parameters -- have an undefined value when they first come into scope.

If you go to the "Detailed Preferences" under the "Compiler" tab you'll find a check box to "Zero variables before program start". The default value of this setting is enabled. If this was inadvertently disabled, then this could potentially be the cause of the issues described in this thread.

Of course, there could be problems with ROBOTC code generation for large arrays. But I'm not aware of any. Can someone submit a program that explicitly demonstates? You can send it directly to dickswan "at" sbcglobal.net to get a faster response.

NOTE: global variables are initialized to zero when program starts. So the contents is undefined when you've first downloaded a program and in the debugger but have not started the program. This is expected behavior. And the "garbage" is actually the settings from the last program that was executed.


Thu Feb 02, 2012 1:15 pm
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Slightly Big Arrays == RobotC Not Happy?
@mightor Ok, I'll try that!

@Dick Swan Good thoughts!
Yes, the "Zero variables before program start (n/a VEX)" box is checked under Compiler Preferences, thanks for asking!
The array gobbledygook seems to be persistent through the starting of the program, though; it is in the array before and after the program starts.

I would imagine that the code I last posted (REDFISH BLUEFISH we'll call it :-) ) would have gobbledygook in it if run on a cortex; it behaves normally when I run it from the PC Simulator, though, as I have explained above. I will test that code on a Cortex when I get the chance.

Thanks for all your help everyone!


Thu Feb 02, 2012 6:40 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Slightly Big Arrays == RobotC Not Happy?
Just a minor point
Code:
task main()
{
  int ArrayX[50][100];
  ...
}

is a "dynamically" allocated array. It is a "local" variable defined within a function/task. The standard ANSI C specification will state the initial value is "undefined" and implementation dependent.

In Microsoft Visual studio you might find that variables are set to some strange value and it debug mode whenever a value is accessed the compiler inserts code to see if the value is this strange value. If so, it creates an error indicating that you've read a variable before it is written.

In ROBOTC what I thought would happen is that the variable would most likely be initialized to zero but there is no guarantee.

NOTE: If the above code were instead the following, then it would be a "statically" allocated variable and the standard ANSI C specification states it should have been initialized to zero just before the program starts.

Code:
int ArrayX[50][100];

task main()
{
  ...
}


IN any case, I will try out on a real VEX Cortex to see if there are any non-zero values.


Thu Feb 02, 2012 7:14 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Slightly Big Arrays == RobotC Not Happy?
There was a bug in the ROBOTC firmware which I have just fixed for the next release. There was a mismatch between the total size of available user variable space that the firmware thought was used versus the size that ROBOTC IDE thought. This was corrected.

The end result was that when firmware was zeroing user variables, the variables at high addresses were not being zeroed. There may also have been other side effects of this under-sized array that I did not explore.

Here's a test program that checks the array is properly initialized. It uses the "Debug Stream" output window of the Debugger (use Advanced or Super User menu level to make it visible).

Code:
task main()
{
  int i;
  int j;
  int ArrayX[50][100];


  for (i = 0; i < 50; ++i)
  {
    for (j = 0; j < 100; ++j)
    {
      if (ArrayX[i][j] != 0)
        writeDebugStreamLine("Value 'ArrayX[%d][%d] is %d. NOT ZERO AS EXPECTED!!", i, j, ArrayX[i][j]);
    }
  }
  writeDebugStreamLine("Done....");
}


Thu Feb 02, 2012 8:11 pm
Profile
Rookie

Joined: Fri Jan 27, 2012 6:57 pm
Posts: 40
Post Re: Slightly Big Arrays == RobotC Not Happy?
So you were able to recreate the problem on a different system, yay!
Thank you so much Dick Swan! When will the next release of ROBOTC come out?
Also, why does this affect the cortex, and not the PC Simulator o_O?


Fri Feb 03, 2012 1:09 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Slightly Big Arrays == RobotC Not Happy?
sumasmreq wrote:
So you were able to recreate the problem on a different system, yay!
Thank you so much Dick Swan! When will the next release of ROBOTC come out?
Also, why does this affect the cortex, and not the PC Simulator o_O?

New release in a week or so.

Only impacted VEX Cortex because it was the only platform that had the mismatch in size declared in ROBOTC IDE and ROBOTC platform firmware.


Fri Feb 03, 2012 3:21 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 17 posts ]  Go to page 1, 2  Next

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.