home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!spacebbs!ted.jensen
- From: ted.jensen@spacebbs.com (Ted Jensen)
- Newsgroups: comp.lang.c
- Subject: Re: Where are literals stored?
- Message-ID: <14542.610.uupcb@spacebbs.com>
- Date: 22 Nov 92 16:48:00 GMT
- Distribution: world
- Organization: SPACE BBS - Menlo Park, CA - 10 Lines + 4gB - 415-323-4193
- Reply-To: ted.jensen@spacebbs.com (Ted Jensen)
- Lines: 88
-
-
- Some time back richard@crowded-house.den.mmc.com (Richard
- Armstrong) wrote:
-
- RA>For instance, is the string a stored in the same place in the following
- RA>two declarations?:
- RA>static char a[]="ABC"
- RA> [...]
- RA>char a[]="ABC";
-
- I have been following the resultant thread for some time now and,
- while I haven't kept all copies of all replies, it seems to me
- that some of them were rather erroneous. For example, someone
- stated, if I recall correctly, that these were not string
- literals since they were character arrays rather than being
- defined as pointers as in:
-
- char *ptr = "ABC";
-
- However, according to K&R2 p194 under "A2.6 String Literals"
-
- "A string literal, also called a string constant, is a sequence
- of characters surrounded by double quotes, as in "..."."
-
- "A string has type 'array of characters' and storage class static
- and is initialized with the given characters."
-
- Thus it seems to me that in both cases above, that which resides
- to the right of the equal sign is a string literal and as such is
- stored in the data area. And that this is true regardless of
- whether a[] is a global or automatic variable. In fact, using
- TC++ and the Borland's Turbo Debugger this was easy enough to
- verify in the following code:
-
- void my_func(void)
- {
- static char a[] = "ABC";
- char b[] = "ABC";
- printf("%s\n",a);
- printf("%s\n",b);
- }
-
-
- The actual strings themselves were, in both cases, stored in the
- data segment. The difference between 'a' and 'b' was not in how the
- string literal was stored, but in the code which manipulated them.
- In the case of array a[], the compiled code uses 'a' much
- as a pointer which gets stored on the stack and points to the
- string in the data segment. In the case of array b[] the
- compiled code, on entering the function at run time, copies the
- string from the data segment to the stack and 'b' takes on the
- value of a pointer pointing to the string now on the stack. This
- was not clear from some of the replies which, IMHO, made it sound
- like that in the case of b[] the string literal ABC was not
- stored in the data segment.
-
- Richard Armstrong's original question was:
-
- "Are literals always stored in the same location in memory?
- (IBM-PC,Borland)".
-
- And I think the answer is, yes. All literals are stored in the
- data segment of the code.
-
- While, IMHO, his question was not in regard to the merging of
- duplicate strings, someone correctly pointed out that such
- merging is normally only done under the following circumstances:
-
- a) If the compiler supports it.
-
- b) If the switch on the compiler is in the right position.
-
- c) If the declaration/definition is in the form of a
- pointer instead of an array, e.g.
-
- char *a = "ABC"; instead of
-
- char a[] = "ABC";
-
- Hope this helps! And, if the above is either incorrect or adding
- to the confusion: a) my apologies in advance, and b) I would
- appreciate further clarification.
-
- Ted Jensen Redwood City, Calif.
-
-
- * SLMR 2.1a *
-
-