pgrep/pkill
Tito
farmatito at tiscali.it
Sat Sep 29 19:44:39 UTC 2007
On Friday 28 September 2007 21:14:45 Ralf Friedl wrote:
> Tito wrote:
> > Maybe this version could be more useful.....
> >
> > void xfree(void *ptr)
> > {
> > /* sets the pointer to NULL
> > after it has been freed.*/
> > void **pp = (void **)ptr;
> >
> > if (*pp == NULL) return;
> >
> > free(*pp);
> > *pp = NULL;
> > }
> >
> >
> You only modify the argument of xfree, not the original pointer.
>
> What you wanted to write is:
>
> void xfree(void **pp)
> {
> /* sets the pointer to NULL after it has been freed.*/
> if (*pp == NULL) return;
> free(*pp);
> *pp = NULL;
> }
>
>
> Regards
> Ralf Friedl
>
Hi,
I'm sorry but i am not able to compile your code.
The only version that compiles seems not to work....
maybe i'm overlooking something very obvious.
OTOH i'm pretty sure my versions works as this code snippet shows:
compile with:
gcc -Wall test.c -o test
and run:
./test
test1
test 2
p is NULL
Ciao,
Tito
-----------------------------------------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
void xfree(void **pp)
{
/* sets the pointer to NULL after it has been freed.*/
if (*pp == NULL) return;
free(*pp);
*pp = NULL;
}
void myxfree(void *ptr)
{
/* sets the pointer to NULL after it has been freed.*/
void **pp = (void **)ptr;
if (*pp == NULL) return;
free(*pp);
*pp = NULL;
}
int main(int argc, char **argv)
{
/* Sets the pointer to NULL so no double-free fault */
char *p;
#if 0
char ** p2p;
p = malloc(10);
/* warning: passing argument 1 of ‘xfree’ from incompatible pointer type */
xfree(&p);
xfree(&p);
p = malloc(10);
/* warning: passing argument 1 of ‘xfree’ from incompatible pointer type */
xfree(p);
xfree(p);
p = malloc(10);
/* warning: passing argument 1 of ‘xfree’ from incompatible pointer type */
p2p = &p;
xfree(p2p);
p2p = &p;
xfree(p);
#endif
puts("test 1");
/* Compiles but fails on p == NULL test */
p = malloc(10);
xfree((void **)p);
if (p == NULL) puts("p is NULL");
xfree((void **)p);
p = malloc(10);
puts("test 2");
myxfree(&p);
if (p == NULL) puts("p is NULL");
myxfree(&p);
return 0;
}
---------------------------------------------------------------------------------------------------------
More information about the busybox
mailing list