J
Jakob Bieling
Hi,
Please have a look at the following snippet:
struct base
{
int* i;
base () { i = new int (); }
~base () { delete i; }
};
struct derived : public base
{
int j;
char str [1];
void* operator new (size_t sz, size_t a) { return malloc (sz + a); }
void operator delete (void* p, size_t a) { free (p); }
};
int main ()
{
derived* = new (13) derived;
strcpy (derived->str, "Hello World!");
}
The idea is to have the 'derived' class contain a fixed length (*)
char-array, without having to call 'operator new' a second time. Does the
above produce undefined behaviour?
(*) but length only known at runtime
Thanks in advance!
Please have a look at the following snippet:
struct base
{
int* i;
base () { i = new int (); }
~base () { delete i; }
};
struct derived : public base
{
int j;
char str [1];
void* operator new (size_t sz, size_t a) { return malloc (sz + a); }
void operator delete (void* p, size_t a) { free (p); }
};
int main ()
{
derived* = new (13) derived;
strcpy (derived->str, "Hello World!");
}
The idea is to have the 'derived' class contain a fixed length (*)
char-array, without having to call 'operator new' a second time. Does the
above produce undefined behaviour?
(*) but length only known at runtime
Thanks in advance!