 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jakob Bieling Guest
|
Posted: Sun Mar 07, 2004 11:37 am Post subject: Q: Legal use? |
|
|
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!
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Mar 07, 2004 2:48 pm Post subject: Re: Legal use? |
|
|
"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote...
| Quote: | Please have a look at the following snippet:
struct base
{
int* i;
base () { i = new int (); }
~base () { delete i; }
|
Once you at least have read the Rule of Three, there would be red lights
and buzzers going off in your head: if you need to define a destructor,
you most likely need to define a copy constructor and an assignment op.
It may not have relevance here, but I can't pass it up.
| Quote: | };
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!");
|
You mean
derived *p = new (13) derived;
strcpy(p->str, "Hello World!");
, of course...
| Quote: | }
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
|
Why do you ask about UB? What's your suspicions? If they are related
to the use of 'strcpy' and giving it a pointer to the first element of
a one-char array while copying 12 characters there, you may be right
because there seems to be nothing in the Standard that requires a specific
layout of a non-POD object (and your 'derived' is not a POD). So, for all
we know, the 'base' subobject of 'derived' may as well follow the 'str'
array of one char in 'derived'.
Then again, I can be mistaken...
Victor
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sun Mar 07, 2004 4:06 pm Post subject: Re: Legal use? |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | "Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote...
Please have a look at the following snippet:
struct base
{
int* i;
base () { i = new int (); }
~base () { delete i; }
Once you at least have read the Rule of Three, there would be red lights
and buzzers going off in your head: if you need to define a destructor,
you most likely need to define a copy constructor and an assignment op.
It may not have relevance here, but I can't pass it up.
|
Yip, I know about it and do keep it in mind when writing production
code. But since this was just an example, I left it out. I added the whole
allocation/c'tor/d'tor stuff to imply that 'base' is not a POD, in case that
might be of relevance to the answer of my question.
| Quote: | };
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!");
You mean
derived *p = new (13) derived;
strcpy(p->str, "Hello World!");
, of course...
|
Uh, ya, sorry.
| Quote: | }
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
Why do you ask about UB? What's your suspicions? If they are related
to the use of 'strcpy' and giving it a pointer to the first element of
a one-char array while copying 12 characters there, you may be right
because there seems to be nothing in the Standard that requires a specific
layout of a non-POD object (and your 'derived' is not a POD). So, for all
we know, the 'base' subobject of 'derived' may as well follow the 'str'
array of one char in 'derived'.
Then again, I can be mistaken...
|
Right, that was exactly what I had in mind. Though, it works for me, I
wanted to know how chances are that it will work with a future compiler I
may use someday. Another concern of mine was padding. Assuming the compiler
used prepends the base class object, can I be sure that there are no padding
bytes between the 'str' member and the 'appended' memory? If not, will this
even be a problem when copying? In my case, copying is not an issue, but I
am just curious.
Thanks for the help!
--
jb
(replace y with x if you want to reply by e-mail)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|