Questions tagged [local-variables]

Local variables have a limited scope, generally one function or one functional block.

Local variables have a limited scope, generally one function or one functional block.

Compare with global variables, which are accessible from any part of a program.

1302 questions
8
votes
1 answer

Why a procedure is so much faster when put into a function?

Here is what I did, I created 2 procedures, one in a function and one in the python file itself. The one on the python file itself run almost 2 times slower even if it's exactly the same. WHY ? Bellow is an example with 2 procedures that are just…
Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
8
votes
3 answers

why is array size limited when declared at compile time?

for example I can do int *arr; arr = (int *)malloc(sizeof(int) * 1048575); but I cannot do this without the program crashing: int arr[1048575]; why is this so?
ladookie
  • 1,343
  • 4
  • 21
  • 24
8
votes
6 answers

Java how to print all local variables?

I have a method and want to examine variables inside it without debugging - is it possible in Java? I do not want to write tons of code like: System.out.println("a: " + a); I want to something like: System.out.printLocals(); Also it should be…
denys
  • 2,437
  • 6
  • 31
  • 55
8
votes
5 answers

Local Type Inference vs Instance

I've tried to scan JEP-286 about local type inference. I see that this works only for local variables - understood. So this does work indeed: public class TestClass { public static void main(String [] args){ var list = new…
Eugene
  • 117,005
  • 15
  • 201
  • 306
8
votes
3 answers

Local Variables Being Passed ( C++)

I have encountered a problem in my learning of C++, where a local variable in a function is being passed to the local variable with the same name in another function, both of these functions run in main(). When this is run, #include using…
8
votes
3 answers

Are local variables threadsafe?

I have a class like the one below: class Program { static void Main(string[] args) { var outputWindow = new OutputWindow(); var threads = new List(); Action action = () =>…
Mike
  • 3,204
  • 8
  • 47
  • 74
8
votes
1 answer

Elisp: referencing previous variables inside let

I'd like to define two variables in let, one of which depends on the value of the other, like so: (let ((a (func)) (b (if (eq a 1) 2 3))) ...) Obviously this is not the right way to do this, emacs says a is void. What's the right way…
Ron
  • 1,989
  • 2
  • 17
  • 33
8
votes
4 answers

inner class non-final variable java

I needed to change variables inside an inner class and I got the infamous "Cannot refer to a non-final variable inside an inner class defined in a different method" error. void onStart(){ bt.setOnClickListener(new View.OnClickListener() { …
Aaron Greenberg
  • 170
  • 1
  • 8
7
votes
1 answer

Will member subobjects of local variables be moved too if returned from a function?

The C++11 standard states that, if the conditions for copy elision are met (§12.8/31), the implementation shall treat a returned local lvalue variable and function parameters, as an rvalue first (move), and if overload resolution doesn't succeed as…
Xeo
  • 129,499
  • 52
  • 291
  • 397
7
votes
2 answers

Should you only use local variables in a partial?

Using local variables seems advisable in a partial that could be used application-wide to avoid dependencies across the application. But within a single controller it seems acceptable to reference instance variables that you know will be available…
eggdrop
  • 3,356
  • 5
  • 29
  • 32
7
votes
2 answers

Ruby: method inexplicably overwritten and set to nil

If I execute this ruby code: def foo 100 end p defined?(foo), foo if false foo = 200 end p defined?(foo), foo The output I get is: "method" 100 "local-variable" nil Can someone explain to me why foo is set to nil after not executing the if?…
bmesuere
  • 502
  • 3
  • 12
7
votes
4 answers

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

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",…
Henrik
  • 14,202
  • 10
  • 68
  • 91
7
votes
4 answers

findbugs complains about Eclipse's auto-generated code

Here's the hashCode() method that Eclipse kindly generated for me: @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); return result; } When I run findbugs on…
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
7
votes
1 answer

Can Byte Buddy access local variable name of a method?

Suppose I have a method m: public void m() { String foo = "foo"; int bar = 0; doSomething(foo, bar); } I want to use ByteBuddy to instrument the code so that when calling doSomething in m, it will automatically put the value of foo and bar…
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
7
votes
3 answers

What is the purpose of using a local variable to hold a global one?

I had a look at the source code of the String.hashcode() method. This was the implementation in 6-b14, which has been changed already. public int hashCode() { int h = hash; if (h == 0) { int off = offset; char…
Steve Benett
  • 12,843
  • 7
  • 59
  • 79