home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / atari / st / tech / 4174 < prev    next >
Encoding:
Text File  |  1992-07-29  |  3.8 KB  |  105 lines

  1. Path: sparky!uunet!mcsun!Germany.EU.net!unido!quando!vombruch
  2. From: vombruch@quando.quantum.de (Stefan vom Bruch)
  3. Newsgroups: comp.sys.atari.st.tech
  4. Subject: Re: A "C" question...
  5. Message-ID: <1992Jul29.135300.12646@quando.quantum.de>
  6. Date: 29 Jul 92 13:53:00 GMT
  7. References: <16832DDF3.MDORMAN1@ua1vm.ua.edu>
  8. Organization: Quantum Software GmbH, Dortmund, Germany
  9. Lines: 93
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:
  13.  
  14. : I've got a question about how to do something in "C".
  15. :  
  16. : I'm in the process of writing a C library to make it easier to manage windows
  17. : (some of the nasty stuff like outputting text through a printf()-like
  18. : interface, etc).
  19. :  
  20. : I have a structure that describes large parts of the window (things like its
  21. : current size, the window handle, etc.).  One of the things I've been thinking
  22. : about doing is storing the window name and info fields as part of the structre.
  23. :  
  24. : Now, obviously, I don't want to have them allocated if they're not going to
  25. : be used.  So I was thinking of creating functions wx_name() and wx_info(),
  26. : where the function definition would be something like:
  27. :  
  28. : wx_name(ws,str)
  29. : Window ws;
  30. : char *str;
  31. :  
  32. : I would then like to malloc() memory to hold the string that we've been given
  33. : so that we won't use any more memory than necessary (I have an ulterior motive,
  34. : I want to create a printf() interface to this stuff, too--I have a couple of
  35. : programs that could use it).
  36. :  
  37. : So, what's the best way to declare the variable within the structure?  Is
  38. : merely making it a char * sufficient, or do I need to make it static char *?
  39. :  
  40. : I never have been able to figure out just what the difference is in practical
  41. : terms.  If someone could explain, I'd be deeply indebted.
  42. :  
  43. : Mike.
  44.  
  45.  
  46. (1) Have you tried declaring the member of the struct as static?
  47.     This should be impossible.
  48.     The difference between static and non-static variables is that
  49.     non-static variables just exist on the stack (as long as the called
  50.     function is active). When you leave the function the space for them
  51.     is "recycled" and their value therefore lost.
  52.     Static variables are like global variables because they exist as long
  53.     as the program is running and their value is never lost. They can
  54.     however only be accessed in the function in which they are defined
  55.     (as opposed to global variables). So when you call the function again    
  56.     you can be sure that they have the same value as after the last call
  57.     of that function.
  58.  
  59.     As the char-pointer isn't a variable but only part of a variable 
  60.     (namely the struct Window ws), it can't be made static.
  61.  
  62.     Static variables are very rare (on the atari anyway), and I don't
  63.     think you'll need them in this program.
  64.  
  65. (2)
  66. Assuming that the struct Window looks something like this:
  67.  
  68.     typedef struct {
  69.             .
  70.             .
  71.             char    *name;
  72.             char    *info;
  73.             .
  74.             .
  75.     } Window;
  76.  
  77. The function wx_name could look like this:
  78.  
  79. int wx_name( ws, str )
  80. Window *ws;        /** use pointer to struct **/
  81. char   *str;
  82. {
  83.     ws->name = (char*) malloc( strlen( str ) + 1 );  /* add 1 for 0-Byte */
  84.  
  85.     strcpy( ws->name, str );
  86. }
  87.  
  88. You should pass a pointer to the struct to the function because passing the
  89. struct itself will either not work at all or every single member of the
  90. struct will be passed to the function (pushed onto the stack) instead of
  91. just the single address, which is of course a lot slower. (Maybe this was
  92. just a typo?)
  93.  
  94. Remember to free the individual pointers ( free( ws->name ) ) before freeing
  95. the struct or you will have a lot of zombie-strings using up small chunks of
  96. memory that cannot be allocated again.
  97.  
  98. Hope this helps, Stefan
  99.  
  100. -- 
  101.  _--_|\            Stefan vom Bruch                         vombruch@quantum.de
  102. /      | ______________________________________________________________________
  103. \.--._/ (_      /  Quantum GmbH, Emil-Figge-Str.83, W-4600 Dortmund 50, Germany
  104. _____v____) |/ /_)
  105.