home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12961 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.6 KB  |  68 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!utcsri!geac!r-node!pseudo!mjn
  3. From: mjn@pseudo.uucp (Murray Nesbitt)
  4. Subject: Re: dynam mem alloc
  5. Organization: Private system in Toronto, ON
  6. Date: Sun, 30 Aug 1992 10:33:11 GMT
  7. Message-ID: <MJN.92Aug30023311@pseudo.uucp>
  8. References: <1992Aug21.191422.28985@news.acns.nwu.edu> <MJN.92Aug26015009@pseudo.uucp> <17gasuINNi8@early-bird.think.com>
  9. Sender: mjn@pseudo.uucp (Murray Nesbitt)
  10. Lines: 56
  11.  
  12.  
  13. barmar@think.com (Barry Margolin) writes:
  14.  
  15. >In article <MJN.92Aug26015009@pseudo.uucp> mjn@pseudo.uucp (Murray Nesbitt) writes:
  16. >>ecm@casbah.acns.nwu.edu (Edward Malthouse) writes:
  17. >>>int *x;
  18. >>>if(n == NULL) /* memory has not been allocated */
  19. >
  20. >Where did "n" come from?
  21. >
  22. >>>This works fine as long as the pointer variable was declared as global or in
  23. >>>main().  If it is declared elsewhere this statement does not work.  For
  24. >>>example:
  25. >
  26. >>Yes, there is a portable way.  Use the storage-class specifier
  27. >>``static''.
  28. >
  29. >That's not a good answer, since it has more effects than just solving this
  30. >problem.  Besides being initialized to 0 automatically, static variables
  31. >also retain their value across calls.  So if he frees the object before
  32. >returning from the function, the next time he comes in he'll think that the
  33. >memory has already been allocated (or worse, since doing anything with a
  34. >pointer to freed storage is a no-no).
  35.  
  36. You're telling me this like I don't already know what ``static'' does.
  37.  
  38. The way I understood his article was that he was looking for a way to
  39. ensure a pointer variable with block scope would 1) be initialized to
  40. NULL, and 2) retain its value between invocations of the block, so
  41. that he would be able to malloc() some memory only in the first
  42. invocation of the block, and on subsequent invocations be able to
  43. reliably test whether memory had already been allocated to the
  44. pointer.
  45.  
  46. >
  47. >He should just provide an explicit initializer:
  48. >
  49. >int *x = NULL;
  50. >
  51.  
  52. No, he shouldn't, because each time the block is executed, x will be
  53. set to NULL.  If all he was looking for was a way to initialize a
  54. pointer to NULL when entering a block of code, do you *really* think
  55. he wouldn't have thought of this method?
  56.  
  57. >BTW, the fact that it is automatically initialized in main() is just a
  58. >feature of that particular implementation.  There is no required default
  59. >initialization of automatic or register variables, so you should always
  60. >provide an initializer if you care.
  61.  
  62. I think it's more likely that the variable in question just happened
  63. to have the initial value 0, as opposed to being explicitly
  64. initialized by the implementation.
  65.  
  66. -- 
  67. Murray
  68.