Re: How to free memory?
- To: mathgroup at smc.vnet.net
- Subject: [mg59834] Re: How to free memory?
- From: "Drago Ganic" <drago.ganic at in2.hr>
- Date: Tue, 23 Aug 2005 04:51:34 -0400 (EDT)
- Organization: Iskon Internet d.d.
- References: <de46jr$r62$1@smc.vnet.net><de6lru$cg5$1@smc.vnet.net> <de9cs2$q5l$1@smc.vnet.net> <debsf8$969$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
>
> Wow! What you are saying is very huge to me. I though that the scope
> of the variables defined in a Module is just inside the Module. But as
> you wrote, the variables defined in a Module can be referred to from
> outside (by referring e$nnn in my case), and they are effectively
> global variables! This is quite different from the concept of local
> variables in the ordinary programming languages like C and C++.
> Thanks,
> Wonseok Shin
>
Hi,
The scope of variables defined in Module is just inside the Module
**except** when they are returned from the Module. In this case they
survive.
Let's look at one example:
In[1]:=Module[{t}, t = 8] (* not returned, constructed & destructed *)
Out[1]=8
In[2]:=?Global`*
Global`t
In[3]:=Module[{t}, t] (* returned *)
Out[3]=t$21
In[4]:=?Global`*
t t$21
In[5]:=Module[{t}, t] (* another constructor*)
Out[5]=t$26
In[5]:=?Global`*
t t$21 t$26
As we can see memory is used by the non-destruction of the new and temporary
symbols. So, *only in this way* the local variables became global.
As you probably agree this is not the way "local" variables should be used.
But if used in this way, a Module is a "constructor" of a new variable. This
feature was used by Roman Maeder's OOP extension to Mathematica.
If you'd like to have just "ordinary" local variables as in C and C++, don't
return them from the Module construct. Then, they will be automatically
removed after Module evaluation as expected.
A small exercise: Reset the Kernel, change Module to Block, evaluate and see
the difference :-)
Greetings from Croatia,
Drago Ganic