home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18588 < prev    next >
Encoding:
Text File  |  1992-12-31  |  1.2 KB  |  41 lines

  1. Path: sparky!uunet!gatech!rutgers!cmcl2!sales!wlee
  2. From: wlee@sales.GBA.NYU.EDU (Wai-Shing Lee)
  3. Newsgroups: comp.lang.c++
  4. Subject: Static global initialization outside of main() legal?
  5. Message-ID: <34727@sales.GBA.NYU.EDU>
  6. Date: 31 Dec 92 06:39:39 GMT
  7. Organization: NYU
  8. Lines: 31
  9.  
  10.  
  11.    I want to initialize a static global at run-time but I don't want to
  12. have to have a programmer using my modules to have to call some init() funciton
  13. at the begining of thier main().
  14.  
  15. I can get this to compile under Borland C++ 3.1, but only if it is compiled as
  16. C++, it doesn't like it if it is compiled as plain Borland C.
  17.  
  18. extern "C"
  19. {
  20. #include <alloc.h>
  21. }
  22.  
  23. static int *pointer ;
  24. static int initialized=findvideomemory() ;
  25.  
  26. static int makepointer(void)
  27. {
  28.     pointer=(int*)calloc(10, sizeof(char)) ;
  29. }
  30.  
  31. The variable "initialized" is junk, but I could just as easily return a value
  32. from makepointer(), but I have more stuff going on in makepointer() than
  33. appears here.
  34.  
  35. I've traced it and sure enough makepointer() is called and pointer is assigned
  36. a value before main() even starts.  Is the portable, legal.  I didn't look
  37. to hard in my C++ book, but there weren't any obvious answers.  Is this
  38. C++, C or something I should stay away from?
  39.  
  40. Thanks
  41.