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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!utzoo!telly!druid!darcy
  3. From: darcy@druid.uucp (D'Arcy J.M. Cain)
  4. Subject: Re: Where are literals stored?
  5. Message-ID: <1992Nov21.142614.11609@druid.uucp>
  6. Date: Sat, 21 Nov 1992 14:26:14 GMT
  7. References: <1992Nov18.233739.2335@den.mmc.com>
  8. Organization: D'Arcy Cain Consulting
  9. Lines: 35
  10.  
  11. richard@crowded-house.den.mmc.com (Richard Armstrong) writes:
  12. >Are literals always stored in the same location in memory? (IBM-PC,Borland)
  13.  
  14. There is a switch in the options menu to turn this on and off
  15.  
  16. >For instance, is the string a stored in the same place in the following
  17. >two declarations?:
  18. >static char a[]="ABC"
  19. > [...]
  20. >char a[]="ABC";
  21.  
  22. Regardless of whether the compiler merges duplicate literal strings, that
  23. is not what you are talking about here.  These variables are not pointers
  24. to strings but are arrays of characters.  In the first case the array is
  25. filled at compile time and is a unique memory location not shared by other
  26. locations.  In the second case the array is filled at run time and may be
  27. implemented by a form of strcpy() and in that case the actual string copied
  28. may be shared with something else.  However even this is not required.  The
  29. copy in this case could be optimized to an assignment of a long to the
  30. location used by a and so there is no actual string in memory to be shared.
  31.  
  32. In the following example the strings may be shared (if the proper switches
  33. are set)
  34.  
  35. static char *a = "ABC";
  36. ...
  37. char *a = "ABC";
  38.  
  39. Of course the variables should probably be declared const in this case.
  40.  
  41. -- 
  42. D'Arcy J.M. Cain (darcy@druid.com)  |
  43. D'Arcy Cain Consulting              |   There's no government
  44. Toronto, Ontario, Canada            |   like no government!
  45. +1 416 424 2871          DoD#0082   |
  46.