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

  1. Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!spacebbs!ted.jensen
  2. From: ted.jensen@spacebbs.com (Ted Jensen) 
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Where are literals stored?
  5. Message-ID: <14542.610.uupcb@spacebbs.com>
  6. Date: 22 Nov 92 16:48:00 GMT
  7. Distribution: world
  8. Organization: SPACE BBS - Menlo Park, CA - 10 Lines + 4gB - 415-323-4193
  9. Reply-To: ted.jensen@spacebbs.com (Ted Jensen) 
  10. Lines: 88
  11.  
  12.  
  13. Some time back richard@crowded-house.den.mmc.com (Richard
  14. Armstrong) wrote:
  15.  
  16. RA>For instance, is the string a stored in the same place in the following
  17. RA>two declarations?:
  18. RA>static char a[]="ABC"
  19. RA> [...]
  20. RA>char a[]="ABC";
  21.  
  22. I have been following the resultant thread for some time now and,
  23. while I haven't kept all copies of all replies, it seems to me
  24. that some of them were rather erroneous.  For example, someone
  25. stated, if I recall correctly, that these were not string
  26. literals since they were character arrays rather than being
  27. defined as pointers as in:
  28.  
  29. char *ptr = "ABC";
  30.  
  31. However, according to K&R2 p194 under "A2.6 String Literals"
  32.  
  33. "A string literal, also called a string constant, is a sequence
  34. of characters surrounded by double quotes, as in "..."."
  35.  
  36. "A string has type 'array of characters' and storage class static
  37. and is initialized with the given characters."
  38.  
  39. Thus it seems to me that in both cases above, that which resides
  40. to the right of the equal sign is a string literal and as such is
  41. stored in the data area.  And that this is true regardless of
  42. whether a[] is a global or automatic variable.  In fact, using
  43. TC++ and the Borland's Turbo Debugger this was easy enough to
  44. verify in the following code:
  45.  
  46. void my_func(void)
  47. {
  48.   static char a[] = "ABC";
  49.   char b[] = "ABC";
  50.   printf("%s\n",a);
  51.   printf("%s\n",b);
  52. }
  53.  
  54.  
  55. The actual strings themselves were, in both cases, stored in the
  56. data segment.  The difference between 'a' and 'b' was not in how the
  57. string literal was stored, but in the code which manipulated them.
  58. In the case of array a[], the compiled code uses 'a' much
  59. as a pointer which gets stored on the stack and points to the
  60. string in the data segment.  In the case of array b[] the
  61. compiled code, on entering the function at run time, copies the
  62. string from the data segment to the stack and 'b' takes on the
  63. value of a pointer pointing to the string now on the stack.  This
  64. was not clear from some of the replies which, IMHO, made it sound
  65. like that in the case of b[] the string literal ABC was not
  66. stored in the data segment.
  67.  
  68. Richard Armstrong's original question was:
  69.  
  70. "Are literals always stored in the same location in memory?
  71. (IBM-PC,Borland)".
  72.  
  73. And I think the answer is, yes.  All literals are stored in the
  74. data segment of the code.
  75.  
  76. While, IMHO, his question was not in regard to the merging of
  77. duplicate strings, someone correctly pointed out that such
  78. merging is normally only done under the following circumstances:
  79.  
  80.     a)  If the compiler supports it.
  81.  
  82.     b)  If the switch on the compiler is in the right position.
  83.  
  84.     c)  If the declaration/definition is in the form of a
  85.     pointer instead of an array, e.g.
  86.  
  87.     char *a = "ABC";          instead of
  88.  
  89.     char a[] = "ABC";
  90.  
  91. Hope this helps!  And, if the above is either incorrect or adding
  92. to the confusion:  a) my apologies in advance, and b) I would
  93. appreciate further clarification.
  94.  
  95. Ted Jensen  Redwood City, Calif.
  96.  
  97.  
  98.  * SLMR 2.1a * 
  99.                                                                              
  100.