473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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)
Jul 22 '05 #1
2 1071
"Jakob Bieling" <ne*****@gmy.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.
};

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...
}

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
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:CCG2c.497031$I06.5336338@attbi_s01...
"Jakob Bieling" <ne*****@gmy.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.
};

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.
}

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)
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pakkocool | last post by:
¿Como ganar dinero en internet? Lee atentamente el siguiente texto, es super interesante y te hara ganar muchos dolares si sigues las instrucciones y le pones empeño: Hace unos días que...
3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
7
by: Nice | last post by:
Hi. I read somewhere that in XML the tag <xml> is not legal. But in W3C specs I can't find this statement. Does anyone know about it ?
0
by: mykidisgollum | last post by:
Greetings, I have code which prints a document who's attributes are saved as flags in a database. One of the those attributes is letter or legal. When I am printing, I use the following...
7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
6
by: aj | last post by:
I currently have 2 official DB2 Workgroup Edition licenses for my 2 v8 production servers. I also have tech support/software upgrade agreements in place for both servers. I am interested in...
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
1
by: Wiktor Zychla | last post by:
is it legal to distribute axshdocvw, shdocvw and mshtml.dll with my application? is it legal to distribute Microsoft.mshtml.dll from 'Primary Interop Assemblies' folder (I assume it comes with...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
2
by: Army1987 | last post by:
Is this program legal C89? /* no headers included */ int main(void) { if (sizeof (exit(0), 0), ((void (* )(int))&exit)( (puts((const char *)"hello, world"), 0) ), 0) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.