472,780 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 2198
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.