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


 
Thread Tools Search this Thread
Top Forums Programming pointer arithmetic vs. strlen() & strnlen()?
# 1  
Old 01-26-2009
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?
# 2  
Old 01-26-2009
Quote:
Originally Posted by jjinno
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 Smilie 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?
# 3  
Old 01-26-2009
Quote:
Originally Posted by shamrock
do you work 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
# 4  
Old 01-27-2009
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.

Last edited by Corona688; 01-27-2009 at 01:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP: declared variables, strlen vs isset

greetings, pretty new to php and i think i might be missing some fundamental limitation of isset. i have two php scripts below that are executed by crond, one using --host X and one that does not. and below that are three different attempts at generating a command line that will be executed. the... (8 Replies)
Discussion started by: crimso
8 Replies

2. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

3. Programming

Pointer arithmetic for list of strings

When i want to use a list of strings char *list; i never understand it well. Here is an easy example. It took me long to figure it out, searching the web didn't help much: #include <stdio.h> int main(void) { char *list = {"foo", "bar", "baz"}; char **pl; int... (6 Replies)
Discussion started by: tornow
6 Replies

4. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

5. Programming

strlen for UTF-8

My OS (Debian) and gcc use the UTF-8 locale. This code says that the char size is 1 byte but the size of 'a' is really 4 bytes. int main(void) { setlocale(LC_ALL, "en_US.UTF-8"); printf("Char size: %i\nSize of char 'a': %i\nSize of Euro sign '€': %i\nLength of Euro sign: %i\n",... (8 Replies)
Discussion started by: cyler
8 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies

9. Shell Programming and Scripting

Problem with the strlen function in ksh

Hello, Just a little problem with the ksh function : strlen I want to use this function in this little ksh program : while read line ; do TOTO=$line TOTONB=strlen($TOTO) echo $TOTONB (3 Replies)
Discussion started by: steiner
3 Replies

10. Programming

Problems with Strlen

hello, i have a problem with strlen. I have written this: for(y=13,z=0; cInBuf!=' ';y++) { cBuf=cInBuf; z++; } len = strlen(cBuf); out=len/2; fprintf(outfile,"F%i",out); If strlen is e.g. 22, it write F22. I want to write F2F2. How can i do this?... (5 Replies)
Discussion started by: ACeD
5 Replies
Login or Register to Ask a Question