Node.js C++ addon global variable scope - c++

I would appreciate if anyone could elaborate about the variable scopes in c++ addons of node js. For example, I have initialized some global variables in the addon (c++ file) like "int size" or something like this. so when I call the function in that addon it doesn't reinitialize the value. in fact if I assign something in some function "size = 27" and then after some time I call another function of that addon it will see size equal to 27. so when is addon stack deallocated and global variables reinitialized in C++ Addons?
Thanks,

Related

C++, Visual Studio 2017: Access a global variable of an .exe from within a loaded .dll [duplicate]

This question already has answers here:
Sharing a global/static variable between a process and DLL
(8 answers)
Closed 4 years ago.
I have the deplorable mission the access a global(!) variable of a .exe from within my .dll (loaded by that .exe).
A header file gives me following declaration of the global variable:
extern Element *ops_TheActiveElement;
Basically, the global variable is a pointer to a class instance which i need to acess.
From what I found on the web, i could use something like this from within the dll:
Element* getTheActiveElement()
{
auto handle = GetModuleHandle(NULL);
if (handle == NULL)
std::cout << "handle" << handle << std::endl;
auto activeElement = GetProcAddress(handle, "ops_TheActiveElement") ;
return (Element*)activeElement;
}
Getting the module handle is fine, but getting the address of the global variable fails.
I am not sure, if this is possible at all since i am not familiar with Windows programming.
What could be a possible approach? Thank you in advance!
Proffered approach would be to supply this variable explicitly when invoking code from your library. Use of extern (except for extern "C" functions) and use of global variables are a code smell.
Alternatively you can export this variable from executable and link you dll to executable so ops_TheActiveElement can be accessed directly.

Python 2.7 - local vs global vars [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 6 years ago.
So I am learning the difference between global and local vars in Python 2.7 and as per my understanding
local var is one that is defined within a function and
globar var is one that is defined outside of the function.
I created this simple function to test how these local and global vars work when used in combination
def f():
global s
print s
s = "Python is great."
print s
Before I ran the function, I declared the global s
s = "I love python!"
f()
The output was:
>>> f()
I love python
Python is great
I understand till this part, but what I don't understand is, when I call the run a print s outside the function, why is it printing the local variable instead of the global one. Does this mean that the global var s is used once and discarded?
>>> print s
Python is great
can someone please explain this?
... what I don't understand is, when I call the run a print s outside the function, why is it printing the local variable instead of the global one.
There is no local s within the function. The global s statement causes the Python VM to use the s in the global scope even when binding it.
You declared s to be a global in the function, overriding the default behaviour, by using the global s statement. Because s is now a global, and you assigned a new value to it, that change is visible globally.

How to initialize global variable for an embedded device?

I'm writing code using Qt under Linux (Ubuntu) for an embedded device.
I arranged a header file with global variables declaration inside; when I tested my project under Linux on my PC, everything is OK. When I try my project with the embedded system, sometimes it doesn't work, because I forgot to initialize some variables (seems that on my PC, Qt automatically set to "0" their values, while on the embedded they can have a random value).
Is there a way to initialize in one step all my variables, in such a way that when I pass them to my embedded the global variables are already correctly initialized?
Can you show us some code ?
When using global variables, you need to define them in a C/C++ file and initialize them here. The declaration in the header only says "Hey ! this one exists ! " even if not...
What I wanna do is to be sure that every global variable I create in my header file is initialized with "0" value. Is there a method, a pragma, an additional C++ header to do this?

Error when creating a global variable in llvm

I am trying to create a global variable in a function pass. The code is
gVar= new GlobalVariable(
/*Type=*/Int32Type,
/*isConstant=*/false,
/*Linkage=*/GlobalValue::CommonLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"gVar",
/*ThreadLocalMode*/GlobalVariable::InitialExecTLSModel);
However, I keep getting the following compiler error:
error: no matching function for call to ‘llvm::GlobalVariable::GlobalVariable(const llvm::Type*&, bool, llvm::GlobalValue::LinkageTypes, int, const char [4], llvm::GlobalVariable::ThreadLocalMode)’
Could you please tell me the right way to declare a global variable in llvm? Thank you very much!
In addition, I've referred to the header file:
http://llvm.org/docs/doxygen/html/GlobalVariable_8h_source.html
and this post
How can I declare a global variable in LLVM?
You need to pass a Module to the constructor. There are plenty of examples in the LLVM code base for creating global vars. For example, in examples/ExceptionDemo/ExceptionDemo.cpp:
new llvm::GlobalVariable(module,
stringConstant->getType(),
true,
llvm::GlobalValue::LinkerPrivateLinkage,
stringConstant,
"");
By the way - important note: you should not be creating new globals or doing anything else that modifies a module in a function pass. If you have to do that, you need a module pass.

Why can't I access my array subscript in global scope

If I have the following code in a function, I will not get an error and I can compile no problem, however, once I put it in global scope I will get an error for "cannot allocate an array of size zero", along with several other errors. Why does this happen and how can I get rid of the errors. I am aware of the risk of global variables, this is just a simple test case.
int* intest[2];
intest[0] = new int;
You are allowed declarations in global scope but not allowed to use the new operator or the assignment. Thus you need the declaration int *intest[2] in global scope (and all your code would see it) but C++ requires the new to be in the sequence of your main code. (probably in some sort of start up function for the app).
EDIT: as pointed out by #phresnel you can use the new operator in this scope but not the assignment (this is unusual but not illegal). However the following new operators used as initiation will work for you:
int *x[2]={new int,new int};
In general the use of such a global buffer is highly discouraged and is considered an anti-pattern - if you can avoid using it you probably should.
int* intest[2];
Is valid placing in the local scope however :
intest[0] = new int;
is not.
The difference is that the upper one is a initalisation statement (creating the variable) and the lower one is a executed code segment.
Code that should be "executed" cant be called in the global scope, for example you cant call a function in the global scope. When would that function be called?
I can create how many variables i want in the global scope but i cant run code from it except from constructors being called when initialisating the global variables.
If you want to execute code such as :
intest[0] = new int;
You would have to execute it trough main or another function, otherwise the program would not know when to execute it.
AFAIK, the global scope only allow u put define and declaration on it. Whereas intest[0] = new int; is a assignment that c/c++ compiler shall fail while compile.