Hello,
Is there any difference between:
A* p = new A;
and
A* p = new A();
?
I know it must have been dicussed here, but just
not easy to search. BTW, I think
A a;
is alike new A as far as constructor call concerned
(default construtor call?) Right?
And
A a();
is just a function declaration?
Thanks and best regards,
Wenjie 13 2108
"Wenjie" <go****@yahoo.com> wrote... Is there any difference between: A* p = new A; and A* p = new A(); ?
There is no difference for classes, there is a difference
for POD: the former leaves the object uninitialised, the
latter zero-initialises it.
I know it must have been dicussed here, but just not easy to search. BTW, I think A a; is alike new A as far as constructor call concerned (default construtor call?) Right?
It's a declaration (and a definition in certain circumstances)
of an object 'a' of type A. If it's a definition, the object
is default-initialised, which for a class means that its c-tor
is invoked, yes. And A a(); is just a function declaration?
Yes, it is.
Victor
Wenjie wrote: Hello,
Is there any difference between: A* p = new A; and A* p = new A(); ?
Yes, there is a difference in certain circumstances.
It has to do with POD's (POD = plain old data structure,
basically a class or a struct built from only builtin types
and having no member functions, in short: a struct as it was
ment to be in C)
new A
creates a new POD-object of type A and leaves all members uninitialized
new A()
creates a new POD-object of type A but 0-initializes all members.
A a(); is just a function declaration?
Yep. That's a pitfall.
--
Karl Heinz Buchegger kb******@gascad.at
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:vf************@corp.supernews.com... There is no difference for classes,
non-POD classes (but you knew that).
there is a difference for POD: the former leaves the object uninitialised, the latter zero-initialises it.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:3E***************@gascad.at... Yes, there is a difference in certain circumstances. It has to do with POD's (POD = plain old data structure, basically a class or a struct built from only builtin types and having no member functions, in short: a struct as it was ment to be in C)
That's not the definition of POD.
POD classes can't have
non-static data of non-pod type (other non-POD structs or unions, pointer-to-members or references).
user-defined constructors, destructors, or copy-assignment operators
base classes
protected or private non-static data members
virtual functions.
They can however have other member functions not specifically mentioned above.
In short, a class whose data behaves identically to a C struct.
"Wenjie" <go****@yahoo.com> wrote... "Victor Bazarov" <v.********@attAbi.com> wrote in message
news:<vf************@corp.supernews.com>... "Wenjie" <go****@yahoo.com> wrote... Is there any difference between: A* p = new A; and A* p = new A(); ?
There is no difference for classes, there is a difference for POD: the former leaves the object uninitialised, the latter zero-initialises it.
The default constructor (whether user defined or not) is called in both cases like for a definition "A a;" right?
If it's not defined ("trivial"), how would it be called? Then "zero-init" is only for simplifying the anwser, since what I know a default constructor could be written as:
Zero-initialisation is only performed for POD. As soon as
you define a constructor, it's not a POD any longer.
class A { public: A(): a_(12) {} //...
private: int a_; };
I know it must have been dicussed here, but just not easy to search. BTW, I think A a; is alike new A as far as constructor call concerned (default construtor call?) Right?
It's a declaration (and a definition in certain circumstances) of an object 'a' of type A. If it's a definition, the object is default-initialised, which for a class means that its c-tor is invoked, yes.
And A a(); is just a function declaration?
Yes, it is.
Victor
Thanks,
"Victor Bazarov" <v.********@attAbi.com> writes: "Wenjie" <go****@yahoo.com> wrote... "Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vf************@corp.supernews.com>... > "Wenjie" <go****@yahoo.com> wrote... > > Is there any difference between: > > A* p = new A; > > and > > A* p = new A(); > > ? > > There is no difference for classes, there is a difference > for POD: the former leaves the object uninitialised, the > latter zero-initialises it.
This is all fine. I just got into a heated debate with a colleague
about operator new[] though:
Q1. Is operator new[] allowed to take a non-default constructor for
a user-defined type? Example:
class A { public: A(int, int, int); };
A* p = new A[100](1, 2, 3);
My position is yes. GCC 3.2 apparently agrees.
Q2. For a POD type, is operator new[] allowed to take non-default
initializers? Example:
int* p = new int[100](1);
My inclination is to say yes. But GCC is not on my side this time.
For int* p = new int(1), *p is initialized to 1. With operator new[]
though, it did not.
Q3. For a POD type, should the following two statements both perform
zero initialization:
int *p = new int[100];
int *p = new int[100]();
My understanding is the first one doesn't and the second one does, just
like for operator new.
We've consulted the standard but our interpretations are different.
My basic stance is what's quoted for operator new should apply as well to
operator new[].
--Yu
"Yu Cao" <yu***@alumnae.caltech.edu> wrote... "Victor Bazarov" <v.********@attAbi.com> writes:
"Wenjie" <go****@yahoo.com> wrote... "Victor Bazarov" <v.********@attAbi.com> wrote in messagenews:<vf************@corp.supernews.com>... > "Wenjie" <go****@yahoo.com> wrote... > > Is there any difference between: > > A* p = new A; > > and > > A* p = new A(); > > ? > > There is no difference for classes, there is a difference > for POD: the former leaves the object uninitialised, the > latter zero-initialises it.
This is all fine. I just got into a heated debate with a colleague about operator new[] though:
Q1. Is operator new[] allowed to take a non-default constructor for a user-defined type?
If the user-defined type does not have a default constructor,
the program that tries to dynamically allocate an array of such
type is ill-formed. See Standard
Example:
class A { public: A(int, int, int); }; A* p = new A[100](1, 2, 3);
My position is yes.
The Standard doesn't agree with you. See 5.3.4/15. Only default
initialisation is allowed for arrays.
GCC 3.2 apparently agrees.
Doesn't prove anything. Q2. For a POD type, is operator new[] allowed to take non-default initializers?
Nope.
Example:
int* p = new int[100](1);
My inclination is to say yes.
Oh, well...
But GCC is not on my side this time. For int* p = new int(1), *p is initialized to 1. With operator new[] though, it did not.
Right. PODs are left uninitialised. Q3. For a POD type, should the following two statements both perform zero initialization:
int *p = new int[100]; int *p = new int[100]();
The first one does not perform any initialisation. My understanding is the first one doesn't and the second one does, just like for operator new.
We've consulted the standard but our interpretations are different. My basic stance is what's quoted for operator new should apply as well to operator new[].
It doesn't. But if you'd like to make a suggestion, you should
definitely do so in comp.std.c++.
Victor
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:vf************@corp.supernews.com... The default constructor (whether user defined or not) is called in both cases like for a definition "A a;" right?
If it's not defined ("trivial"), how would it be called?
As far as the standard is concerend it behaves as if one was automatically
inserted by the compiler.
Ron Natalie wrote:
[...] int *p = new int[100]; int *p = new int[100]();
The latter is ILLEGAL. The former provides no initialization. Another major defect of the C++ language.
You just love things like
struct IntGarbage { IntGarbage() {} int garbage; };
I guess. ;-)
regards,
alexander.
In article <vf************@corp.supernews.com>,
Victor Bazarov <v.********@attAbi.com> wrote: "Ron Natalie" <ro*@sensor.com> wrote... "Yu Cao" <yu***@alumnae.caltech.edu> wrote in message news:bd**********@naig.caltech.edu... > Q2. For a POD type, is operator new[] allowed to take non-default > initializers? Example: > > int* p = new int[100](1); // LINE AA
What you wrote above is illegal for ANY type. > > Q3. For a POD type, should the following two statements both perform > zero initialization: > > int *p = new int[100]; // LINE BB > int *p = new int[100](); // LINE CC
The latter is ILLEGAL.
Really? How would you explain that it works in Comeau? Another "fucked-up Comeau extension"?
Just so there is no confusion, I labelled the above statements.
LINE BB and LINE CC are both valid, legal, and well-defined,
and usually mean different things. LINE AA is not allowed.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
"Ron Natalie" <ro*@sensor.com> wrote... "Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vf************@corp.supernews.com... The default constructor (whether user defined or not) is called in both cases like for a definition "A a;" right?
If it's not defined ("trivial"), how would it be called?
As far as the standard is concerend it behaves as if one was automatically inserted by the compiler.
How can one verify that "as if"? There is no code to put
the breakpoint or a debugging output statement, is there?
That's what my question was about.
"Greg Comeau" <co****@panix.com> wrote in message
news:bd**********@panix5.panix.com... In article <vf************@corp.supernews.com>, Victor Bazarov <v.********@attAbi.com> wrote:"Ron Natalie" <ro*@sensor.com> wrote... "Yu Cao" <yu***@alumnae.caltech.edu> wrote in message news:bd**********@naig.caltech.edu... > Q2. For a POD type, is operator new[] allowed to take non-default > initializers? Example: > > int* p = new int[100](1); // LINE AA
What you wrote above is illegal for ANY type. > > Q3. For a POD type, should the following two statements both perform > zero initialization: > > int *p = new int[100]; // LINE BB > int *p = new int[100](); // LINE CC
The latter is ILLEGAL.
Really? How would you explain that it works in Comeau? Another "fucked-up Comeau extension"?
Just so there is no confusion, I labelled the above statements. LINE BB and LINE CC are both valid, legal, and well-defined, and usually mean different things. LINE AA is not allowed.
The whole idea for my "Really" was to coerce Ron into looking it
up in the Standard in an attempt to make him substantiate his
claims. It has become customary here (in response to some newbies'
"it works this way here so it's right" statements) to actually
quote the Standard (not that it has the desired effect every time),
so I would expect both Ron and you not to use "it's legal" or "it's
ILLEGAL" without proof. And, mind you, *I* don't need that proof,
most of the time. It's the other posters who read the forum I am
concerned about. :-)
Victor
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:VHhLa.41883$Fy6.12552@sccrnsc03... How can one verify that "as if"? There is no code to put the breakpoint or a debugging output statement, is there? That's what my question was about.
Debugging is not part of the language.
The constructor is treated AS IF it exists. Whether the compiler
emits code or calls it is an implementation detail. Read 12.1/5
and 12.1/7 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |