Type string and memory allocation
Page 1 of 1

Author:  skatefriday [ Thu Oct 09, 2014 12:55 am ]
Post subject:  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?

Author:  BurningLights [ Thu Oct 09, 2014 8:45 am ]
Post subject:  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.

Author:  JohnWatson [ Fri Oct 31, 2014 2:30 pm ]
Post subject:  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:

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group