[prev in list] [next in list] [prev in thread] [next in thread] 

List:       fuse-devel
Subject:    Re: [fuse-devel] example of function to remove unnecessary slashes.
From:       Roger Willcocks <roger () filmlight ! ltd ! uk>
Date:       2009-12-28 15:53:57
Message-ID: 38D0FF28-B22D-4D0E-ACF3-596516A236F8 () filmlight ! ltd ! uk
[Download RAW message or body]


On 28 Dec 2009, at 13:15, Stef Bon wrote:

> 2009/12/28 Roger Willcocks <roger@filmlight.ltd.uk>:
>> Your function leaks memory (it never frees tmpchar). You could just  
>> return
>> tmpchar and be done with it (taking care to remove any trailing  
>> slash),
>> instead of realloc-ing 'path' and copying into it.
>
> Ok, that tmpchar needs to be freed? I thought that every var defined
> in a function is automaticcaly
> freed. If I have to free tmpchar here, than I have to free every
> pointer assigned to an address where data is stored right?

Getting increasingly off topic for a fuse mailing list, but you need  
to free any memory which you allocate using malloc and friends. In  
your case strdup called malloc, and tmpchar points at the allocated  
memory, so you need to free it again at some point.

>
> Questions: what does the latest line do?
>
> if (q > p + 2 && q[-2] == '/') q[-2] = '\0';
>

Oops, well spotted. That was a thinko:

void unslash(char *path)
{
	char *p = path;
	char *q = path;
         while ((*q++ = *p++) != 0)
                 if (q[-1] == '/')
                         while (*p == '/')
                                 p++;
         if (q > path + 2 && q[-2] == '/')
                 q[-2] = '\0';
}


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
fuse-devel mailing list
fuse-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fuse-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic