On Tue, Jun 2, 2009 at 10:58 PM, Paul Brook
<address@hidden> wrote:
> I think you're afraid of the following case instead:
>
> foo* ptr;
> ptr = QEMU_MEW(bar); => compiler will complain that 'ptr' is not a bar*
>
>But this is not possible with the first version anyway.
Rubbish. Works perfectly.
I think there is a misunderstanding here, what I mean is that:
#define QEMU_NEW(ptr) (ptr) = malloc(sizeof(*ptr))
foo* ptr;
QEMU_NEW(ptr);
Will never try to allocate sizeof(bar) bytes, anyway.
The rest is taste/personal preference, and there is no point fighting about it.
As I said, I just want to say that abort() on qemu_malloc(0) is not the shiny good thing
some people are expecting, and that there are better ways to fight potential developer errors.
cat test.c <<EOF
#include <stdlib.h>
#define QEMU_NEW(type) (type*)malloc(sizeof(type))
typedef struct { int x; } foo;
typedef struct { double y; } bar;
int main(int argc, char **argv)
{
foo *ptr = QEMU_NEW(bar);
return 0;
}
EOF
gcc test.c
test.c: In function ‘main’:
test.c:7: initialization from incompatible pointer type
Paul