View unanswered posts | View active topics It is currently Thu Sep 24, 2020 9:31 pm

Reply to topic  [ 3 posts ] 
Type string and memory allocation 
Author Message

Joined: Tue Dec 11, 2012 9:19 pm
Posts: 58
Post Type string and memory allocation
'string' is not legal C, it's an extension that RobotC has added.

I was looking for documentation to see if RobotC did any special
memory handling with this type and I found this.

Which says this...
  string str1 = "";            // 'str1' is "" (empty)
  string str2 = "DNA";         // string 'str2' is "DNA"
  strcpy(str1, str2);          // copy string 'str2' onto string 'str1'

I find this really strange as this is never a pattern you'd see
in standard C wherein you had a string literal of zero length
and you copy a string literal of length N into it.

And in fact the documentation for strcpy right above that says...

(void) Function copies pFromBuffer to pToBuffer. The variables are arrays of bytes terminated with a zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking! Identical to the function found in conventional C 'string.h' library.

So str1 is empty (or one byte for the null terminator), but we are going to copy 4
bytes into it.

So is the type string essentially

typedef char* string;

or is it something special, and if it's not special why does
the above not result in a write beyond array boundaries?

Thu Oct 09, 2014 12:55 am
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 256
Post Re: Type string and memory allocation
String does seem to be it's own special kind of type. Now how the internals of it work, I have no idea. However it does seem safe to say that the ROBOTC documentation is wrong in that the strcpy function DOES NOT work exactly like the C one. Maybe the documentation is left over from an earlier version, where it actually used C-strings instead of their new specialized string type.

FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at

Thu Oct 09, 2014 8:45 am
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
Post Re: Type string and memory allocation
Strings are handled as a special data type in ROBOTC, with a maximum of 20 characters allowed per string. Per the Data Type support documents page:

Multiple ASCII characters 'strung' together, such as a word or sentence.
20 ASCII characters per string
160 bits/20 bytes

Whenever a string variable is created in ROBOTC, 20 bytes (19 characters worth of memory) is allocated for that variable, whether the string uses all 20 characters or not. In the example you have provided above, the str1 string has 20 bytes available for storage even though none are being used. If you attempted to put in more than 19 characters into a string, any extra characters would be truncated off of the string:

task main()
string one = "";
string two = "123456789012345678901234567890";

strcpy(one, two);



Edit: The new support documents can be found at the links below, the wiki is no longer being updated with new information.

ROBOTC for LEGO Mindstorms 4.X Support Documents:
ROBOTC for VEX Robotics 4.X Support Documents:

Check out our Blog! And our Facebook page!
Need help? Take a look at our updated help documentation and the ROBOTC Forums.

Fri Oct 31, 2014 2:30 pm
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.