All Questions

818
votes
20answers
238732 views

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

I have the following code. int * foo() { int a = 5; return &a; } int main() { int* p = foo(); cout << *p; *p = 8; cout << *p; } And the code is just running...
42
votes
9answers
25271 views

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...
30
votes
4answers
32312 views

returning a local variable from function in C

#include <stdio.h> int foo1(void) { int p; p = 99; return p; } char *foo2(void) { char buffer[] = "test_123"; return buffer; } int *foo3(void) { int t[3] = {1,2,3}; ...
63
votes
2answers
2247 views

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(); ...
67
votes
9answers
11500 views

"life-time" of string literal in C

Wouldn't the pointer returned by the following function inaccessible? char *foo( int rc ) { switch (rc) { case 1: return("one"); case 2: return("two"); ...
37
votes
1answers
1912 views

Why is `a = a` `nil` in Ruby?

I watched this video. Why is a = a evaluated to nil if a is not defined? a = a # => nil b = c = q = c # => nil
138
votes
4answers
15295 views

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 o...
149
votes
7answers
54070 views

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 langu...
50
votes
8answers
89792 views

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; Syst...
19
votes
7answers
57416 views

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....

Previous Next