Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. /*
  2. The main problem of c strlen is that stops at first null character, returning wrong size especially in serialized data when
  3. many null characters exists within your data. You just need to pass the pointer to char * as well as the memory allocated size
  4. as second parameter (max). This method just walks from the maximum memory address to the minimum and stops the loop when the
  5. first non null character appears. Then returns the real data length.
  6. */
  7.  
  8. size_t better_strlen(const char * str, int max) {
  9. const char * maxs;
  10. maxs = str + max;
  11.  
  12. while(str<maxs) {
  13. if(*maxs!=0) break;
  14. maxs--;
  15. }
  16.  
  17. return(maxs-str)+1;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement