local-variables

Are local variables threadsafe?

落爺英雄遲暮 提交于 2019-12-22 05:53:14
问题 I have a class like the one below: class Program { static void Main(string[] args) { var outputWindow = new OutputWindow(); var threads = new List<Thread>(); Action action = () => outputWindow.Display(20); for (int i = 0; i < 10; i++) { var thread = new Thread(() => action()) {Name = "Thread " + i}; threads.Add(thread); } foreach (var thread in threads) { thread.Start(); } } } public class OutputWindow { public void Display(int x) { for (int i = 0; i < x; i++) { Console.WriteLine(Thread

C# Returning local variables

老子叫甜甜 提交于 2019-12-22 04:40:50
问题 Coming from C++, returning a local variable was a bad idea (when allocated memory on the stack). Now using C# I'm getting the impression it isn't a bad idea (when returning a value, not a reference). Why is that? I understand C# uses the GC but I'm not sure what difference that would make in this case. 回答1: The problem in C/C++ is that you can return a pointer to data that is located on the stack. If you do that the pointer is invalid as soon as the stack frame is destroyed. In managed C# you

Javascript - local scope objects not accessible from nested function

六月ゝ 毕业季﹏ 提交于 2019-12-21 21:38:28
问题 I am trying to have a function grab an object from a php file on another page. I'm using the jQuery ajax function to to do the json grab, which is working correctly. The issue is when I try to return that object from the function. The first time I log the object (from within the success function) it is correct in the console, but the returned object from the function getGantt() logs as "undefined". How do I get this object out of the function? My code: function getGantt(requestNumber){ var

Get value from string representing local variable [duplicate]

試著忘記壹切 提交于 2019-12-21 04:06:32
问题 This question already has answers here : Is there a 'variable_get' method? If not, how can I create my own? (2 answers) Closed 4 years ago . I have a local variable name as a string, and need to get its value. variable = 22 "variable".to_variable? How can I get the value 22 form the string? 回答1: You can use eval . variable = 22 eval("variable") # => 22 However eval can be nasty. If you dont mind declaring an instance variable, you can do something like this too: @variable = 22 str = "variable

How to use acast (reshape2) within a function in R?

浪尽此生 提交于 2019-12-20 23:19:06
问题 I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it. Here is my data: library("reshape2") x <- data.frame(1:3, rnorm(3), rnorm(3), rnorm(3)) colnames(x) <- c("id", "var1", "var2", "var3") y <-melt(x, id = "id", measure = c("var1", "var2", "var3")) y then looks like this: id variable value 1 1 var1 0.1560812 2 2 var1 1.0343844 3 3 var1 -1.4157728 4 1 var2 0.8808935 5 2 var2 0.1719239 6 3 var2 0.6723758 7 1 var3 -0

Why are local variables accessed faster than global variables in lua?

冷暖自知 提交于 2019-12-20 12:07:16
问题 So I was reading Programing in Lua 2nd Ed and I came across this paragraph here: It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones . Could anyone explain why this is? And is this "feature" only in Lua, or is it in other languages aswell? (e.g. C, C++, Java) 回答1: The difference in running time is due to the

How to change local static variable value from outside function

[亡魂溺海] 提交于 2019-12-20 05:55:28
问题 #include <stdio.h> void func() { static int x = 0; // x is initialized only once across three calls of // func() printf("%d\n", x); // outputs the value of x x = x + 1; } int main(int argc, char * const argv[]) { func(); // prints 0 func(); // prints 1 func(); // prints 2 // Here I want to reinitialize x value to 0, how to do that ? <-- this line return 0; } In the above code, after calling func() 3 times I want to re-initialize x to zero. Is there any method to reinitialize it to 0? 回答1: Do

Setting local variables to a function instead of using globals optimize the function

岁酱吖の 提交于 2019-12-20 01:35:27
问题 In the documentation for the itertools module I found this comment def dotproduct(vec1, vec2): return sum(imap(operator.mul, vec1, vec2)) Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the dotproduct recipe can be written as: def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul): return sum(imap(mul, vec1, vec2)) How is it?. Is there a practical noticeable speed-up (that could balance the

Passing local variable with name of a global variable isn't possible in JS?

牧云@^-^@ 提交于 2019-12-20 01:16:31
问题 foo = "foobar"; var bar = function(){ var foo = foo || ""; return foo; } bar();` This code gives a result empty string. Why cannot JS reassign a local variable with same name as a global variable? In other programming languages the expected result is of course "foobar", why does JS behave like that? 回答1: That's because you declared a local variable with the same name - and it masks the global variable. So when you write foo you refer to the local variable. That's true even if you write it

Ruby array manipulation inside method

回眸只為那壹抹淺笑 提交于 2019-12-19 19:55:09
问题 In the following, input_1 changes: def method_1(a) a << "new value" end input_1 = [] method_1(input_1) input_1 #=> ["new value"] In the following, input_2 does not change: def method_2(a) a = ["new value"] end input_2 = [] method_2(input_2) input_2 #=> [] Why does input_1 change while input_2 doesn't change? 回答1: With a bit of simplification we can say that a variable in Ruby is a reference to a value. In your case variable a holds a reference to an array. a << ( a.append ) mutates the value