Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The main problem of c strlen is that stops at first null character, returning wrong size especially in serialized data when
- many null characters exists within your data. You just need to pass the pointer to char * as well as the memory allocated size
- as second parameter (max). This method just walks from the maximum memory address to the minimum and stops the loop when the
- first non null character appears. Then returns the real data length.
- */
- size_t better_strlen(const char * str, int max) {
- const char * maxs;
- maxs = str + max;
- while(str<maxs) {
- if(*maxs!=0) break;
- maxs--;
- }
- return(maxs-str)+1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement