"John J. Corelli" <[EMAIL PROTECTED]> writes:

>    strptr = "00:00.0";
>    dbgptr = "12:34.5";

>       StrCopy(dbgptr, srcptr);

I don't know about CW, but gcc will put those string constants into the
text segment, so trying to strcpy one onto the other will cause a
segfault.

With gcc, this behavior can be changed, but in general, it is very bad
practice to copy one string constant onto another, or modify a string
constant at all. It makes for unreadable and difficult to maintain
code. In addition, it prevents the compiler from utilizing certain space
optimization techniques.

To demonstrate why modifying constant strings is bad, consider the
following code:

------------------------------------------------------------------------
$ cat > example.c
int foo(int changeit)
{
  char *s = "This is a string";

  if (changeit) s[3] = 'x';
  printf("%s\n", s);
}

int main(int argc, char *argv[])
{
  foo(0);
  foo(1);
  foo(0);
}
^D
$ gcc -o example example.c -fwritable-strings
$ ./example
This is a string
Thix is a string
Thix is a string
------------------------------------------------------------------------

Notice how the third call to foo() still prints out the changed string,
even though the parameter is 0. That's because the string "constant" was
changed in the second call, and will remain changed in subsequent
calls. In any complex program, this could lead to extremely obscure bugs.

-- 
Dave Carrigan ([EMAIL PROTECTED])            | Yow! I hope the ``Eurythmics''
UNIX-Apache-Perl-Linux-Firewalls-LDAP-C-DNS | practice birth control...
Seattle, WA, USA                            | 
http://www.rudedog.org/                     | 

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to