473,399 Members | 4,177 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,399 software developers and data experts.

Is the following code legal?

#include <new>

class T
{
};

int main()
{
T t = t;
T u(u);
T v;
new (&v) T(v);
}

Kiuhnm
Mar 18 '06 #1
30 2097
Kiuhnm wrote:
#include <new>

class T
{
};

int main()
{
T t = t;
T u(u);
T v;
new (&v) T(v);
}

Kiuhnm


Have you tried compiling this?

Does you compiler give you errors?

Why do you think that is?

What do you expect each line to do?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 18 '06 #2
Ben Pope ha scritto:
Have you tried compiling this?
Yes, but it is irrelevant. How can I be sure that my compiler is
completely standard-compliant?
What do you expect each line to do?


It is irrelevant.
All I would like to know is whether the code, according to the standard,
produces a defined behavior or not.

Kiuhnm
Mar 18 '06 #3
Kiuhnm wrote:
Ben Pope ha scritto:
Have you tried compiling this?


Yes, but it is irrelevant. How can I be sure that my compiler is
completely standard-compliant?


You can't.
What do you expect each line to do?


It is irrelevant.
All I would like to know is whether the code, according to the standard,
produces a defined behavior or not.


Not.

Mar 18 '06 #4
In article <44***********************@reader1.news.tin.it>,
Kiuhnm <"kiuhnm03["@]yahoo.it> wrote:
Ben Pope ha scritto:
Have you tried compiling this?


Yes, but it is irrelevant. How can I be sure that my compiler is
completely standard-compliant?
What do you expect each line to do?


It is irrelevant.
All I would like to know is whether the code, according to the standard,
produces a defined behavior or not.


T t = t;
T u(u);

The above two lines will compile but the behavior is undefined.
T v;
new (&v) T(v);

The above two lines produce defined behavior.

Please, why do you ask?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 18 '06 #5
Rolf Magnus ha scritto:
Not.


Why?
I read 3.3.1 and 3.8 in the c++ iso, but that did not clarify my doubts.

Kiuhnm
Mar 18 '06 #6
Daniel T. ha scritto:
T t = t;
T u(u);

The above two lines will compile but the behavior is undefined.
And what about
int x = x;
?
Please, why do you ask?


I am just curious.

Kiuhnm
Mar 18 '06 #7
Kiuhnm" <"kiuhnm03[ wrote:
Daniel T. ha scritto:
T t = t;
T u(u);

The above two lines will compile but the behavior is undefined.


And what about
int x = x;
?


What's the difference from

T t = t;

do you see here?

V
--
Please remove capital As from my address when replying by mail
Mar 19 '06 #8
Kiuhnm wrote:
#include <new>

class T
{
};

int main()
{
T t = t;
T u(u);
T v;
new (&v) T(v);
}


What is your particular question anyway? What do you suspect to be
illegal and why? The obvious violation is double construction of an
object which is illegal. However, you put other stuff into the
article, too, which might indicate that you are actually looking for
other sources of "illegal code".
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 19 '06 #9
Dietmar Kuehl wrote:
Kiuhnm wrote:
#include <new>

class T
{
};

int main()
{
T t = t;
T u(u);
T v;
new (&v) T(v);
}
What is your particular question anyway?


He is asking because we have been discussing that in our national C++
newsgroup but did not come to a definitive conclusion.

There are more variations on the same theme and they may or may not
change the validity of those constructs.
What do you suspect to be
illegal and why?
It depends who you ask to. If we had any consensus we would not even ask.

For as much as we can say the placement new construct is perfectly defined.

That is:

void foo()
{
class T {} v;
new (&v) T(v);
}

is perfectly legal.
The obvious violation is double construction of an
object which is illegal.
Not. The standard does not say you cannot construct twice. And does not
guarantee that the number of constructions matches the number of
destructions. If you reuse the memory of a live object, that object
lifetime ends without the construction being called and it is explicitly
legal to do so. See 3.8#4
However, you put other stuff into the
article, too, which might indicate that you are actually looking for
other sources of "illegal code".


In fact 3.8 is no help in the other cases.

We know that

struct T {
void *p;
T(){}
T(T*pp){ p=pp; if (pp==this) cout << "wow!\n"; }
};

void foo()
{
T t=&t;
T u(&u);
}

is perfectly defined (see 3.3.1#1 and 3.8#5)

But the wording in 3.8, specifically the first item of the list in
3.8#5, together with 3.8#1 seem to imply that

struct T {
int i;
T(){}
T(const T&r){i=r.i;}
};

void foo()
{
T t=t;
T u(u);
}

is undefined behavior (cannot access r.i until the lifetime has begun,
and that happens at the end of the constructor).

And this is apparently incongruous with the fact that

void foo()
{
int i=i;
int j(j);
}

is perfectly valid as in 3.3.1#1
Mar 19 '06 #10
Victor Bazarov ha scritto:
What's the difference from

T t = t;

do you see here?


T may not be a built-in type.

Anyway, my question was subtler than you think:
excerpt from c++ iso/iec 14882:

3.3.1 Point of declaration

The /point of declaration/ for a name is immediately after its complete
declarator (clause 8) and before its /initializer/ (if any), except as
noted below.
[Example:

int x = 12;
{ int x = x; }

Here the second x is initialized with its own (indeterminate) value.]
<<<

Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk).

Kiuhnm
Mar 19 '06 #11
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk).


