explicit constructor call on casted void *

From: Timo Qvist (timo.qvist_at_telia.com)
Date: 11/30/04


Date: Tue, 30 Nov 2004 20:23:04 GMT

Hi, I know there is a proper way to do this but I find myself with a
void pointer returned from a malloc( sizeof( class T ) ) which gives me
a pointer to a memory area of the size of class T. The thing is, I need to
use
this data as a class object, but the constructor hasnt been called which
means I get
a zeroed out vptr table; so I tried this...

void foo(int a) {
T *newTptr = (T *)allocptr(); // returns a void * so casted...
    newTptr->T::T( a );
}

The second line explicitly calling the constructor actually works with MS
Visual C++ 7,
but not gcc 3.3.3 .... basically get a parse error.. I was kinda surprised
it worked
and am wondering whether someone can help me get it to work on gcc?

Thanks!
/tq



Relevant Pages

  • Re: large files: when ubiquitous?
    ... You don't need to malloc any ... The point of this particular subthread is that gcc is ... have written some broken code, ... And now you complain about gcc being so smart, ...
    (comp.os.linux.development.system)
  • conflicting types for malloc
    ... I cannot install mpack error "conflicting types for malloc" ... checking for gcc... ... checking whether the C compiler is a cross-compiler... ...
    (comp.os.linux.setup)
  • Survey of sizes of types
    ... this is a bit GCC related, but there's an ISO C99 program hidden to ... # usage: mailsz [gccname] ... printf ("c %zd\n", sizeof sc); ...
    (comp.lang.c.moderated)
  • Re: p = kmalloc(sizeof(*p), )
    ... Not only does that obviously generate a lot of extra code, ... > least a few versions of gcc. ... a struct that would generate a warning whenever sizeof is calculated - i.e. ...
    (Linux-Kernel)
  • Re: C Strings
    ... sizeof int and long are 4 bytes each and so is the sizeof a pointer. ... printf to expect. ... gcc -ansi produces ... be what is expected, or garbage and may vary across systems, compilers ...
    (comp.lang.c)