portable?

G

Gibby Koldenhof

Hiya,

Is the following portable, meaning the line:

bar -> my_free(bar);

or is this asking for trouble? (i.e. freeing the thing that is
pointing to the free call).

Cheers,
Gibby

---- test.c, compile with gcc -ansi -pedantic -Wall test.c -o test

#include <stdio.h>
#include <stdlib.h>

typedef struct {
void * (*my_malloc)(size_t);
void (*my_free)(void *);
} foo;

int main(void)
{
foo * bar = malloc(sizeof * bar);

bar->my_malloc=malloc;
bar->my_free=free;

bar -> my_free(bar->my_malloc(10));

bar -> my_free(bar);

return 0;
}
 
J

Jeremy Yallop

Gibby said:
Is the following portable, meaning the line:

bar -> my_free(bar);

or is this asking for trouble? (i.e. freeing the thing that is
pointing to the free call).

It's fine. The value of `bar' is only used before it's freed (once as
an argument and once to determine which function to call).

Jeremy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top