<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Forum BestPig ToolBox - Implementation de la fonction strlen]]></title>
		<link>http://www.bestpig.fr/forum/index.php</link>
		<description><![CDATA[Les messages le plus récents de Forum BestPig ToolBox.]]></description>
		<lastBuildDate>Wed, 21 Apr 2010 20:18:59 +0000</lastBuildDate>
		<generator>FireBB</generator>
		<item>
			<title><![CDATA[Implementation de la fonction strlen]]></title>
			<link>http://www.bestpig.fr/forum/index.php?page=viewtopic&amp;pid=26#p26</link>
			<description><![CDATA[D&eacute;sol&eacute;, mais franchement la, j'ignore la r&eacute;ponse que tu cherche <img src="http://www.bestpig.fr/forum/images/blue/bbcode/smilies/sad.png" width="15" height="15" alt="sad" />.]]></description>
			<author><![CDATA[dummy@example.com (bestpig)]]></author>
			<pubDate>Wed, 21 Apr 2010 20:18:59 +0000</pubDate>
			<guid>http://www.bestpig.fr/forum/index.php?page=viewtopic&amp;pid=26#p26</guid>
		</item>
		<item>
			<title><![CDATA[Implementation de la fonction strlen]]></title>
			<link>http://www.bestpig.fr/forum/index.php?page=viewtopic&amp;pid=25#p25</link>
			<description><![CDATA[Bonsoir a tous,<br />
<br />
Suite a des conversations sur le SdZ et le fclc, on a vu que l'implementation du K&amp;R et de Plauger de la fonction strlen soulevait un probleme de protabilite entre les differentes architectures, voici le code en question :<br />
<br />
<div class="codebox"><cite>Code C : <a onclick="code_select('s8908153132')" href="javascript:void(0)">[Séléctionner le code]</a></cite><pre id="s8908153132"><code><br /><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
size_t mystrlen<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p;
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">=</span> s; <span style="color: #339933;">*</span>p; <span style="color: #339933;">++</span>p<span style="color: #009900;">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> p <span style="color: #339933;">-</span> s;
<span style="color: #009900;">&#125;</span>
&nbsp;</code></pre></div>
<br />
et voici l'implementation de la GlibC qui utilise globalement le meme principe mais en paliant a ce souci de portabilite :<br />
<br />
<div class="codebox"><cite>Code C : <a onclick="code_select('s3436618095')" href="javascript:void(0)">[Séléctionner le code]</a></cite><pre id="s3436618095"><code><br /><span style="color: #808080; font-style: italic;">/* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc.
&nbsp; &nbsp;This file is part of the GNU C Library.
&nbsp; &nbsp;Written by Torbjorn Granlund (tege@sics.se),
&nbsp; &nbsp;with help from Dan Sahlin (dan@sics.se);
&nbsp; &nbsp;commentary by Jim Blandy (jimb@ai.mit.edu).
&nbsp;
&nbsp; &nbsp;The GNU C Library is free software; you can redistribute it and/or
&nbsp; &nbsp;modify it under the terms of the GNU Lesser General Public
&nbsp; &nbsp;License as published by the Free Software Foundation; either
&nbsp; &nbsp;version 2.1 of the License, or (at your option) any later version.
&nbsp;
&nbsp; &nbsp;The GNU C Library is distributed in the hope that it will be useful,
&nbsp; &nbsp;but WITHOUT ANY WARRANTY; without even the implied warranty of
&nbsp; &nbsp;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. &nbsp;See the GNU
&nbsp; &nbsp;Lesser General Public License for more details.
&nbsp;
&nbsp; &nbsp;You should have received a copy of the GNU Lesser General Public
&nbsp; &nbsp;License along with the GNU C Library; if not, write to the Free
&nbsp; &nbsp;Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
&nbsp; &nbsp;02111-1307 USA. &nbsp;*/</span>
&nbsp;
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #339933;">#undef strlen</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Return the length of the null-terminated string STR. &nbsp;Scan for
&nbsp; &nbsp;the null terminator quickly by testing four bytes at a time. &nbsp;*/</span>
size_t
strlen <span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str;
<span style="color: #009900;">&#123;</span>
&nbsp; <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>char_ptr;
&nbsp; <span style="color: #993333;">const</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>longword_ptr;
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> longword, magic_bits, himagic, lomagic;
&nbsp;
&nbsp; <span style="color: #808080; font-style: italic;">/* Handle the first few characters by reading one character at a time.
&nbsp; &nbsp; &nbsp;Do this until CHAR_PTR is aligned on a longword boundary. &nbsp;*/</span>
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>char_ptr <span style="color: #339933;">=</span> str; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> char_ptr
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>longword<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span>;
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">++</span>char_ptr<span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>char_ptr <span style="color: #339933;">==</span> <span style="color: #ff0000;">'x98AnTiSlAsHx980'</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> char_ptr <span style="color: #339933;">-</span> str;
&nbsp;
&nbsp; <span style="color: #808080; font-style: italic;">/* All these elucidatory comments refer to 4-byte longwords,
&nbsp; &nbsp; &nbsp;but the theory applies equally well to 8-byte longwords. &nbsp;*/</span>
&nbsp;
&nbsp; longword_ptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> char_ptr;
&nbsp;
&nbsp; <span style="color: #808080; font-style: italic;">/* Bits 31, 24, 16, and 8 of this number are zero. &nbsp;Call these bits
&nbsp; &nbsp; &nbsp;the &quot;holes.&quot; &nbsp;Note that there is a hole just to the left of
&nbsp; &nbsp; &nbsp;each byte, with an extra at the end:
&nbsp;
&nbsp; &nbsp; &nbsp;bits: &nbsp;01111110 11111110 11111110 11111111
&nbsp; &nbsp; &nbsp;bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
&nbsp;
&nbsp; &nbsp; &nbsp;The 1-bits make sure that carries propagate to the next 0-bit.
&nbsp; &nbsp; &nbsp;The 0-bits provide holes for carries to fall into. &nbsp;*/</span>
&nbsp; magic_bits <span style="color: #339933;">=</span> 0x7efefeffL;
&nbsp; himagic <span style="color: #339933;">=</span> 0x80808080L;
&nbsp; lomagic <span style="color: #339933;">=</span> 0x01010101L;
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>longword<span style="color: #009900;">&#41;</span> &gt; <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span>
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* 64-bit version of the magic. &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Do the shift in two steps to avoid a warning if long has 32 bits. &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; magic_bits <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>0x7efefefeL &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> | 0xfefefeffL;
&nbsp; &nbsp; &nbsp; himagic <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>himagic &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> | himagic;
&nbsp; &nbsp; &nbsp; lomagic <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>lomagic &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> &lt;&lt; <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> | lomagic;
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>longword<span style="color: #009900;">&#41;</span> &gt; <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; abort <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp; <span style="color: #808080; font-style: italic;">/* Instead of the traditional loop which tests each character,
&nbsp; &nbsp; &nbsp;we will test a longword at a time. &nbsp;The tricky part is testing
&nbsp; &nbsp; &nbsp;if *any of the four* bytes in the longword in question are zero. &nbsp;*/</span>
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>;;<span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span>
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* We tentatively exit the loop if adding MAGIC_BITS to
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LONGWORD fails to change any of the hole bits of LONGWORD.
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1) Is this safe? &nbsp;Will it catch all the zero bytes?
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Suppose there is a byte with all zeros. &nbsp;Any carry bits
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;propagating from its left will fall into the hole at its
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;least significant bit and stop. &nbsp;Since there will be no
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;carry from its most significant bit, the LSB of the
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte to the left will be unchanged, and the zero will be
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;detected.
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2) Is this worthwhile? &nbsp;Will it ignore everything except
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;zero bytes? &nbsp;Suppose every byte of LONGWORD has a bit set
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;somewhere. &nbsp;There will be a carry into bit 8. &nbsp;If bit 8
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is set, this will carry into bit 16. &nbsp;If bit 8 is clear,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;one of bits 9-15 must be set, so there will be a carry
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;into bit 16. &nbsp;Similarly, there will be a carry into bit
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;24. &nbsp;If one of bits 24-30 is set, there will be a carry
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;into bit 31, so all of the hole bits will be changed.
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;The one misfire occurs when bits 24-30 are clear and bit
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;31 is set; in this case, the hole at bit 31 is not
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;changed. &nbsp;If we had access to the processor carry flag,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;we could close this loophole by putting the fourth hole
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;at bit 32!
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;So it ignores everything except 128's, when they're aligned
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;properly. &nbsp;*/</span>
&nbsp;
&nbsp; &nbsp; &nbsp; longword <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>longword_ptr<span style="color: #339933;">++</span>;
&nbsp;
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>
<span style="color: #339933;">#if 0</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Add MAGIC_BITS to LONGWORD. &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>longword <span style="color: #339933;">+</span> magic_bits<span style="color: #009900;">&#41;</span>
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Set those bits that were unchanged by the addition. &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">^</span> ~longword<span style="color: #009900;">&#41;</span>
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/* Look at only the hole bits. &nbsp;If any of the hole bits
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; are unchanged, most likely one of the bytes was a
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zero. &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">&amp;</span> ~magic_bits<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#else</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>longword <span style="color: #339933;">-</span> lomagic<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> himagic<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#endif</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Which of the bytes was the zero? &nbsp;If none of them were, it was
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a misfire; continue the search. &nbsp;*/</span>
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>longword_ptr <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>longword<span style="color: #009900;">&#41;</span> &gt; <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">4</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">5</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">6</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>cp<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> cp <span style="color: #339933;">-</span> str <span style="color: #339933;">+</span> <span style="color: #cc66cc;">7</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span>
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
libc_hidden_builtin_def <span style="color: #009900;">&#40;</span>strlen<span style="color: #009900;">&#41;</span>
&nbsp;</code></pre></div>
<br />
Ma question est donc la suivante : Comment est il possible que Plauger et le K&amp;R aient diffuse ce code tout en sachant qu'il ne fonctionnerait pas correctement partout? Tandis que la GNU libC elle a repondu a ce probleme?<br />
<br />
Ici plauger explique son choix <a href="http://bytes.com/topic/c/answers/458286-plauger-size_t-ptrdiff_t#post1756007" >http://bytes.com/topic/c/answers/458286-plauger-size_t-ptrdiff_t#post1756007</a><br />
<br />
Une implementation exempte des overflows dont il parle aurait ete d'incrementer un compteur jusqu'a ce qu'un caractere NULL BYTE soit rencontre. Il y a toujours risque d'overflow mais la, on y peut plus rien... depasser la capacite d'un size_t ou d'un unsigned long (ou unsigned long long pour le C99) il faut le faire<br />
<br />
Le probleme est explique plus en detail ici : <a href="http://groups.google.fr/group/comp.lang.c/msg/f06bf3ea5893778b?hl=fr" >http://groups.google.fr/group/comp.lang.c/msg/f06bf3ea5893778b?hl=fr</a>]]></description>
			<author><![CDATA[dummy@example.com (Adroneus)]]></author>
			<pubDate>Wed, 21 Apr 2010 20:18:59 +0000</pubDate>
			<guid>http://www.bestpig.fr/forum/index.php?page=viewtopic&amp;pid=25#p25</guid>
		</item>
	</channel>
</rss>