Using an indeterminate value, for anything, is formally UB.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '06 #12
Kiuhnm wrote:
Victor Bazarov ha scritto:
What's the difference from

T t = t;

do you see here?


T may not be a built-in type.

Anyway, my question was subtler than you think:
excerpt from c++ iso/iec 14882:
>>>

3.3.1 Point of declaration

The /point of declaration/ for a name is immediately after its complete
declarator (clause 8) and before its /initializer/ (if any), except as
noted below.
[Example:

int x = 12;
{ int x = x; }

Here the second x is initialized with its own (indeterminate) value.]
<<<

Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk).


It doesn't say that. It just says that, when the right hand side is
evaluated, the name 'x' already exists. It doesn't say anything about the
behavior of initializing x with an indeterminate value.

Mar 19 '06 #13
Rolf Magnus ha scritto:
It doesn't say that. It just says that, when the right hand side is
evaluated, the name 'x' already exists. It doesn't say anything about the
behavior of initializing x with an indeterminate value.


3.3.1#1:
"[...]Here the second x is initialized with its own (indeterminate) value."

Kiuhnm
Mar 19 '06 #14
Alf P. Steinbach ha scritto:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
compiler cannot format your hard disk).

Using an indeterminate value, for anything, is formally UB.


I am just copying it (we cannot overload operator= for built-in types).

Kiuhnm
Mar 19 '06 #15
Kiuhnm ha scritto:
I am just copying it (we cannot overload operator= for built-in types).


Please, forget "overload". I meant "provide a user-defined".

Kiuhnm
Mar 19 '06 #16
* Kiuhnm:
Alf P. Steinbach ha scritto:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
compiler cannot format your hard disk).

Using an indeterminate value, for anything, is formally UB.


I am just copying it (we cannot overload operator= for built-in types).


Formally, that's using it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '06 #17
Kiuhnm wrote:
Rolf Magnus ha scritto:
It doesn't say that. It just says that, when the right hand side is
evaluated, the name 'x' already exists. It doesn't say anything about the
behavior of initializing x with an indeterminate value.


3.3.1#1:
"[...]Here the second x is initialized with its own (indeterminate)
value."


And again, it doesn't say that initializing it with an indeterminate value
is legal, just that it is done here, because x is known before it is
initializied, so the name can be used for the initializer.

Mar 19 '06 #18
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk).


Using an indeterminate value, for anything, is formally UB.


I did a search for "indeterminate" in my electronic version of the standard,
and I did not find a statement to that extend. I would be grateful for
chapter and verse.

It appears that almost all uses (and most definitely all interesting uses)
of indeterminate values lead to undefined behavior per the catch all clause
in [1.3.12]: "Undefined behavior may also be expected when this
International Standard omits the description of any explicit definition of
behavior." However, one could argue that in the case of

int x = x;

the standard actually provides an explicit definition of behavior, namely
that the variable x is initialized in such a way that its value after
initialization is undefined.
Best

Kai-Uwe Bux
Mar 19 '06 #19
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
compiler cannot format your hard disk).


Using an indeterminate value, for anything, is formally UB.


Can you point me to where in the standard it says so?

AFAIK it is defined behavior (with indeterminate result) for every POD
type except when dereferencing a pointer.
Mar 19 '06 #20
* Kai-Uwe Bux:
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk).

