From: vombruch@quando.quantum.de (Stefan vom Bruch)
Newsgroups: comp.sys.atari.st.tech
Subject: Re: A "C" question...
Date: 29 Jul 92 13:53:00 GMT
Organization: Quantum Software GmbH, Dortmund, Germany
X-Newsreader: Tin 1.1 PL4

MDORMAN1@ua1vm.ua.edu (Mike Dorman) writes:

: I've got a question about how to do something in "C".
:  
: I'm in the process of writing a C library to make it easier to manage windows
: (some of the nasty stuff like outputting text through a printf()-like
: interface, etc).
:  
: I have a structure that describes large parts of the window (things like its
: current size, the window handle, etc.).  One of the things I've been thinking
: about doing is storing the window name and info fields as part of the structre.
:  
: Now, obviously, I don't want to have them allocated if they're not going to
: be used.  So I was thinking of creating functions wx_name() and wx_info(),
: where the function definition would be something like:
:  
: wx_name(ws,str)
: Window ws;
: char *str;
:  
: I would then like to malloc() memory to hold the string that we've been given
: so that we won't use any more memory than necessary (I have an ulterior motive,
: I want to create a printf() interface to this stuff, too--I have a couple of
: programs that could use it).
:  
: So, what's the best way to declare the variable within the structure?  Is
: merely making it a char * sufficient, or do I need to make it static char *?
:  
: I never have been able to figure out just what the difference is in practical
: terms.  If someone could explain, I'd be deeply indebted.
:  
: Mike.


(1) Have you tried declaring the member of the struct as static?
    This should be impossible.
    The difference between static and non-static variables is that
    non-static variables just exist on the stack (as long as the called
    function is active). When you leave the function the space for them
    is "recycled" and their value therefore lost.
    Static variables are like global variables because they exist as long
    as the program is running and their value is never lost. They can
    however only be accessed in the function in which they are defined
    (as opposed to global variables). So when you call the function again	
    you can be sure that they have the same value as after the last call
    of that function.

    As the char-pointer isn't a variable but only part of a variable 
    (namely the struct Window ws), it can't be made static.

    Static variables are very rare (on the atari anyway), and I don't
    think you'll need them in this program.

(2)
Assuming that the struct Window looks something like this:

	typedef struct {
			.
			.
			char	*name;
			char	*info;
			.
			.
	} Window;

The function wx_name could look like this:

int wx_name( ws, str )
Window *ws;		/** use pointer to struct **/
char   *str;
{
	ws->name = (char*) malloc( strlen( str ) + 1 );  /* add 1 for 0-Byte */

	strcpy( ws->name, str );
}

You should pass a pointer to the struct to the function because passing the
struct itself will either not work at all or every single member of the
struct will be passed to the function (pushed onto the stack) instead of
just the single address, which is of course a lot slower. (Maybe this was
just a typo?)

Remember to free the individual pointers ( free( ws->name ) ) before freeing
the struct or you will have a lot of zombie-strings using up small chunks of
memory that cannot be allocated again.

Hope this helps, Stefan

-- 
 _--_|\            Stefan vom Bruch                         vombruch@quantum.de
/      | ______________________________________________________________________
\.--._/ (_      /  Quantum GmbH, Emil-Figge-Str.83, W-4600 Dortmund 50, Germany
_____v____) |/ /_)
From: jensk@hpbbrd.bbn.hp.com (Jens Kilian)
Date: Wed, 22 Jul 1992 10:34:20 GMT
Subject: Problems with (MiNT-)BASH and GNU configure scripts
Organization: HP Mechanical Design Division