Sign in to follow this  
virious

Local variables allocation question

Recommended Posts

I wanted to know if local variables are allocated or their constructors - if present - are called at the start of script function or when they are used?

Below simple example:

float a = 4.2f;

float calc(float a, float b)
{
Log("Received: " + a + ", " + b + "\n");
if (a == 4.2f)
{
int a = 6;
}

return a * b;
}

void main()
{
Log("Result of calc function: " + calc(5, 5));
}

When breakpoint is set at line with "Log" in calc function, I can see that local nested variable a (line: int a = 6) already exists, so we have 2 local variables named "a", before even nested "if" is executed (which btw. is never executed with this condition).

[Edited by - virious on November 30, 2010 7:25:08 AM]

Share this post


Link to post
Share on other sites
All the necessary memory for local variables is allocated when entering the function. The constructors/factories for object types are only called when the declaration is reached, before this the GetAddressOfVar will return 0. GetAddressOfVar will always return a valid address for primitive types.

Currently AngelScript doesn't give you the scope of each variable, so when enumerating local variables you will see all local variables, whether they are currently in scope or not.

I plan on adding this information for debugging purposes for a future release. So if you need this information you must unfortunately determine this from the source script by yourself at the moment.

Observe that variables in different scopes may share the same location on the stack but only if the type is compatible.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this