Using an indeterminate value, for anything, is formally UB.


I did a search for "indeterminate" in my electronic version of the standard,
and I did not find a statement to that extend. I would be grateful for
chapter and verse.


§4.1/1 "... if the object is uninitialized, a program that necessitates
this conversion [to rvalue] has undefined behavior".

I don't think it ever was /meant/ to cover int values, but, it does.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '06 #21
* AnalogFile:
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
compiler cannot format your hard disk).


Using an indeterminate value, for anything, is formally UB.


Can you point me to where in the standard it says so?

AFAIK it is defined behavior (with indeterminate result) for every POD
type except when dereferencing a pointer.


Now I just replied to the same question posted by Kai-Uwe... :-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '06 #22
Kai-Uwe Bux wrote:
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant compiler
cannot format your hard disk). Using an indeterminate value, for anything, is formally UB.

.... It appears that almost all uses (and most definitely all interesting uses)
of indeterminate values lead to undefined behavior per the catch all clause
in [1.3.12]: "Undefined behavior may also be expected when this
International Standard omits the description of any explicit definition of
behavior." However, one could argue that in the case of


No. "omits the description of any explicit definition of behavior" does
not cover indeterminate values.

int x;

creates and x with indeterminate value. But it's defined behavior.

You can even use the x.

int x;
cout << x;

writes a value to standard output. Not guarantees on what value, except
it's in the range of ints. It's defined behavior.

If it was undefined a compiler could power off your machine with that
code. But it is defined and the only thing a conforming compiler can do
is write a value to standard output.

Mar 19 '06 #23
Rolf Magnus wrote:
Kiuhnm wrote:
Rolf Magnus ha scritto:
It doesn't say that. It just says that, when the right hand side is
evaluated, the name 'x' already exists. It doesn't say anything about the
behavior of initializing x with an indeterminate value.

3.3.1#1:
"[...]Here the second x is initialized with its own (indeterminate)
value."


And again, it doesn't say that initializing it with an indeterminate value
is legal, just that it is done here, because x is known before it is
initializied, so the name can be used for the initializer.


When the standard says: "this happens" it means that "this happens" is
the legal, defined, behavior. Undefined behavior is when the standard
says something is undefined or when it says nothing at all.

Also "indeterminate value" is a defined behavior for initialization in
many other cases.
Mar 19 '06 #24
Rolf Magnus ha scritto:
And again, it doesn't say that initializing it with an indeterminate value
is legal, just that it is done here, because x is known before it is
initializied, so the name can be used for the initializer.


I cannot believe a formal example contains illegal code (if not
specifically noted).
Anyway, where does the standard say that code is illegal?

Kiuhnm
Mar 19 '06 #25
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
Alf P. Steinbach wrote:
* Kiuhnm:
Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
compiler
cannot format your hard disk).
Using an indeterminate value, for anything, is formally UB.


I did a search for "indeterminate" in my electronic version of the
standard,
and I did not find a statement to that extend. I would be grateful for
chapter and verse.


§4.1/1 "... if the object is uninitialized, a program that necessitates
this conversion [to rvalue] has undefined behavior".

I don't think it ever was /meant/ to cover int values, but, it does.


Does not apply. Quote the whole text!

"If the object to which the lvalue refers is not
an object of type T and is not an object of a type derived from T, or if
the object is uninitialized, a program
that necessitates this conversion has undefined behavior."

only if a conversion is required because the object is not already of
the required type, then the object must be initialized.

In

int x=x;

no conversion is required and that text does not apply.

Mar 19 '06 #26
* AnalogFile:
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
Alf P. Steinbach wrote:

* Kiuhnm:
> Then "int x = x;" is /not/ undefined (e.g. a standard-compliant
> compiler
> cannot format your hard disk).
Using an indeterminate value, for anything, is formally UB.

I did a search for "indeterminate" in my electronic version of the
standard,
and I did not find a statement to that extend. I would be grateful for
chapter and verse.


§4.1/1 "... if the object is uninitialized, a program that
necessitates this conversion [to rvalue] has undefined behavior".

I don't think it ever was /meant/ to cover int values, but, it does.


Does not apply. Quote the whole text!

"If the object to which the lvalue refers is not
an object of type T and is not an object of a type derived from T, or if
the object is uninitialized, a program
that necessitates this conversion has undefined behavior."

