4 ms·
Copy-free contiguous subsets of arrays are fairly simple but often convenient. If you want elements 5 through 33 of big_array, you just get a pointer to element
by _delirium 16y ago
Copy-free contiguous subsets of arrays are fairly simple but often convenient. If you want elements 5 through 33 of big_array, you just get a pointer to element 5, and keep track separately of the length. A common case is where you split an array into two non-overlapping subparts, in which case, if you no longer need the original, you can treat each subpart as if it were a separate array. Saves the work of allocating two new arrays for the split parts, which is necessary in many other languages. Useful for efficiently implementing things like decision-tree learners.
- cma 16y agoAlso useful for storing parse results: "<a href='blah'>" gets modified during parsing to become: "<a\0href\0'blah\0>" Your parse tree result result can then just contain pointers to "a\0", "href\0", and "blah\0" rather than doing any copying.
- deutronium 16y agoThis is how strtok returns tokens too. Incidentally I only found this after trying: char tmp[] = "cat,mat,sat"; char *t; t = strtok(tmp,","); while(t != NULL){ printf("%s\n",t); t = strtok(NULL,","); } And getting a segfault, as 'tmp' is not writeable memory.