pointer arithmetic vs. strlen() & strnlen()?

I have been getting some flack recently for my use of strlen() and strnlen(). Honestly I have always just taken their functionality for granted as being the easiest way of getting the length of a string. Is it really so much better to do pointer arithmetic? What am I gaining besides more thought-process for me to worry about?

Why are you getting flak :confused: do you work in embedded systems??? strlen and strnlen are library functions so there is overhead associated with them but that is normal. The alternative is to roll your own using pointers but that as you rightly said is just more thought-process. That is why I am wondering if you are working in embedded systems?

Nope. I just happened to be helping somebody debug a seg-fault in their code caused by pointer-arithmetic error & we ended up in this "philosophical discussion".

His point was to suggest that if a "\0" made it into the string, the simple use of strlen() or strnlen() would not do "what he wants" (basically a simple socket-based string messenger). Trying to understand his point, I asked if what he was planning on sending/receiving strings with NULL characters in them... to which he responded quite obviously "No".

I was just trying to ascertain if there really was some performance/reliability/speed/efficiency to be gained in not using strlen() or strnlen(). I thought there legitimately might be, but now I am second-guessing, and simply thinking the guy is nuts to write his own function for something so simple... especially since he got it wrong

If he uses something other than NULLs to terminate strings, then he needs his own strlen. If he already has given lengths for data, he doesn't need strlen. But if he thinks writing his own will be faster than the library function that's not too likely. gcc in particular has intrinsics that can reduce it to a vanishingly small number of instructions in some cases and architectures.

He does have a point, though, in that sockets deliver raw data, not strings per-se. They only have nulls on the end if you send nulls, which is not something you want to rely on with data from an uncontrolled outside source. better to deal with data in random lengths than measure it every time.