473,387 Members | 1,517 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,387 software developers and data experts.

Operator new and POD-struct

Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};

int main()
{
new C; // new-initializer is omitted
}

C++98 says (5.3.4 #15):
A new-expression that creates an object of type T initializes that object as
follows:
- If the new-initializer is omitted:
- If T is a (possibly cvqualified) non-POD class type (or array
thereof)...
- Otherwise, the object created has indeterminate value...

And then (9 #4):
A POD-struct is an aggregate class that has no non-static data members of
type pointer to member, non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment operator and no
user-defined destructor.

However, I've found that both MSVC 7.1 and EDG C front 3.6 generate a
constructor call at the new operator invocation. Can anybody explain why
they don't leave an object created unitialized, as written?

Thank you.

--
Unicals Group -- Development Tools for OEMs
http://www.unicals.com
Dec 1 '05 #1
6 2214
Ivan A. Kosarev wrote:
Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};


This is not a POD struct. If it has any user defined methods, it's not
POD type.
Dec 1 '05 #2

Ivan A. Kosarev wrote:
Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};

int main()
{
new C; // new-initializer is omitted
}

C++98 says (5.3.4 #15):
A new-expression that creates an object of type T initializes that object as
follows:
- If the new-initializer is omitted:
- If T is a (possibly cvqualified) non-POD class type (or array
thereof)...
- Otherwise, the object created has indeterminate value...

And then (9 #4):
A POD-struct is an aggregate class that has no non-static data members of
type pointer to member, non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment operator and no
user-defined destructor.

However, I've found that both MSVC 7.1 and EDG C front 3.6 generate a
constructor call at the new operator invocation. Can anybody explain why
they don't leave an object created unitialized, as written?

Thank you.


You need to look at what an aggregate class is defined as.
If you read section 8.5.1, it states the following:
-------------------------------------------------------------------------------------
An aggregate is an array or a class with no user declared constructors,
............
-------------------------------------------------------------------------------------

Your class has a constructor, so it's not an aggregate class, and
doesn't fit the criteria for a POD type as defined in the standard.

Dec 1 '05 #3
Ivan A. Kosarev wrote:
Consider the fragment:

class C { // A POD-struct
It's not POD-struct. Please read the definition of POD-struct more
carefully. A POD-struct is _an_aggregate_ class. The definition of
an aggregate is in 8.5.1/1 and it requires no user-defined c-tor,
explicitly.
public:
C() { puts("C::C()"); }
};

int main()
{
new C; // new-initializer is omitted
}

C++98 says (5.3.4 #15):
It's time you get C++03 (although it didn't change much t
A new-expression that creates an object of type T initializes that object as
follows:
- If the new-initializer is omitted:
- If T is a (possibly cvqualified) non-POD class type (or array
thereof)...
- Otherwise, the object created has indeterminate value...

And then (9 #4):
A POD-struct is an aggregate class that has no non-static data members of ^^^^^^^^^ type pointer to member, non-POD-struct, non-POD-union (or array of such
types) or reference, and has no user-defined copy assignment operator and no
user-defined destructor.

However, I've found that both MSVC 7.1 and EDG C front 3.6 generate a
constructor call at the new operator invocation. Can anybody explain why
they don't leave an object created unitialized, as written?


They are correct. Your 'C' is a non-POD class.

V
Dec 1 '05 #4
Gianni Mariani wrote:
Ivan A. Kosarev wrote:
Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};


This is not a POD struct. If it has any user defined methods, it's not
POD type.


Actually, it's the presence of a user-defined CONSTRUCTOR that makes it
not pod. Regular method functions are OK in PODS.
Dec 1 '05 #5
Ron Natalie wrote:
Gianni Mariani wrote:
Ivan A. Kosarev wrote:
Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};

This is not a POD struct. If it has any user defined methods, it's
not POD type.

Actually, it's the presence of a user-defined CONSTRUCTOR that makes it
not pod. Regular method functions are OK in PODS.


I didn't know that. Now I do. Cool - thanks. Now I can sleep better that
all that code I wrote that violated this rule is actually standard.

Dec 2 '05 #6
Ivan A. Kosarev wrote:
Hello,

Consider the fragment:

class C { // A POD-struct
public:
C() { puts("C::C()"); }
};

int main()
{
new C; // new-initializer is omitted
}
However, I've found that both MSVC 7.1 and EDG C front 3.6 generate a
constructor call at the new operator invocation. Can anybody explain why
they don't leave an object created unitialized, as written?


As noted, it's not a POD, so they have to call C::C( ).

However, leaving an object uninitialized is never mandatory for
compilers.
You simply can't detect whether an object is uninitialized. It the
standard
says the object is uninitialized, any attempt to read it is Undefined
Behavior.
So, if the read always returns 0, then that's just one special case of
UB. If
it always returns 0 except if your boss looks, it's another allowed
case.

Regards,
Michiel Salters

Dec 2 '05 #7

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

Similar topics

4
by: Giorgos P. | last post by:
classname *ptr = new classname(); Any idea why the parenthesis in the end?I thought that " new " worked just fine with the classname alone? Thanks in advance.
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
15
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
13
by: Alex Vinokur | last post by:
Is the any difference between ------ 1 ------ Foo* p = operator new (n * sizeof (Foo)); // Stuff delete p; --------------- and
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
13
by: martyn_dobson | last post by:
this has probably been asked/suggested many timtes before, but i just thought of it. why is there no such thing as something like operator...() imho it would allow us to smooth off one of the...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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,...

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.