local-variables

Returning reference to a local variable

让人想犯罪 __ 提交于 2019-11-29 11:56:57
Why can this code run successfully in Code::block. The IDB just reports warning: "reference to local variable ‘tmp’ returned", but ouput the result "hello world" successfully. #include <iostream> #include<string> using namespace std; const string &getString(const string &s) { string tmp = s; return tmp; } int main() { string a; cout<<getString("hello world")<<endl; return 0; } prvit Maybe this link will help you. Upon leaving a function, all local variables are destroyed. By returning a reference to tmp , you are returning a reference to an object that soon ceases to exist (that is,

Does an int in Objective-C have a default value of 1?

坚强是说给别人听的谎言 提交于 2019-11-29 11:04:50
I have this simple line of code: int x; x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1. Does an int have a default value of 1?! No. int has an undefined default value. It just happens to be 1 in this case. It could just as easily be -18382 or 22 or 0xBAADF00D . Always initialize your variables in C. The initial value is undefined, and in this case will be whatever happened to be in that memory location before x started using it. (Depending on the surrounding code, you might find that in your specific case it's always 1 , but you can't be

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

為{幸葍}努か 提交于 2019-11-29 09:33:19
问题 The C++11 standard states that, if the conditions for copy elision are met ( §12.8/31 ), the implementation shall treat a return ed local lvalue variable and function parameters, as an rvalue first (move), and if overload resolution doesn't succeed as detailed, shall then treat it as an lvalue (copy). §12.8 [class.copy] p32 When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is

Declaring a useless local variable

对着背影说爱祢 提交于 2019-11-29 09:17:58
So this is an odd one, I know the code itself is fairly useless, but what I'm wondering why I get the error: I was writing some code, I had written this: if(scan.hasNextInt()) int row = scan.nextInt(); Wasn't thinking about variable scope at the time, obviously this is useless because I can't use row past the if anyway. What I don't get is why I got the error I did: > javac hw.java hw.java:25: '.class' expected int row = scan.nextInt(); ^ hw.java:25: not a statement int row = scan.nextInt(); ^ hw.java:25: illegal start of expression int row = scan.nextInt(); ^ hw.java:25: ';' expected int row

Where are Java final local variables stored?

﹥>﹥吖頭↗ 提交于 2019-11-29 07:01:15
问题 Take the following example: public void init() { final Environment env = new Environment(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { env.close(); } }); } Firstly, where is env stored? Is it: copied by the compiler into a hidden member variable of the inner class that references it copied to, and referenced on, the heap left on the stack and somehow referenced there something else My guess is the first option. Secondly, do any performance issues that arise from

Ruby: method inexplicably overwritten and set to nil

廉价感情. 提交于 2019-11-29 04:31:45
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? Is this expected behavior or a ruby bug? Names on the left hand side of assignments get set to nil , even if the code can't be reached as in the if false case. >> foo NameError: undefined local variable or method `foo' for main:Object ... >> if false .. foo = 1 .. end #=> nil >> foo #=> nil When Ruby tries to resolve barewords, it first looks for local

access variables of outer class in Java

ⅰ亾dé卋堺 提交于 2019-11-29 03:27:27
in Java android application how can i access variables of outer class from the inner anonymous class ? Example: ProgressDialog dialog = new ProgressDialog(this); ..... send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //here i'd like to do something with **dialog** variable ....... } }); If the dialog variable is a field of the outer class, you can use this prefixed with the outer class name ( a qualified this ): send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ProgressDialog dlg = OuterClass.this.dialog; ....... } });

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

余生长醉 提交于 2019-11-29 02:04:29
问题 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 had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: "

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives “true”, and @@wah.nil? gives error again?

半腔热情 提交于 2019-11-29 01:38:36
Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for #<Object:0x3794c> from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized class variable @@wah in Object from (irb):3 why the instance variable treated differently than a local and class variable? In Ruby, most uninitialized or even non-existing variables evaluate to nil . This is true for local variables, instance variables and global variables: defined? foo #=> nil local_variables #=> [] if false foo = 42 end defined? foo #=>

Why does local variable kill my global variable?

浪子不回头ぞ 提交于 2019-11-28 22:51:21
Sorry for this question, but this issue really screwed up my day. The following Code alerts 10 as it should: var globalId='10'; function check(){ alert(globalId); } check(); But this next code alerts undefined : var globalId='10'; function check(){ alert(globalId); var globalId; } check(); I am aware that if I declare a variable in a function its a local variable, but if I already declared it as global, how can it be that my alerts says undefined ? This is an easy example, but in my original code I did a lot of stuff in between the beginning of the function, then a long way down I checked to