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.

1118 questions
1087
votes
21 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
  • 5,923
  • 8
  • 45
  • 82
317
votes
8 answers

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

I'm new to Python, so this is probably 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…
froadie
  • 71,770
  • 69
  • 154
  • 228
142
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,736
  • 2
  • 13
  • 21
105
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
  • 12,749
  • 6
  • 61
  • 126
89
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,083
  • 7
  • 26
  • 33
81
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,096
  • 3
  • 24
  • 21
81
votes
10 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…
77
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
  • 113,402
  • 22
  • 170
  • 242
67
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
  • 35,369
  • 19
  • 80
  • 136
63
votes
3 answers

How to make a variable inside a try/except block public?

How can I make a variable inside the try/except block public? import urllib.request try: url = "http://www.google.com" page = urllib.request.urlopen(url) text = page.read().decode('utf8') except (ValueError, RuntimeError, TypeError,…
x0x
  • 1,714
  • 3
  • 21
  • 42
57
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
  • 13,346
  • 17
  • 65
  • 104
56
votes
5 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. So, for example, bye = '' def hi(): global bye something something bye = 5 sigh = 10 hi() print bye The above works fine as it should. Since I want…
askance
  • 847
  • 3
  • 12
  • 17
54
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
  • 12,352
  • 16
  • 69
  • 126
51
votes
5 answers

Undefine variable in Ruby

Let's say I'm using irb, and type a = 5. How do I remove the definition of a so that typing a returns a NameError? Some context: later I want to do this: context = Proc.new{}.binding context.eval 'a = 5' context.eval 'undef a' # though this doesn't…
Peter
  • 114,224
  • 47
  • 167
  • 205
45
votes
4 answers

How to declare a variable in SQL Server and use it in the same Stored Procedure

Im trying to get the value from BrandID in one table and add it to another table. But I can't get it to work. Anybody know how to do it right? CREATE PROCEDURE AddBrand AS DECLARE @BrandName nvarchar(50), @CategoryID int, @BrandID int SELECT…
Nicklas
  • 481
  • 1
  • 4
  • 7
1
2 3
74 75