19 August 2004
Pointer arithmetic
One rule: avoid it.
If code passes you a pointer to allocated, raw memory, parse it using the template functions within <algorithm>.
// Get a buffer of null-terminated strings from some dumb function. char * data = NULL; size_t dataSize = 0; _my_alloc_func(&data, &dataSize); // Create our collection iterators. char * begin = data; char * end = data + dataSize; // Parse out the delimited data. char * begin_token = begin; char * end_token = begin; while (end_token != end) { end_token = find(begin_token, end, '\0'); string newToken(begin_token, end_token); begin_token = end_token + 1; } // Clean up. _my_free_func(&data, dataSize);
Here, I've treated the buffer bounds as begin and end iterators. This is effectively the same as "pointer arithmetic," but it allows me to leverage the standard library algorithms while parsing the data. If we make the conceptual shift from byte buffer to byte container, we can use the canned search and looping code that's aready written and optimized.
You might want to make the while-loop into a for-loop in order to separate most of the looping statements from the intent of the loop.
for ( char * begin_token = begin, end_token = begin; end_token != end; begin_token = end_token + 1) { end_token = find(begin_token, end, '\0'); string newToken(begin_token, end_token); }
Scott Meyers recommends, in item 43 of Effective STL, to Prefer algorithm calls to hand-written loops.
His reasons are:
- Efficiency,
- Correctness - There is less chance of off-by-one or increment and decrement errors,
- Maintainability - The intent of code using an algorithm is more clear than that of a hand-written loop.
The last point is important. You have to examine a loop and all of its logic before you understand what it does. With an algorithm, its intent is explicity stated: find, count, copy, etc. This is clear in the code above. The outer loop was hand-coded and requires some explanation. The inner loop (find) is on one line and is immediately understandable.
- Techniques after using Swift for a month posted by sstrader on 26 August 2015 at 11:41:51 PM
- Some thoughts on Ruby after finishing a week (minus one day) of training posted by sstrader on 15 December 2011 at 8:59:30 PM
- Links on the singleton pattern posted by sstrader on 9 December 2011 at 9:19:50 AM
- Phonebot posted by sstrader on 29 October 2011 at 6:37:05 PM
- The labeled break in Java posted by sstrader on 4 September 2011 at 11:27:56 AM