Sponsored Content
Top Forums Programming pointer arithmetic vs. strlen() & strnlen()? Post 302280391 by jjinno on Monday 26th of January 2009 09:26:13 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRNLEN(3)						     Linux Programmer's Manual							STRNLEN(3)

NAME
strnlen - determine the length of a fixed-size string SYNOPSIS
#include <string.h> size_t strnlen(const char *s, size_t maxlen); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): strnlen(): Since glibc 2.10: _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE DESCRIPTION
The strnlen() function returns the number of characters in the string pointed to by s, excluding the terminating null byte (''), but at most maxlen. In doing this, strnlen() looks only at the first maxlen characters in the string pointed to by s and never beyond s+maxlen. RETURN VALUE
The strnlen() function returns strlen(s), if that is less than maxlen, or maxlen if there is no null terminating ('') among the first maxlen characters pointed to by s. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +----------+---------------+---------+ |Interface | Attribute | Value | +----------+---------------+---------+ |strnlen() | Thread safety | MT-Safe | +----------+---------------+---------+ CONFORMING TO
POSIX.1-2008. SEE ALSO
strlen(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2016-03-15 STRNLEN(3)
All times are GMT -4. The time now is 01:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy