when a ttcn function "runs on SomeComponent", how can it access the instance of this SomeCOmponent type? - ttcn

I read that when ttcn function is annotated with "runs on SomeComponent", it means that when it is called, it gets passed an instance of that SomeComponent. How can this function access this instance? Is it available in some special variable?

Inside the function, you can use all variables defined in SomeComponent as if they are local variables defined in the function.
The only difference between these variables and local variables is that their lifetime and scope is larger than local's. Their values are accessible in all functions that run on that component and they are initialised on component startup.

As Nomce said, the instance variables of a component are available in the same scope as local variables (mixing dynamic and static scoping).
But sometimes you need the actual component reference - in case you need to pass it around, do mappings or connection (store in array and - generally - perform dynamic configuration). Then you can use the special self reference.

Related

Difference between static data member and global variable

Variations of this question might have been asked earlier as well. This question is about static data member vs global variable not static variable vs global variable.
All of us know what static data member and global variables do. How compilers link them, where they get mapped in memory layout, their default values etc. When I think about them I do not have a clear picture of the possible scenarios where we should use static data member instead of global variable.
I thought of one use case where you want to count the number of objects created for a class. You make one static data member and increase in the ctor whenever you create an new object. But at same thought, we could also do the same thing, counting of the created objects, with the global variable as well.
So, it is not clear to me till now that what are the use cases of using static data member vs global variable?
They are in different scopes:
The static data member can have visibility restriction public/protected/private.
The global variable can be modified without restriction
The static (global) variable can only be modified in file scope.

C++, What is difference between static local variable(method) vs global(file) variable?

A local static variable is created once and the method will go out of scope, but the static variable is not destroyed and it will live in memory until the program ends.
A global static variable live in memory until the program ends.
So what is difference between local and global static variables?
When should we use local static variables and global static variables?
Other answers have told you the differences between a local static object (local to a function) and a non-local static object (static objects declared global or in a namespace); however, you should also understand the importance of using one over the other.
The information I use here comes from Effective C++ Third Edition by Scott Myers (a recommended and excellent read)
A static object is one that exists from the time it's constructed until the end of the program.
As others have mentioned, a non-local static object is constructed before main whereas a local static object is constructed the first time the function is called.
But what if you had two static objects and one relied on the other. How could you be sure that one would be constructed before the other? You can't: The relative order of initialisation of non-local static objects defined in different translation units is undefined
Scott Myers definition of a translation unit (from aforementioned book):
A translation unit is the source code giving rise to a single object file. It's basically a single source file, plus all of the #include files.
So imagine you have these two non-local static objects in separate source files, you couldn't guarantee one would be constructed before the other. This is where a local static object prevails!
Consider Scott Myers' example of a FileSystem class and a Directory class, where the Directory relies on the FileSystem:
class FileSystem
{
public:
size_t numDisks() const;
};
extern FileSystem tfs;
and
class Directory
{
public:
Directory()
{
size_t disks = tfs.numDisks();
...
}
};
If Directory gets constructed before FileSystem, then you are going to be using an uninitialised class! Fortunately, you can get around this by using local static objects!
class FileSystem
{
public:
size_t numDisks() const;
};
FileSystem& tfs()
{
static FileSystem fs;
return fs;
}
Now when Directory is constructed, even if it's constructed before FileSystem is, FileSystem is guaranteed to be constructed before it's used!
An addition what others have said :
Scope and Lifetime are two different things.
Static variables' lifetime = Program's lifetime, that's why static variable is not destroyed and it will live in memory until program got end.
Scope means where we work with the variable/use the variable
Lifetime on the other hand is the complete execution time of the program, there may be certain cases where we terminate the program but in backgroud still bits and pieces are running.
When we have to use local static variable and global static variable?
We should use a local static when we need a variable restricted to that function only
We should use a global static when we need a variable common to all functions in the program. Eg:- A variable which contains the Budget of the organisation, whic is same for all departments
Hope this helps. Thanks
Differences I can think of:
The scope of the variable. A global static variable is visible to the entire program. A function static variable is visible only in the function.
Initialization. A global variable is initialized before main is executed. The function static variable is initialized when the function is called the first time.
A local static is visible only within the function. This allows the name to be kept secret. This could be used to implement profiling logic, where the number of calls for each function are tracked.
The more significant benefit of a local static, is the point in time when it is constructed. Global data is constructed before main is called. This occurs from the top of the file to the bottom of the file for each C++ file in the program, but the order of files in the construction is undefined. A local static is only constructed the first time it is called (C++ 11 guarantees only once, earlier C++ requires some synchronization if it can be called multi-threaded.) This means a complex object which requires some facility of your program to be initialized (e.g. some database) then this can be controlled as you can perform that initialization in main before your functions containing these static variables are called.
Global static variables can be accessed, and potentially changed, by other scopes. For example, two functions can share state using a global static variable. A local static variable is just that: local. Other functions cannot see it directly.
The fact that global static variables may be changed by other functions is a primary reason why they are so dangerous. Programmers often make unwarranted assumptions about the variable. For example, if a method uses, but does not modify a global static variable, it is still possible for the value to change from the beginning of the method to the end. People will tend to assume, however, that the value is unchanging.
A local Static is visible only inside the function, it has the scope of function level only, while Global Static has the program scope. And initialization, global is initialized at before main, local static would be initialized the first time function will get called.
Global Static variables if not used properly can cause issues, because in a large program it would be hard to keep track what is changing it's values.
Another possible drawback may be the linkage, what if you are using a global static variable named varnew and you are using an external library which also have the same variable.
It is always suggested to limit the scope of your variables to avoid unnecessary errors.

How does the compiler know where are static fields allocated?

Imagine you have a class A with a static field int mstatic.
Imagine if that class has a method mymethod that modifies mstatic. When compiling mymethod, how can the adress of mstatic be known ? I know that in case of non-static fields, a pointer to the calling object (the famous "this") is implicitly passed to the method so it is used to find the adresses, but how do we do for static fields ?
Static fields are allocated similarly to namespace-scope or global variables... there's basically one or two areas (variables needing 0 initialisation may be separated from those needing initial non-0 values) sequentially populated with all such variables in the translation unit. If the variable's defined in another variable, the address will be patched in during linking or loading. Note that the addresses are typically effectively hard-coded (fixed address, perhaps from a specific data segment register), unlike stack (which may be stack register relative, but the stack register is modified as functions are called and return, unlike data segment registers which may be set to the same value while the thread is running) or heap hosted variables (where the address is determined during malloc or new.

Read Only Variable Assignment?

I have a private member variable called "Probes", Within one of my private member functions, i want to use the variable "Probes" to keep track of the number of comparisons made. To do this I just put the variable within the loop and did Probes++ each time through. But when I compile and run it, it says that I cannot assign to a read only variable? How can I correct this?
If this is a read only variable (constant) you cannot do Probes++ (which is similar to Probes = Probes + 1) because constants cannot be updated. What you can do is to declare it as a variable (which you can modify while the program is executed) or just declare Probes as mutable.

gdb local variables in a function

Is there a way in gdb to check the local variables of a member function and also the class private members. Sometimes, I need to know which variables are local to the function and which ones are class variables.
Best,
Umut
info local
will print the values of all local variables (see info args if you also want function parameters)
print *this
will show the member variables.