home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17058 < prev    next >
Encoding:
Text File  |  1992-11-23  |  2.6 KB  |  67 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ocsmd!ted
  3. From: ted@ocsmd.ocs.com (Ted Scott)
  4. Subject: Allocating string space via constant (was Re: Where are literals)
  5. Message-ID: <By6tGC.CvM@ocsmd.ocs.com>
  6. Sender: news@ocsmd.ocs.com
  7. Organization: Online Computer Systems, Inc.  Germantown, Md., 20874
  8. X-Newsreader: Tin 1.1 PL5
  9. References: <27583@dog.ee.lbl.gov>
  10. Date: Mon, 23 Nov 1992 21:09:47 GMT
  11. Lines: 54
  12.  
  13. Chris Torek (torek@horse.ee.lbl.gov) wrote:
  14. : In article <By0vzy.4Ly@ocsmd.ocs.com> I wrote:
  15. : >Ok, *I'm* confused here. Does this mean that if I do something like:
  16. : > *p[1] = ' '; 
  17. : >q will now point to "A C" ??
  18. : Since p[1] is a value of type `char', *p[1] is illegal: the operand of
  19. : a unary `*' indirection operator must have type `pointer to T', for some
  20. : valid type T.
  21. Um, Er, Ah...
  22. : Assuming you meant
  23. :     p[1] = ' ';
  24. Thanks for DWIM!
  25.  
  26.  
  27. The reasons for my question was that I have run accross *alot* of code
  28. that allocates buffers as:
  29.  
  30. char *foo ="This string constant looks big enough to hold the *REAL* data";
  31.  
  32. and then goes on to muck with the contents of foo! I hadn't realized 
  33. the inherent dangers of this practice, and apparently neither did 
  34. Mitchell Waite and Steven Prata when they wrote their primer. (p416)
  35. (Yes I know what primers are *for*) They say only that the storage 
  36. obtained with this mechanism is static, nothing aobut it may be part
  37. of a read-only segment.
  38.  
  39. Probably other C authors haven't explored these corners as well.
  40.  
  41. : >or will the above assignment yield a SEGV? or what? (I know probably what :)
  42. : Yes, `or what' :-) .  In ANSI C, the effect is explicitly undefined;
  43. : *anything* can happen.  Your computer might suddenly quote King Lear
  44. : (`old Tom's a-cold!'). 
  45. I can only hope ;)
  46.  
  47. : <Really good explanation of why this is a bad thing deleted>
  48.  
  49. This really seems to me to be a design error in the language, or the Standard.
  50. Can someone in the know explain how this came about? I mean that if I declare
  51. two seperate pointers to an initialization string that coincedentally are 
  52. identical I may or may not end up aliasing one of them, depending on the
  53. mood of the compiler writer, or maybe even the linker developer. 
  54.  
  55. Further, it is quite possible that two occurrences of the same program
  56. that share code space under a multitasking OS could point to the same memory
  57. because they share the same string initializer! What a Feature!
  58.  
  59. IOCC here I come ... <to the tune of Calif. here I come>
  60.  
  61. --
  62.  
  63. -Ted Scott          
  64. tscott@ocsmd.ocs.com           I was told that I'm a P.C. person:
  65. (301) 601-2252                               Politically Challenged, that is.
  66.