Who modified my local variable?
Hans Fugal
hans at fugal.net
Tue Jun 13 14:12:24 MDT 2006
On Tue, 13 Jun 2006 at 13:11 -0600, Michael L Torrie wrote:
> On Tue, 2006-06-13 at 12:47 -0600, Bryan Sant wrote:
> > C++:
> >
> > void no_leak() {
> > MyObject obj(1, 2, 3);
> > }
> >
> > Local variable. Allocated on the stack. Falls out of scope shortly. No leaks.
> >
> > Java:
> >
> > void noLeak() {
> > MyObject obj = new MyObject(1, 2, 3);
> > }
> >
> > Local variable. Sometimes called an automatic variable in Java-terms.
> > Allocated on the heap (which is really just a stack in the case of
> > auto vars). Falls out of scope shortly. GC will pop this off the
> > heap-stack-thingy. No leaks.
>
> Sure. But my real point was the Java habit of always doing "new" causes
> a ton of problems for Java programmers heading into C++. Rather than
> use a local variable allocated on the stack, they do "new" which leaks.
> That is the problem. So really it's not a language problem; it's a
> programmer problem. But a programmer can be trained; unfortunately Java
> is not a good trainer in this instance. In my opinion it would be
I doubt Java's goals included training students to program in C/C++.
You sound silly.
> better to subject a new CS student to C and C++ in all their glory and
> then introduce them to managed, Garbage-collected languages later,
> rather than the other way around.
That's a valid approach. In either case, students are going to have to
_understand_ the memory model of C++ to avoid memory leaks. This doesn't
happen automatically with either approach.
--
Hans Fugal ; http://hans.fugal.net
There's nothing remarkable about it. All one has to do is hit the
right keys at the right time and the instrument plays itself.
-- Johann Sebastian Bach
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: Digital signature
Url : http://plug.org/pipermail/plug/attachments/20060613/8934b376/attachment.pgp
More information about the PLUG
mailing list