home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13231 < prev    next >
Encoding:
Text File  |  1992-09-04  |  2.3 KB  |  51 lines

  1. Path: sparky!uunet!ogicse!network.ucsd.edu!munnari.oz.au!cs.mu.OZ.AU!mundil.cs.mu.OZ.AU!fjh
  2. From: fjh@mundil.cs.mu.OZ.AU (Fergus James HENDERSON)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strcpy implementation question
  5. Message-ID: <9224902.15217@mulga.cs.mu.OZ.AU>
  6. Date: 4 Sep 92 16:15:40 GMT
  7. Article-I.D.: mulga.9224902.15217
  8. References: <PINKAS.92Aug21114508@caraway.intel.com>     <PINKAS.92Aug25163006@caraway.intel.com> <14213@goanna.cs.rmit.oz.au> <PINKAS.92Sep3173643@skywalker.intel.com>
  9. Sender: news@cs.mu.OZ.AU
  10. Organization: Computer Science, University of Melbourne, Australia
  11. Lines: 38
  12.  
  13. pinkas@skywalker.intel.com (Israel Pinkas) writes:
  14.  
  15. >ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
  16. >
  17. >RK> When is the size of the source known at compile time?  Two cases:
  18. >RK> (a) the source is a string literal (or is a pointer whose value is known
  19. >RK>     by constant propagation to be a specific string literal)
  20. >RK> (b) the code looks something like
  21. >RK>  if (strlen(x) == 27) { /* don't change x */; strcpy(d, x); ...}
  22. >
  23. >Not quite.  I said that the size of the source and destination are known,
  24. >not the length of the string.  If the length of the string is known, and it
  25. >can be guaranteed to be inviolate, we only have to copy enough bytes to
  26. >move the string.  However, strings are not inviolate, as the middle of the
  27. >string can be modified in ways that the compiler cannot control, such as
  28. >signal handlers.
  29.  
  30. Actually, in a strictly conforming program, a signal handler should only
  31. modify variables of type "volatile sig_atomic_t". More liberally, most
  32. compilers will allow a signal handler to modify variables of any type, but
  33. they should be declared as "volatile".
  34.  
  35. If the string is not declared as volatile, then the compiler should be
  36. able to assume that the string *is* "inviolate".
  37.  
  38. >The only way a compiler can know the size of a char array is:
  39. >    (a) It is a string literal.
  40. >    (b) It is an array declared at compile time, with dimension(s).
  41.  
  42. Well, a smart compiler might be able to determine that after
  43.     char *s = (chart *) malloc(100);
  44. that s points to a char array of size 100.
  45.  
  46. -- 
  47. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  48. This .signature virus is a self-referential statement that is true - but 
  49. you will only be able to consistently believe it if you copy it to your own
  50. .signature file!
  51.