only if a conversion is required because the object is not already of
the required type, then the object must be initialized.


No, sorry, that's just wishfulness on your part.

This paragraph mainly applies to conversion T -> T, where you have an
lvalue of type T and end up with an rvalue of type T.

There is a bit more, type-wise, regarding classes, but for primitive
types like int "the type of the rvalue is the cv-unqualified version of
T", i.e., the conversion is then cv T -> T for any cv-qualification.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '06 #27
AnalogFile wrote:
Rolf Magnus wrote:
Kiuhnm wrote:
Rolf Magnus ha scritto:
It doesn't say that. It just says that, when the right hand side is
evaluated, the name 'x' already exists. It doesn't say anything
about the behavior of initializing x with an indeterminate value.
3.3.1#1:
"[...]Here the second x is initialized with its own (indeterminate)
value."
And again, it doesn't say that initializing it with an indeterminate
value is legal, just that it is done here, because x is known before
it is initializied, so the name can be used for the initializer.


When the standard says: "this happens" it means that "this happens" is
the legal, defined, behavior.


None of what the Standard says in "Notes" or "Examples" is normative.
Undefined behavior is when the standard
says something is undefined or when it says nothing at all.

Also "indeterminate value" is a defined behavior for initialization in
many other cases.


Yes, but the fact that you need to _extract_ that value from an object
makes the behaviour undefined.

V
--
Please remove capital As from my address when replying by mail
Mar 19 '06 #28
Victor Bazarov wrote:
....
Also "indeterminate value" is a defined behavior for initialization in
many other cases.


Yes, but the fact that you need to _extract_ that value from an object
makes the behaviour undefined.


If what you say was true, a conforming C++ compiler could translate the
following code:

#include <iostream>
int main()
{
int x;
std::cout << x;
}

to code that formats you disk.
I think it cannot.

And I see nowere in the standard any wording that says that extracting
an undetermined value from a POD is undefined. But I do find wordings
that say that extracting a value is defined and that initializing with
undetermined value is defined.
Mar 20 '06 #29

AnalogFile wrote:
Victor Bazarov wrote:
...
Also "indeterminate value" is a defined behavior for initialization in
many other cases.


Yes, but the fact that you need to _extract_ that value from an object
makes the behaviour undefined.


If what you say was true, a conforming C++ compiler could translate the
following code:

#include <iostream>
int main()
{
int x;
std::cout << x;
}

to code that formats you disk.
I think it cannot.

And I see nowere in the standard any wording that says that extracting
an undetermined value from a POD is undefined. But I do find wordings
that say that extracting a value is defined and that initializing with
undetermined value is defined.


I believe that C++ in this case has inherited its behaviour from C.
Without wanting to be taken to serious, I believe that accessing
uninitialized storage in the form of char or unsigned integral types
should not be allowed to trap. Accessing it as other types (thus
including plain int) is allowed to trap.

/Peter

Mar 20 '06 #30
* AnalogFile:

#include <iostream>
int main()
{
int x;
std::cout << x;
Error: use of possibly undefined operator<<.
Error: use of uninitialized variable.
}
[snip]
And I see nowere in the standard any wording that says that extracting
an undetermined value from a POD is undefined.
§4.1/1.

But I do find wordings
that say that extracting a value is defined and that initializing with
undetermined value is defined.


No, you don't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 20 '06 #31

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

Similar topics

18
by: Adrian B. | last post by:
Does anyone know of a framework or library that will enable me to use publish/subscribe comms? I want to create a server (using Python) running on a Unix box that will accept client connections...
17
by: George Sakkis | last post by:
Is there a general way of injecting code into a function, typically before and/or after the existing code ? I know that for most purposes, an OO solution, such as the template pattern, is a cleaner...
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...
20
by: vb | last post by:
hi all, I have to compare an address of structure with an absolute address(this address signifies the start of a memory component). Now my question is the follwong code leagal:- #include...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
10
by: sara | last post by:
Hi - I have a report that is 14 columnar sub-reports (Line up: Position- holders in each of our 14 locations - Manager, Assistant Manager, Receiving, Office, etc). I output directly to PDF...
7
by: eduzea | last post by:
I have some code that compiles with gcc but fails with MS VC 8. I would like to know which one is following the C++ standard. istream& istr = false ? cin : ( * new ifstream("test.txt")) ; ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.