
char *  
crypt_getpwdfilename()
{ 
    
  static char *pfnam = NULL;
  
  if (!pfnam)
  { 
    int bufsize;
    bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
    pfnam = (char *) palloc(bufsize); 
    spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
  }
  
  return pfnam;
}

Why the check for '!ipfnam'?  Seems useless since we are setting it to
NULL the line before...no?

Marc G. Fournier                                
Systems Administrator @ hub.org 
primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org 


Mon, 14 Dec 1998 02:21:07 -0400 (AST)
The Hermit Hacker <scrappy@hub.org>
Does this make sense:
> Why the check for '!ipfnam'?  Seems useless since we are setting it to
> NULL the line before...no?

Does a static char get initialized each time through, or just once at
allocation? Is this setting a "persistant value" for pfnam? I would
guess so, but ymmv...

                      - Tom

Mon, 14 Dec 1998 07:29:51 +0000
"Thomas G. Lockhart" <lockhart@alumni.caltech.edu>
Re: [HACKERS] Does this make sense:
> 
> char *  
> crypt_getpwdfilename()
> { 
>     
>   static char *pfnam = NULL;
>   
>   if (!pfnam)
>   { 
>     int bufsize;
>     bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
>     pfnam = (char *) palloc(bufsize); 
>     spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
>   }
>   
>   return pfnam;
> }
> 
> Why the check for '!ipfnam'?  Seems useless since we are setting it to
> NULL the line before...no?

Actually, no.  We are declaring it as static, so the first time the
function is called, it is set to NULL.  After that, it is not
initialized for each function call because a static local variable's
value is kept between function calls.  It is like a global variable in
its duration, but in local scope.

This is an old trick to run the initialization code only the first time
the function is called.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Mon, 14 Dec 1998 09:55:56 -0500 (EST)
Bruce Momjian <maillist@candle.pha.pa.us>
Re: [HACKERS] Does this make sense:
Then <scrappy@hub.org> spoke up and said:
> 
> char *  
> crypt_getpwdfilename()
> { 
>     
>   static char *pfnam = NULL;
>   
>   if (!pfnam)
>   { 
>     int bufsize;
>     bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
>     pfnam = (char *) palloc(bufsize); 
>     spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
>   }
>   
>   return pfnam;
> }
> 
> Why the check for '!ipfnam'?  Seems useless since we are setting it to
> NULL the line before...no?

static variables should be thought of as globals with only local
scope.  This variable is allocated in the BSS area rather than on the
stack at runtime.  The initialization is performed only once (usually
at compile time), and the value is retained from execution to
execution.  Basically, it always returns the previously computed
filename except for the very first call.

-- 
=====================================================================
| JAVA must have been developed in the wilds of West Virginia.      |
| After all, why else would it support only single inheritance??    |
=====================================================================
| Finger geek@andrew.cmu.edu for my public key.                     |
=====================================================================
14 Dec 1998 10:21:12 -0500
geek+@cmu.edu
Re: [HACKERS] Does this make sense:
>
> >
> > char *
> > crypt_getpwdfilename()
> > {
> >
> >   static char *pfnam = NULL;
> >
> >   if (!pfnam)
> >   {
> >     int bufsize;
> >     bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
> >     pfnam = (char *) palloc(bufsize);
> >     spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
> >   }
> >
> >   return pfnam;
> > }
> >
> > Why the check for '!ipfnam'?  Seems useless since we are setting it to
> > NULL the line before...no?
>
> Actually, no.  We are declaring it as static, so the first time the
> function is called, it is set to NULL.  After that, it is not
> initialized for each function call because a static local variable's
> value is kept between function calls.  It is like a global variable in
> its duration, but in local scope.
>
> This is an old trick to run the initialization code only the first time
> the function is called.

    But  is  it  good  then  to use palloc() instead of malloc()?
    Anything palloc()'d is thrown away when the memory context in
    which  it  is made get's destroyed. So you have to care about
    the memory context in which the call is made. If  under  some
    (but  not  all)  circumstances  the FIRST call is made in the
    wrong mcxt, the pointer maybe get's corrupted later.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #



Mon, 14 Dec 1998 17:20:50 +0100 (MET)
jwieck@debis.com (Jan Wieck)
Re: [HACKERS] Does this make sense:
> >
> > >
> > > char *
> > > crypt_getpwdfilename()
> > > {
> > >
> > >   static char *pfnam = NULL;
> > >
> > >   if (!pfnam)
> > >   {
> > >     int bufsize;
> > >     bufsize = strlen(DataDir) + strlen(CRYPT_PWD_FILE) + 2;
> > >     pfnam = (char *) palloc(bufsize);
> > >     spprintf(pfnam, bufsize, "%s/%s", DataDir, CRYPT_PWD_FILE);
> > >   }
> > >
> > >   return pfnam;
> > > }
> > >
> > > Why the check for '!ipfnam'?  Seems useless since we are setting it to
> > > NULL the line before...no?
> >
> > Actually, no.  We are declaring it as static, so the first time the
> > function is called, it is set to NULL.  After that, it is not
> > initialized for each function call because a static local variable's
> > value is kept between function calls.  It is like a global variable in
> > its duration, but in local scope.
> >
> > This is an old trick to run the initialization code only the first time
> > the function is called.
> 
>     But  is  it  good  then  to use palloc() instead of malloc()?
>     Anything palloc()'d is thrown away when the memory context in
>     which  it  is made get's destroyed. So you have to care about
>     the memory context in which the call is made. If  under  some
>     (but  not  all)  circumstances  the FIRST call is made in the
>     wrong mcxt, the pointer maybe get's corrupted later.

Oops, yep, I didn't see that.  palloc is bad to use in this context. 
Malloc should be used, Marc.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Mon, 14 Dec 1998 11:43:38 -0500 (EST)
Bruce Momjian <maillist@candle.pha.pa.us>
Re: [HACKERS] Does this make sense: