would the workings of strlen() be something like this?

int strlen(const char * str)
{
       int size = 0;
       while (!!*str)
              size++;
       return size;
}

any feedback would be appreciated. also i have learned that !! will convert a value to an actual bool eg. !!104 = 1

ah yes I forgot to increment the pointer as well. so just drop the double ! and the incrementation would be the basic workings right?

int strlen(const char * str)
{
       int size = 0;
       while (*str++)
              size++;
       return size;
}

That's basically the idea. On Intel based PCs the compiler might actually implement it by using assembly instructions such as the scansb instruction, which greatly speeds up the operation because there is no software loop involved.

Edited 6 Years Ago by Ancient Dragon: n/a

Comments
thanks for your help

I'm using MSVC++ 2005 is there a way to see the code for this function in assembly to see if it does convert it to a scansb instruction?

I think AD is referring to scasb, not scansb? I get way more hits on scasb. Frankly, I had never heard of either of them, but you could fit what I know about Assembly in a test tube and still have room for several cc's of blood. Here's a thread that might be of interest:

http://www.int80h.org/strlen/

Google "assembly strlen scasb" and some good hits come up.

Thanks Vernon for the link it was very insightful. Also I found out how to output the code in assembly to check it and my code is much longer. I think I might have to play with my optimization settings. here it is just FYI

// driver.cpp
int strlen(const char * str)
{
       int size = 0;
       while (*str++)
              size++;
       return size;
}

and

//driver.asm
; Listing generated by Microsoft (R) Optimizing Compiler Version 14.00.50727.762 

	TITLE	c:\Documents and Settings\Nathan\My Documents\Visual Studio 2005\Projects\daniweb\daniweb\Driver.cpp
	.686P
	.XMM
	include listing.inc
	.model	flat

INCLUDELIB MSVCRTD
INCLUDELIB OLDNAMES

PUBLIC	?strlen@@YAHPBD@Z				; strlen
EXTRN	__RTC_Shutdown:PROC
EXTRN	__RTC_InitBase:PROC
;	COMDAT rtc$TMZ
; File c:\documents and settings\nathan\my documents\visual studio 2005\projects\daniweb\daniweb\driver.cpp
rtc$TMZ	SEGMENT
__RTC_Shutdown.rtc$TMZ DD FLAT:__RTC_Shutdown
rtc$TMZ	ENDS
;	COMDAT rtc$IMZ
rtc$IMZ	SEGMENT
__RTC_InitBase.rtc$IMZ DD FLAT:__RTC_InitBase
; Function compile flags: /Odtp /RTCsu /ZI
rtc$IMZ	ENDS
;	COMDAT ?strlen@@YAHPBD@Z
_TEXT	SEGMENT
tv65 = -205						; size = 1
_size$ = -8						; size = 4
_str$ = 8						; size = 4
?strlen@@YAHPBD@Z PROC					; strlen, COMDAT

; 11   : {

	push	ebp
	mov	ebp, esp
	sub	esp, 208				; 000000d0H
	push	ebx
	push	esi
	push	edi
	lea	edi, DWORD PTR [ebp-208]
	mov	ecx, 52					; 00000034H
	mov	eax, -858993460				; ccccccccH
	rep stosd

; 12   :        int size = 0;

	mov	DWORD PTR _size$[ebp], 0
$LN2@strlen:

; 13   :        while (*str++)

	mov	eax, DWORD PTR _str$[ebp]
	mov	cl, BYTE PTR [eax]
	mov	BYTE PTR tv65[ebp], cl
	mov	edx, DWORD PTR _str$[ebp]
	add	edx, 1
	mov	DWORD PTR _str$[ebp], edx
	movsx	eax, BYTE PTR tv65[ebp]
	test	eax, eax
	je	SHORT $LN1@strlen

; 14   :               size++;

	mov	eax, DWORD PTR _size$[ebp]
	add	eax, 1
	mov	DWORD PTR _size$[ebp], eax
	jmp	SHORT $LN2@strlen
$LN1@strlen:

; 15   :        return size;

	mov	eax, DWORD PTR _size$[ebp]

; 16   : }

	pop	edi
	pop	esi
	pop	ebx
	mov	esp, ebp
	pop	ebp
	ret	0
?strlen@@YAHPBD@Z ENDP					; strlen
_TEXT	ENDS
END

after optimization the code for my strlen function is now

?strlen@@YAHPBD@Z PROC					; strlen

; 12   :        int size = 0;

	xor	eax, eax
$LL2@strlen:

; 13   :        while (*str++)
; 14   :               size++;

	add	eax, 1
	cmp	BYTE PTR $SG-5[eax], 0
	jne	SHORT $LL2@strlen

; 15   :        return size;
; 16   : }

	ret	0
?strlen@@YAHPBD@Z ENDP					; strlen

I just found out that VC++ 2008 Express doesn't produce the assembly code that older M$ compilers did. Your function is the same as produced by VC++ 2008.

This question has already been answered. Start a new discussion instead.