Changes between Version 10 and Version 11 of Docs/Prog/Manual/ApplicationLibraries/lib825ev/String
- Timestamp:
- 08/20/10 09:27:26 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Docs/Prog/Manual/ApplicationLibraries/lib825ev/String
v10 v11 1 1 = String Functions = 2 The 825 can be programmed using C/C++ character array strings and the standard C librarysuch as:2 The 825 can be programmed using C/C++ character array strings and standard C library string functions such as: 3 3 {{{ 4 4 char szMsg[10]; … … 9 9 }}} 10 10 11 This is the traditional C programming way of handling strings. The C language has no true string type; strings are just arrays of characters. This is very fast and efficient because the compiler just generates code to read and write the memorydirectly. However, there is no automatic checking that pointers point to valid memory, or to prevent writing beyond memory that is allocated. It is very easy to crash an 825 indicator by programming this code incorrectly.11 This is the traditional C programming way of handling strings. The C language has no true string type; strings are just arrays of characters. This is very fast and efficient because the compiler generates code to read and write the memory locations directly. However, there is no automatic checking that pointers point to valid memory, or to prevent writing beyond memory that is allocated. It is very easy to crash an 825 indicator by programming this code incorrectly. 12 12 13 In the above example the szMsg and szID character arrays are each allocated 10 bytes. C strings require a null character at the end to tell various functions such as strcpy when to stop reading characters. This means szMsg and szID can have a maximum of 9 characters each. If the strcpy(szID, "123") is changed to strcpy(szID, "1234567890") the compiler will generate code that writes the null character into an invalid memory location. When the program is executed this may crash the indicator immediately, or it may not cause a crash until some other time in execution when that memory location becomes important. If the strcpy is changed to strcpy(szID, "123456")szID will not overflow, but szMsg will overflow in the sprintf statement that follows.13 In the above example the szMsg and szID character arrays are each allocated 10 bytes. C strings require a null character at the end to tell various functions such as strcpy when to stop reading characters. This means szMsg and szID can contain a maximum of 9 characters each. If the ''strcpy(szID, "123")'' is changed to ''strcpy(szID, "1234567890")'' the compiler will generate code that writes the null character into an invalid memory location. When the program is executed this may crash the indicator immediately when this code executes, it may not cause a crash until some other time in execution when that memory location becomes important, or it may cause some other strange behavior in the program. If the strcpy is changed to ''strcpy(szID, "123456")'' szID will not overflow, but szMsg will overflow in the sprintf statement that follows. 14 14 15 15 The 825 also supports a newer C++ string type std::string. Using std::string does not require as much diligence from the programmer. The string will automatically allocate more memory and grow if needed by an assignment. The operations are also more like the BASIC language so this is helpful for programmers who are not as skilled in C/C++.