Ramkumar Ramachandra wrote on Thu, Sep 23, 2010 at 01:12:52 +0530:
> Daniel Shahaf writes:
> > Choosing path=NULL to represent "the root" seems odd to me (that's not
> > how we usually represent it), but that's for another day.
> 
> I think I'm using "" to represent the root somewhere, but I'll have to
> check this. I'll figure something out during the cleanup.
> 

I see.

> > > True. However, abspath is a `char *` with no memory allocated to it: I
> > > want whatever its value is to be allocated in `pool` since it's passed
> > > around.
> > 
> > I think you've got your pointers confused.  Can you drop the pstrdup(),
> > see that nothing breaks, and then we can discuss why nothing broke?
> 
> Hehe, I wouldn't be surprised if this is the case -- I've been working
> with APR pools day and night now :p
> 
> Okay, I'll drop this during the cleanup. Do explain this to me though :)

abspath is a pointer, lives on the stack, and is passed by value (like
any old int) in function calls.  What you allocate from the pool is the
value it points to --- i.e., the two bytes of "/".

However, literal string constants are static strings --- they are loaded
into memory from the binary image (ever tried 'strings /usr/bin/svn'?)
and stay there until that is unloaded --- while pool-allocated strings
allocated on the heap, and stays there until the pool is cleared.
The other difference is that literal strings are not writable (they are
const char *) while pool-allocate strings are mutable (non-const char *).

So, the only reasons you'd have to duplicate a static string is if the
static string lives in a library which you know you'll unload[1] or if
you need a non-const char * for some reason.  In your case, 'abspath' is
a const char *, so neither of these cases applies.

Okay?

Daniel
(waiting to hear from you that dropping the strdup() causes a segfault)


[1] Thanks, dev@ archives.

Reply via email to