Do Lua variables lose their value between script calls? - scripting

In a C app when I call a Lua script, do the variables in the code stay with the same value when I call the script again later?

They will still exist in the lua state you created until you close that state. The variables are tied to the state not the script file.
edit
As noted in the comments local variables will be garbage collected when they go out of scope. A further caveat is that Lua supports closures and upvalues so the scope may not always be completely obvious.

Related

Difference between a constant and variable member in compiled or interpreted code

For a while now I have been a little confused about the role of constant members within a language, such as Java or C. I understand that at the source code level, they prevent certain critical members from being mutated and changed, but when compiled or interpreted, is there any difference between them and variable members at all or are they all just pointers to memory addresses?
I thought that perhaps the compiler/interpreter has to implement something special to allow a variable to be mutable, something it wouldn't have to when handling a constant member (perhaps making execution faster or making it use less memory?), is this true or am I completely up the wrong tree?
The const variable and the variable are not stored in the same place once your code is executed. The constant values will go in the flash memory with your program. The variables will go in the flash too but will then be copied in the ram to be modified as your program runs. Making a variable const makes your computer save time and space by not pushing everything in the ram. When you need to modify it, you will push it into the Ram anyway, but most of the time const variables will not be modified.
This is in addition to the software fact that you might want to prevent your code to modify a value by mistake.

When do I need to copy a block?

I don't really know when do I need to copy a block.
The documentation says when you expect the block to be used after destruction of the scope within which it was declared.
Do I need to copy it when I'm using them as callback methods for example? By copy I mean creating a copy property for my block and saving the block there or somehow sending a copy message. The scope is probably destroyed when a download or something similar is finished. So do I need to copy the block in that case? Can I create circular references this way?
Right now I have a downloader class (to test blocks only, it's too dangerous for me to use it yet) that uses a completion block and when I copy the block the view controller that created the block and the downloader class is not released.
Can you give me some good examples especially using them for finishing async operations like file downloads?
Thanks.
Most of the time methods that take callbacks will copy the block for you
I can think of two cases when you need to copy a block yourself in Objective-C
when you're returning a block from a function
when you're saving a block to a class variable (this is the case that happens when you're implementing a method that accepts a callback at a later point).
Since these are the times the block will drop out of scope before they're used or dealt with.

Objective-C, global variables and threads

I've made several Objective-C class files. Two of them had the same name for a global variable. When the program was running a thread from one file but was also accessing code from the other file on another thread, the thread seemed to get confused on what global variable belongs to it.
Is this a true issue or was my code wrong? I seem to have fixed it by changing the variable name.
I would go with your code was wrong, but I think there's a more fundamental thing you are misunderstanding here.
A thread doesn't per-se belong to a file, or own anything. What really happened is say you have two functions, one in each of your file, the compiler (since your variables aliased each other) chose to use one variable in one file, and another in another. This has nothing to do with threads, or anything else.
Furthermore, if you are looking for a thread local variable, you might want to look at the threadDictionary property of NSThread http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html

How does an interpreter switch scope?

I'm asking this because I'm relatively new to interpreter development and I wanted to know some basic concepts before reinventing the wheel.
I thought of the values of all variables stored in an array which makes the current scope, upon entering a function the array is swapped and the original array put on some sort of stack. When leaving the function the top element of the "scope stack" is popped of and used again.
Is this basically right?
Isn't swapping arrays (which means moving around a lot of data) not very slow and therefore not used by modern interpreters?
Why swap the array? Just look at the top array on your stack. Furthermore, in most languages you don’t have to copy the array when you want to swap it, you can just swap references or pointers.
This is also what an interpreter might do. An alternative is having a special data structure for the current scope which holds a reference to its parent frame explicitly.
Python uses the C stack to keep track of its scope. Everytime a new scope is entered a new function call is made so that the scope's data is always held in the local variables on the stack.
For some other interpreters, everything is kept on the stack something like your suggestion. However, the interpreter acts on the top of the stack in-place. There is no need to copy things back and forth since there is only one copy.

OOP question about functions that struck me all of a sudden

May be my question is stupid. But i would like to get it cleared. We know that functions are loaded in memory only once and when you create new objects, only instance variables gets created, functions are never created. My question is, say suppose there is server and all clients access a method named createCustomer(). Say suppose all clients do something which fired createCustomer on server. So, if the method is in middle of execution and new client fires it. Will the new request be put on wait? or new request also will start executing the method? How does it all get managed when there is only one copy of function in memory? No book mentions answers to this type of questions. So i am posting here where i am bound to get answers :).
Functions are code which is then executed in a memory context. The code can be run many times in parallel (literally in parallel on a multi-processor machine), but each of those calls will execute in a different memory context (from the point of view of local variables and such). At a low level this works because the functions will reference local variables as offsets into memory on something called a "stack" which is pointed to by a processor register called the "stack pointer" (or in some interpreted languages, an analog of that register at a higher level), and the value of this register will be different for different calls to the function. So the x local variable in one call to function foo is in a different location in memory than the x local variable in another call to foo, regardless of whether those calls happen simultaneously.
Instance variables are different, they're referenced via a reference (pointer) to the memory allocated to the instance of an object. Two running copies of the same function might access the same instance variable at exactly the same time; similarly, two different functions might do so. This is why we get into "threading" or concurrency issues, synchronization, locks, race conditions, etc. But it's also one reason things can be highly efficient.
It's called "multi-threading". If each request has its own thread, and the object contains mutable data, each client will have the opportunity to modify the state of the object as they see fit. If the person who wrote the object isn't mindful of thread safety you could end up with an object that's in an inconsistent state between requests.
This is a basic threading issue, you can look it up at http://en.wikipedia.org/wiki/Thread_(computer_science).
Instead of thinking in terms of code that is executed, try to think of memory context of a thread that is changed. It does not matter where and what the actual code happens to be, and if it is the same code or a duplicate or something else.
Basically, it can happen that the function is called while it was already called earlier. The two calls are independent and may even happen to run in parallel (on a multicore machine). The way to achieve this independence is by using different stacks and virtual address spaces for each thread.
There are ways to synchronize calls, so additional callers have to wait until the first call finishes. This is also explained in the above link.