TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Darin McBride
from: Neil Heller
date: 2004-04-17 20:33:00
subject: Squares

JB>>>> and possibly even better re-written to use a char* instead

JB>>>>  for( s=target ; *s ; s++ ) {

   >>> Yes, I like this even more ;-)

BB>>     Not much point in using a for(;;) loop at all then. Why not
BB>> just:-

BB>>     char *s=target;
BB>>     while(*s++) {...};

BB>>     simpler, neater, more direct.

JB> but not equivalent.

DM>Good catch!


How so?  These sure _look_ like they do the same thing.






DM> JB>   char *s=target-1;
DM> JB>   while(*++s) {...};

DM>Not supported by the standard ;-)

DM>char* s = target;

DM>if (*s != '\0')
DM>{
DM>  do {
DM>    ...
DM>  } while (*++s);
DM>}

DM>The problem with yours is that if target was, for example, pointing
DM>to  address 0x00000001, when you subtracted 1, it may not work. 
DM> Now, before any of the technical gurus attack me for 
DM>breaking the standard just as much as I just complained 
DM>about, I just want to say that this is only intended to get 
DM>a point across.  In reality, subtracting 1 from 0x00000001 
DM>may work (an address of 0x00000000 is not necessarily the 
DM>same as NULL).  However, I'm sure anyone who didn't see the 
DM>problem with Jasen's original code would have missed this 
DM>problem, too ;-)

DM>Unfortunately, I'm not sure that there is a more real 
DM>example.  All I know is that the standard does not 
DM>guarantee what happens when you point to memory that is not 
DM>given to you by the compiler (either by saying char*s="foo" 
DM>or taking the address of something on the stack, or from 
DM>the return of malloc or any other allocating routine).  
DM>Even if you don't dereference it.

DM>So, you can't simply decrement and increment and expect things to
DM>work  perfectly across all conforming environments (generally 
DM>speaking, an "environment" will be a compiler, but, again, 
DM>nothing in the standard decrees such ;->).  I can't imagine 
DM>a reason for this to fail on most PCs, but perhaps there 
DM>are embedded systems that go quaint on us here, or perhaps 
DM>there are RISC machines that have these issues, or 
DM>whatever.  I'm not an expert on things behind the scenes.  
DM>All I know is that as long as I follow the standard, and 
DM>things don't work, then I have the ability to complain to 
DM>the vendor.  Otherwise, well, the vendor may tell me it's a 
DM>feature ;->

DM>All that said, I'm not sure that the while loop version is 
DM>better than the for loop version.  ;-)

DM>Darin

DM>---

þ CMPQwk 1.42 999

--- Maximus/2 3.01
DM> * Origin: Tanktalus' Tower BBS (1:250/102)
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.