Page 2 of 2

Re: Interesting Lua Fact...

Posted: Sat May 09, 2009 6:37 pm
by sparda
... "dynamically allocate" in Lua? Dude, Lua is a scripting language. When can you STATICALLY allocate? XD
For Lua, I don't know?

Well, Lua and Python are both scripting languages.
In Python, I you can do something like this for static variables allocation:

Code: Select all


x=0  
def static_num() :
   global x
   return 

This would be analogous to this in C/C++:

Code: Select all

void static_num(void)
{
    static int x;   
}

Re: Interesting Lua Fact...

Posted: Sat May 09, 2009 8:11 pm
by Ginto8
sparda wrote:
... "dynamically allocate" in Lua? Dude, Lua is a scripting language. When can you STATICALLY allocate? XD
For Lua, I don't know?

Well, Lua and Python are both scripting languages.
In Python, I you can do something like this for static variables allocation:

Code: Select all


x=0  
def static_num() :
   global x
   return 

This would be analogous to this in C/C++:

Code: Select all

void static_num(void)
{
    static int x;   
}
AHEM. That is creating a static variable in a function. Static variables and statically ALLOCATED variables are completely different things. Statically allocated variables use memory from the stack. Dynamically allocated ones, on the other hand, use memory from the heap.

Re: Interesting Lua Fact...

Posted: Sat May 09, 2009 9:18 pm
by sparda
Well, sorry for the confusion in my part. I will admit, I have seldom experience with scripting languages beyond python, so I got a little mixed up. Ginto, some of what you say is true, but that is not applicable here. I was asked to show how you can declare a variable with static allocation, under a VM environment. This is not possible, of course. I however showed how you can get in effect, a functional "property" of local static variables under C/C++; which is merely to retain a value between function calls.

My mistake was that I overlooked python's interpreter. You can not have static variables per say, because static allocation happens at compile time; yet scripting languages have no compile time for scripts. You see BITCHES, I am willing to admit when I'm am *cough*confused*cough*!!

With that said, Ginto you whippersnapper, you are wrong when you say:
Ginto wrote:Static variables and statically ALLOCATED variables are completely different things
Actually they are not.

Statically allocated variables, are still ALLOCATED regardless, be it in a function, or global; and be it stack-based or free-store store based. The real question is, are variables statically allocated (at compile time), or dynamically allocated (at runtime). In truth, this kind of stuff is language and implementation dependent.

When it comes to C for example (which is the the language Python is written in), a local static variable IS statically allocated. Only an automatic variable with local scope (i.e. within a function without the "static" storage specification), is pushed onto the stack.