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
1154
votes
20 answers

Can a local variable's memory be accessed outside its scope?

I have the following code. #include int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The…
Avi Shukron
  • 6,088
  • 8
  • 50
  • 84
397
votes
7 answers

What's the scope of a variable initialized in an if statement?

This could be a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == '__main__': x = 1 print x In other languages I've worked in, this code would throw an exception, as the x…
froadie
  • 79,995
  • 75
  • 166
  • 235
288
votes
14 answers

UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)

When I try this code: a, b, c = (1, 2, 3) def test(): print(a) print(b) print(c) c += 1 test() I get an error from the print(c) line that says: UnboundLocalError: local variable 'c' referenced before assignment in newer versions…
tba
  • 6,229
  • 8
  • 43
  • 63
144
votes
4 answers

Why do local variables require initialization, but fields do not?

If I create a bool within my class, just something like bool check, it defaults to false. When I create the same bool within my method, bool check(instead of within the class), i get an error "use of unassigned local variable check". Why?
nachime
  • 1,826
  • 4
  • 16
  • 25
106
votes
2 answers

How do you extract local variable information (address and type) from a Delphi program or the compiler-generated debug info?

My goal is: Given a suspended thread in a Delphi-compiled 32 or 64-bit Windows program, to walk the stack (doable) Given stack entries, to enumerate the local variables in each method and their values. That is, at the very least, find their address…
David
  • 13,360
  • 7
  • 66
  • 130
101
votes
9 answers

"Life-time" of a string literal in C

Wouldn't the pointer returned by the following function be inaccessible? char *foo(int rc) { switch (rc) { case 1: return("one"); case 2: return("two"); default: …
user113454
  • 2,273
  • 9
  • 28
  • 35
94
votes
11 answers

Default values and initialization in Java

Based on my reference, primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line…
86
votes
2 answers

In ArrayBlockingQueue, why copy final member field into local final variable?

In ArrayBlockingQueue, all the methods that require the lock copy it to a local final variable before calling lock(). public boolean offer(E e) { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; …
mjlee
  • 3,374
  • 4
  • 27
  • 22
82
votes
6 answers

C++ local variable destruction order

Is there a defined order in which local variables are deallocated in C++ (11) ? To be more concise: In which order will side effects of the destructors of two local variables in the same scope become visible? e.g.: struct X{ ~X(){/*do…
gexicide
  • 38,535
  • 21
  • 92
  • 152
78
votes
6 answers

Access a function variable outside the function without using "global"

I am trying to access a local function variable outside the function in Python. I can make code like this work with global variables: bye = '' def hi(): global bye bye = 5 sigh = 10 hi() print(bye) Next, I tried this code, hoping to…
askance
  • 1,077
  • 4
  • 14
  • 19
74
votes
4 answers

Why can't we access static content via uninitialized local variable?

Take a look at below code: class Foo{ public static int x = 1; } class Bar{ public static void main(String[] args) { Foo foo; System.out.println(foo.x); // Error: Variable 'foo' might not have been initialized …
Pshemo
  • 122,468
  • 25
  • 185
  • 269
68
votes
4 answers

angular - using async pipe on observable and bind it to local variable in html
Hi I have a observable user$ with a lot of properties (name, title, address...) component{ user$:Observerable; constructor(private userService:UserService){ this.user$ = this.userService.someMethodReturningObservable$() } } Is there…
Han Che
  • 8,239
  • 19
  • 70
  • 116
66
votes
10 answers

How to access a local variable from a different function using pointers?

May I have any access to a local variable in a different function? If so, how? void replaceNumberAndPrint(int array[3]) { printf("%i\n", array[1]); printf("%i\n", array[1]); } int * getArray() { int myArray[3] = {4, 65, 23}; return…
Radek Simko
  • 15,886
  • 17
  • 69
  • 107
57
votes
3 answers

Using locals() and format() method for strings: are there any caveats?

Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = 'john', age=22): userId = user.getUserId() return "Name: {name}, age: {age}, userid:{userId}".format(**locals()) I…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
56
votes
8 answers

Returning string from C function

I haven't used C in over 3 years, I'm pretty rusty on a lot of things. I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this. Here is my code: #include…
MrWolf
  • 690
  • 1
  • 6
  • 6
1
2 3
86 87