self initialization (??)

S

Srini

Pardon me if this is trivial.

I think I read somewhere that statements like this cause undefined
behavior.

int n = n;

How is the above statement different from

int n(n);

This above statement gives an error saying that 'n' is undeclared. I
saw the assembly listing for this following piece of code. Using g++
3.3.3

int x = 10;
int n = x;
int m(x);

The assembly listing was something like this...

movl $10, -4(%ebp)
movl -4(%ebp), %eax
movl %eax, -8(%ebp)
movl -4(%ebp), %eax
movl %eax, -12(%ebp)

I see that for both of these, the assembly code generated is the same.
May be its different for class objects. This whole thing brings doubts
in my mind whether I must check for self initialization in copy
constructors. Please share your thoughts on this.

Regards,
Srini
 
B

Bob Hairgrove

Pardon me if this is trivial.

I think I read somewhere that statements like this cause undefined
behavior.

int n = n;

How is the above statement different from

int n(n);

I think it is a matter of scope. If there is a variable n defined
within the enclosing (outer) scope, the local n will use the outer n
to initialize itself with. Afterwards, the local n hides the outer n.
But I am not 100% sure that this is well-defined behavior.

If there is no n visible in the outer scope, the code is ill-formed.
This above statement gives an error saying that 'n' is undeclared. I
saw the assembly listing for this following piece of code. Using g++
3.3.3

int x = 10;
int n = x;
int m(x);

The assembly listing was something like this...

movl $10, -4(%ebp)
movl -4(%ebp), %eax
movl %eax, -8(%ebp)
movl -4(%ebp), %eax
movl %eax, -12(%ebp)

I see that for both of these, the assembly code generated is the same.
May be its different for class objects. This whole thing brings doubts
in my mind whether I must check for self initialization in copy
constructors. Please share your thoughts on this.

It is not possible. In a copy constructor, you always have a valid
object to construct from because the argument to the copy c'tor is a
const reference. Whether or not that object has been correctly
initialized or not is another story; but there IS an object, otherwise
the compiler couldn't generate a reference to it. And since the object
being constructed doesn't exist yet, there is no need to check.

Are you more concerned with self assignment, perhaps?
 
S

Stephen Howe

This whole thing brings doubts
in my mind whether I must check for self initialization in copy
constructors.

No you should not.
Doing either

SomeClass x = x;
SomeClass x(x);

is ill-formed.
If the copy-constructor is invoked here, what would you do inside if you did
detect self intialisation?
Leave it as a zombie object? There is nothing good you can do.

Stephen Howe
 
S

Srini

SomeClass x = x;
SomeClass x(x);
is ill-formed.
If the copy-constructor is invoked here, what would you do inside if you did
detect self intialisation?
Leave it as a zombie object? There is nothing good you can do.

Mostly this is a compiler issue. Because with aCC, both

int n = n;
int m(m);

went thru fine. There were no problems with int but with string I got a
seg fault. Even if the issue is the compiler, is there nothing in the
standard that allows/disallows both? What I feel is that both of them
must have consistent behavior.

Regards,
Srini
 
J

Jacques Labuschagne

Srini said:
seg fault. Even if the issue is the compiler, is there nothing in the
standard that allows/disallows both? What I feel is that both of them
must have consistent behavior.

Both seem to be disallowed by 8.5/2:
"[variables] of namespace scope can be initialized by arbitrary
expressions involving literals and previously declared variables and
functions."

In
int n = n;
the variable n is neither a literal nor a previously declared variable.

J.
 
R

Ron Natalie

Jacques said:
Srini said:
seg fault. Even if the issue is the compiler, is there nothing in the
standard that allows/disallows both? What I feel is that both of them
must have consistent behavior.


Both seem to be disallowed by 8.5/2:
"[variables] of namespace scope can be initialized by arbitrary
expressions involving literals and previously declared variables and
functions."

In
int n = n;
the variable n is neither a literal nor a previously declared variable.

J.

It is a previously declared variable. The first lines of 3.3.1 "Point
of declaration" address this specifically:

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. ]
 
J

Jacques Labuschagne

Ron said:
It is a previously declared variable. The first lines of 3.3.1 "Point
of declaration" address this specifically:

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

Far out. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top