By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

about copy constructor and destructor

P: n/a
Hello experts!

I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
AccountForStudent global;
global = create(300);

return 0;
}

Here is what happen according to a book.:
1. Program execution enters the body of create(), and the variable local in
initialized by calling the constructor.
2. A temporary variable, temp, created by the compiler, is initialized by
calling the compiler generated copy constructor because no other is defined
and using the value of the local variable.
3. An assignment operator is called, which assign the value of the temporary
variable temp to global.
4.The destructor for the temporary variable temp is called.
5. create() terminates.

Now to my question: temp and local will share the same Student object
because we have a shallow copy.
When the destructor for temp executes the Student object is deallocated and
therefore you now cannot access the Student object through the variable
global.
The strange thing here is first the destuctor for object local must also
have been called.
When this was called and tries to deallocate the Student object that already
has been deallocated by the temp object. This will as I think destroy the
free heap list and cause a crash. Am I right?.

Here is the class definition for AccountForStudent
class AccountForStudent
{
public:
AccountForStudent(long number, double balance);
~AccountForStudent();
long getNumber() const;
. . .
private:
Student* stud_;
double balance_;
};

Many thanks

//Tony

Aug 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony Johansson wrote:
I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
AccountForStudent global;
global = create(300);

return 0;
}

Here is what happen according to a book.:
1. Program execution enters the body of create(), and the variable local in
initialized by calling the constructor.
2. A temporary variable, temp, created by the compiler, is initialized by
calling the compiler generated copy constructor because no other is defined
and using the value of the local variable.
3. An assignment operator is called, which assign the value of the temporary
variable temp to global.
4.The destructor for the temporary variable temp is called.
5. create() terminates.
Actually, I'd move 5 between 2 and 3.
Now to my question: temp and local will share the same Student object
because we have a shallow copy.
How is that? Is 'Student' object in the temporary and 'local' identified
by the pointer and created in the free store by the 'local's constructor?
When the destructor for temp executes the Student object is deallocated and
therefore you now cannot access the Student object through the variable
global.
Actually, this might happen even sooner. When 'local' is destroyed, the
pointer to it (I am assuming it's a pointer) in the temporary becomes
invalid, and while the value of that pointer is copied into 'global', the
validity of it has already been disrupted.
The strange thing here is first the destuctor for object local must also
have been called.
Ah, yes, that's what I just described above.
When this was called and tries to deallocate the Student object that already
has been deallocated by the temp object. This will as I think destroy the
free heap list and cause a crash. Am I right?.
It causes _undefined_behaviour_. Whether that means "crash" or some kind
of destruction "the free heap", I don't know.

The trick here is that the compiler is allowed to employ NRVO (named
return value optimization), and forgo creation of a temporary and use the
'local' object instead. That means only one object will be destroyed
after 'create' function returns, not two.
Here is the class definition for AccountForStudent
class AccountForStudent
{
public:
AccountForStudent(long number, double balance);
~AccountForStudent();
long getNumber() const;
. . .
private:
Student* stud_;
double balance_;
};


V
Aug 11 '05 #2

P: n/a
Tony Johansson wrote:
I have this piece of code. No user defined copy constructor exist.
In effect, the C++ compiler supplies a default copy constructor.
AccountForStudent create(long number) {
AccountForStudent local(number, 0.0);
return local;
}
Why not

AccountForStudent create(long number) {
return local(number, 0.0);
}
int main(int argc, char* argv[]) {
AccountForStudent global;
global = create(300);

return 0;
}
Why not

int main(int argc, char* argv[]) {
AccountForStudent global = create(300);
return 0;
}
Here is what happen according to a book:
Which book?
1. Program execution enters the body of create()
and the variable local in initialized by calling the [explicit] constructor.
No! A good optimizing compiler
will recognize local as an alias for the return value
and initialize it directly -- no local object is created or destroyed.
This is called the Named Return Value Optimization (NRVO).
2. A temporary variable created by the compiler
is initialized by calling the compiler generated copy constructor
because no other is defined and using the value of the local variable.
No!
The compiler emits code to pass a reference to the temporary variable
to function create as a [hidden] argument
which represents the return value.
3. An assignment operator is called
which assign the value of the temporary variable temp to global.
4.The destructor for the temporary variable temp is called.
This is unnecessary if you use create(long) to initialize global
as shown in my example above.
5. create() terminates.
No!
create(long) terminates *before* the assignment.
The temporary variable is created in the scope of the calling program
so it must be destroyed in the scope of the calling program --
after create returns.
Now to my question: temp and local will share the same Student object
because we have a shallow copy.
Meaning, I suppose, that you don't copy the Student object *stud_.
When the destructor for temp executes,
the Student object is deallocated and, therefore,
you now cannot access the Student object through the variable global.
That's stupid.
You need to define a copy constructor and assignment operator
which do a *deep* copy
if you are going to define a "deep" destructor.

The strange thing here is [that]
first the destuctor for object local must also have been called.
When this was called and tries to deallocate the Student object
that already has been deallocated by the temp object.
This will as I think destroy the free heap list and cause a crash.
Am I right?.
You *might* be right.
Here is the class definition for AccountForStudent:

class AccountForStudent {
public:
AccountForStudent(long number, double balance);
~AccountForStudent(void);
long getNumber(void) const;
// . . .
private:
Student* stud_;
double balance_;
};

Aug 11 '05 #3

P: n/a

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:dd**********@nntp1.jpl.nasa.gov...
Tony Johansson wrote:
AccountForStudent create(long number) {
AccountForStudent local(number, 0.0);
return local;
}


Why not

AccountForStudent create(long number) {
return local(number, 0.0);
}

I think you mean
return AccountForStudent (number, 0.0);

/dan
Aug 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.