mrwad99
asked on
Reuse of a pointer ??
Hello...
I am playing with the following code at the minute, after reading something similar in a textbook of mine. Let me first show you the code:
////////////////////////// ////////// /
#include <iostream>
using std::cout;
using std::endl;
class Test
{
public:
Test() { count++; }
~Test() { count--; cout << count << endl; }
Test(const Test&) { cout << "Copying" << endl; }
static int count;
};
int Test::count = 0;
int main()
{
Test a[10];
Test* t = 0;
for (int i = 0; i < 10; i++) {
t = new Test();
a[i] = *t;
delete t;
}
cout << Test::count << endl;
//delete[] a;
return 0;
}
////////////////////////// ////////// /
Nice and simple. Now, I have declared a pointer to an instance of my Test class, t, and am setting that up to point to 10 objects that will be created in a loop. Right. Now first of all, I do not understand how the pointer t can point to 10 different objects when it is being (what I perceive as) reassigned by the statement
t = new Test();
So firstly, why is this ? The book I am reading says 'a copy of the object is placed into the array a', hence that is why we 'delete t', but I have wrote a copy constructor and there is no evidence of it being called !
Secondly, I put the static variable in as a count of how many objects are being created, and it appears that my book is correct; when I do not have the 'delete t' statement the output from
cout << Test::count << endl
is 20; when I do have it the output is 10. So with this info, I decided I had better delete the contents of my array 'a', so proceeded to call 'delete [] a', to which VC++ replied:
warning C4154: deletion of an array expression; conversion to pointer supplied
Eh ?!?!
I then realised that there is no reason to delete this array, as I have not allocated space with 'new' for it. But the output (as stated above) is:
20
19
18
17
16
15
14
13
12
11
10
meaning 10 items are still left undestroyed. So, why does 'delete[] a' give the warning (and crash the program on running if I ignore it), and how can I delete the objects left over ?
Cheers in advance.
I am playing with the following code at the minute, after reading something similar in a textbook of mine. Let me first show you the code:
//////////////////////////
#include <iostream>
using std::cout;
using std::endl;
class Test
{
public:
Test() { count++; }
~Test() { count--; cout << count << endl; }
Test(const Test&) { cout << "Copying" << endl; }
static int count;
};
int Test::count = 0;
int main()
{
Test a[10];
Test* t = 0;
for (int i = 0; i < 10; i++) {
t = new Test();
a[i] = *t;
delete t;
}
cout << Test::count << endl;
//delete[] a;
return 0;
}
//////////////////////////
Nice and simple. Now, I have declared a pointer to an instance of my Test class, t, and am setting that up to point to 10 objects that will be created in a loop. Right. Now first of all, I do not understand how the pointer t can point to 10 different objects when it is being (what I perceive as) reassigned by the statement
t = new Test();
So firstly, why is this ? The book I am reading says 'a copy of the object is placed into the array a', hence that is why we 'delete t', but I have wrote a copy constructor and there is no evidence of it being called !
Secondly, I put the static variable in as a count of how many objects are being created, and it appears that my book is correct; when I do not have the 'delete t' statement the output from
cout << Test::count << endl
is 20; when I do have it the output is 10. So with this info, I decided I had better delete the contents of my array 'a', so proceeded to call 'delete [] a', to which VC++ replied:
warning C4154: deletion of an array expression; conversion to pointer supplied
Eh ?!?!
I then realised that there is no reason to delete this array, as I have not allocated space with 'new' for it. But the output (as stated above) is:
20
19
18
17
16
15
14
13
12
11
10
meaning 10 items are still left undestroyed. So, why does 'delete[] a' give the warning (and crash the program on running if I ignore it), and how can I delete the objects left over ?
Cheers in advance.
Note that you should increment count in the copy constructor, too, since it is called when things are created and the count will be wrong if it is not incremented in all constructors.
-bcl
-bcl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
With the presence of:
delete t;
I can't believe that your ouput is
>20
>19
>...
>10
Can someone verify this.
[ ... not having access to a compiler really sucks]
delete t;
I can't believe that your ouput is
>20
>19
>...
>10
Can someone verify this.
[ ... not having access to a compiler really sucks]
I can get the output in the original post if I comment out the delete call. I would guess that it is from one of the test runs where delete t was NOT funcitonal.
-bcl
-bcl
ASKER
Right then:
As _ys_ states, the output given *was* when 'delete p' was commented out.
I am forgetting that the statement
Test a[10];
Results in the default constructor being called ten times, hence creating 10 objects (never). I was getting confused thinking "how the heck is the assignment operator copying the object; should that not be the copy constructor"... but in fact I know now that the *object itself* was not being copied, only the values of it. Hence the values of the object pointed to by p were assigned to the values of the objects *already there* that were created by the default constructor (I say values; I changed the code locally to allow the constructor to take an int parameter just to test).
The penny drops !
I have thus split the points between rstavely and bcladd for their combined answer.
Many thanks to all once again !
As _ys_ states, the output given *was* when 'delete p' was commented out.
I am forgetting that the statement
Test a[10];
Results in the default constructor being called ten times, hence creating 10 objects (never). I was getting confused thinking "how the heck is the assignment operator copying the object; should that not be the copy constructor"... but in fact I know now that the *object itself* was not being copied, only the values of it. Hence the values of the object pointed to by p were assigned to the values of the objects *already there* that were created by the default constructor (I say values; I changed the code locally to allow the constructor to take an int parameter just to test).
The penny drops !
I have thus split the points between rstavely and bcladd for their combined answer.
Many thanks to all once again !
Variables declared locally in functions have what is called automatic storage. An object will go out of scope and have its destructor called when the function returns.
Try instrumenting the destructor. You'll see it called ten times after the last executable line in main (as well as once for each call to delete).
Each call to new allocates a new object on the heap. These objects are NOT automatically deleted. This is good since you can keep dynamically allocated objects around between calls to functions. It is bad because it requires the user to handle the deletion (this is the source of many memory leaks).
Hope this helps, -bcl