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

self initialization (??)

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

Jul 23 '05 #1
6 1756
On 22 Jun 2005 03:08:34 -0700, "Srini" <sr*********@gmail.com> wrote:
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?

--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #2
> 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
Jul 23 '05 #3
> 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

Jul 23 '05 #4
Srini wrote:
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.
Jul 23 '05 #5
Jacques Labuschagne wrote:
Srini wrote:
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. ]
Jul 23 '05 #6
Ron Natalie wrote:
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.
Jul 23 '05 #7

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

Similar topics

2
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. ...
13
by: Frans Englich | last post by:
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
49
by: valentin tihomirov | last post by:
fDeleted = false; uint jobId; foreach (Struct struct in structures) { if (struct.type == JOB) { jobId = struct.id; if (struct.dataType == STATUS) fDeteted = (struct.data & STATUS_DELETED) !=...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
6
by: wxPythoner | last post by:
Hello! I have trouble understanding something in this code snippet: class TextReader: """Print and number lines in a text file.""" def __init__(self, file): self.file = file
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...
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...

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.