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