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
7
votes
1 answer

C# Returning local variables

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…
Ben Y
  • 291
  • 6
  • 14
7
votes
2 answers

C++ local variables and threads (not thread_local)

What are the C++98 and C++11 memory models for local arrays and interactions with threads? I'm not referring to the C++11 thread_local keyword, which pertains to global and static variables. Instead, I'd like to find out what is the guaranteed…
7
votes
1 answer

PHP performance: $this->variable versus local $variable (manipulating)

I had a section in a class that I decided to split into a new one. When I had ported the code section into a new class I noticed it was considerably slower at executing one of the foreach loops. I managed to track down part of the problem to be…
user1015149
  • 187
  • 1
  • 10
7
votes
1 answer

Difference between return value and local variable

Suppose I have #include class A { public: template operator T(); A child(); }; void f() { A a; std::string s1 = a; // ok std::string s2 = a.child(); // error (line 34) s1 = a; …
7
votes
6 answers

In C, are variables declared within a loop, local?

#include int a; void myproc() { int a = 2; while (a == 2) { int a = 3; printf("a = %d\t", a); break; } printf("a = %d\t", a); } int…
hardcoder
  • 415
  • 1
  • 5
  • 13
7
votes
5 answers

"Java concurrency in practice" - cached thread-safe number factorizer (Listing 2.8)

In the following code (copied from Java Concurrency in Practice Chapter 2, section 2.5, Listing 2.8): @ThreadSafe public class CachedFactorizer implements Servlet { @GuardedBy("this") private BigInteger lastNumber; @GuardedBy("this") private…
khachik
  • 28,112
  • 9
  • 59
  • 94
7
votes
3 answers

Table variable and exec

How can I use a table variable while executing a command string? DECLARE @FileIDs TABLE ( File_ID int ) insert into @FileIDs select ID from Files where Name like '%bla%'; DECLARE @testquery as varchar(max); set @testquery = 'select * from…
alex555
  • 1,676
  • 4
  • 27
  • 45
7
votes
5 answers

passing a local variable to thread. is it possible?

i'm working on gcc , i'm wondering if this is possible: I have a function (NOTmain but aLocalFn) and I declare a local variable in it. Then I pass this local argument as a thread argument. is it doable? or there is the chance (depending on what is…
nass
  • 1,453
  • 1
  • 23
  • 38
7
votes
3 answers

Local variable with same name as instance variable = unexpected results

ASP.NET 4.0 Webforms project. I have the following in my code-behind. public partial class _Default : System.Web.UI.Page { private string testVar; protected override void OnInit(EventArgs e) { string testVar = "test"; } …
Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
6
votes
2 answers

In C#, why can't I populate a local variable using its address, then use the variable later?

Consider the following code: private unsafe void Function() { int length; // This line raises error CS1686, "Local 'length' or its members cannot have their address taken and be used inside an anonymous method or lambda expression". …
Grimelios
  • 351
  • 2
  • 11
6
votes
5 answers

How to conditionally declare a local variable based on a template argument?

I would like to conditionally declare a local variable in a function, based on a template bool parameter. So if it is true it should be there, otherwise shouldn't be there in the sense that I don't want that variable to allocate memory on the stack…
6
votes
5 answers

Why are local variables not declared final in most open source java projects?

If I look at the java source source code in the OpenJDK or Hibernate or Apache I have yet to see any local variables declared final. This suggests that the developers of some of the most widely used java software libraries: do not believe the final…
user695654
  • 103
  • 5
6
votes
5 answers

Is there a performance hit of replacing local variables with arguments in Javascript?

Is there any performance hit for writing a function such that local var statements are replaced with arguments? Example: function howManyMatch(arr, pattern, /*ignored:*/ i, l, total) { l = arr.length; total = 0; for (i = 0, i < l; i++) { …
Steve Clay
  • 8,671
  • 2
  • 42
  • 48
6
votes
1 answer

Declaring a value as local once is slower than declaring it local each time

How is it possible that this piece of code: local t for n = 0, 255 do t = math.random(0, 255) ... end Is actually slower than this one? for n = 0, 255 do local t = math.random(0, 255) ... end Since I am accessing t more than once…
user6245072
  • 2,051
  • 21
  • 34
6
votes
1 answer

SQL Server : drop column @variable not working

I'm using Microsoft SQL Server and it seems that I cannot get around this issue. I have a table on which I will have some dynamic and static columns. Static columns would be name of product, type of product and the dynamic data would be some…
hoisu
  • 305
  • 2
  • 14