A
Alan Johnson
John said:Hi Karl:
Thank you very much. I understand now.
On this discussion list, there are so many warm-hearted experts who
spend their precious time instructing me--a newbie of C++.
I have two questions. Is the following code free of trouble?
void f()
{
X *x0 = new X(10);//create and initialize an object of class X.
X *x1;
x1 = x0;
delete x1; //release the memory that x0 uses.
}
I do not "delete" x0, but "delete" x1 to release the memory. Will it
cause any problem. An old code that I am modifying uses this approach.
No problems there. Although it isn't a particularly "safe" way to do
things, because code that is using x0 doesn't necessarily know that the
memory to which it points has been deleted.
If you need to use multiple pointers to point to the same dynamic
memory, try to come up with some scheme that let's you know which
pointer "owns" (i.e., is responsible for deleting) the memory. One
possible approach is as follows:
---
X *x0 = new X(10); // Allocate some memory. This memory is owned by x0.
X *x1 = NULL; // This pointer doesn't own anything, so it is NULL.
f(x0); // Do something with x0.
x1 = x0; // Transfer ownership of memory
x0 = NULL; // to x1.
f(x1); // Do something with x1.
if (x1) delete x1; // Delete the memory, but if and only if x1 owns it.
---
I'd mostly try to just avoid passing ownership of allocated memory
blocks around in the first place, though. Also, depending on what you
are doing, you may try using an auto_ptr to "own" your allocated memory,
so that you don't need to worry about whether or not it gets
deallocated. For example:
void f()
{
auto_ptr<X> x0 = new X(10);
auto_ptr<X> x1;
// Do something with x0.
x1 = x0 ;
// Do something with x1.
} // Ignore the memory, it will get deleted on its own.
A nice auto_ptr tutorial is available here:
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
Another question is:
What is the differece between the two lines below:
X *xx = new X;//line 1
X *xx1;//line 2
Line 1 declare xx and allocate it memory.
Line 2 declare xx1 and xx1 points to somewhere unknown. Has xx1 been
allocated memory?
This would all depend on the platform and/or implementation, but for the
sake of discussion, let's say that pointers, on your system, require 4
bytes, and the object you are calling X requires 32 bytes.
Let's look at line 2 first. This allocates 4 bytes of "automatic"
memory. By automatic, we mean that the any allocation and deallocation
of the memory is handled for us by the compiler. This is no different
from declaring an int, double, char, etc. The standard doesn't specify
how this must be implemented, but typically this occurs on what is
referred to as "the stack". What is the value contained in those 4
bytes? We can't really say, because it wasn't initialized.
Now, line 1. Obviously this line must do at least as much as line 2. It
allocates 4 bytes (which happen to have the name xx) of automatic
memory. It also allocates 32 bytes (at least) of dynamic memory.
Dynamic memory means that the programmer is responsible for allocating
and deallocating it, instead of the compiler. In most implementations,
this memory comes from a place called "the heap" or "the free store"
(refer to other posts in this newsgroup for arguments about what to call
this). What is in the 4 bytes called xx? If we look, we should find
that the value there is the memory address at which our 32 byte block
got allocated, which is, of course, why we call it a pointer.
Thanks a lot.
John
Alan