local-variables

At what moment is memory typically allocated for local variables in C++?

与世无争的帅哥 提交于 2019-11-30 14:32:41
问题 I'm debugging a rather weird stack overflow supposedly caused by allocating too large variables on stack and I'd like to clarify the following. Suppose I have the following function: void function() { char buffer[1 * 1024]; if( condition ) { char buffer[1 * 1024]; doSomething( buffer, sizeof( buffer ) ); } else { char buffer[512 * 1024]; doSomething( buffer, sizeof( buffer ) ); } } I understand, that it's compiler-dependent and also depends on what optimizer decides, but what is the typical

At what moment is memory typically allocated for local variables in C++?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 10:53:34
I'm debugging a rather weird stack overflow supposedly caused by allocating too large variables on stack and I'd like to clarify the following. Suppose I have the following function: void function() { char buffer[1 * 1024]; if( condition ) { char buffer[1 * 1024]; doSomething( buffer, sizeof( buffer ) ); } else { char buffer[512 * 1024]; doSomething( buffer, sizeof( buffer ) ); } } I understand, that it's compiler-dependent and also depends on what optimizer decides, but what is the typical strategy for allocating memory for those local variables? Will the worst case (1 + 512 kilobytes) be

Ruby: method inexplicably overwritten and set to nil

ぃ、小莉子 提交于 2019-11-30 07:31:41
问题 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? 回答1: 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

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

元气小坏坏 提交于 2019-11-30 07:21:54
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 designated by an lvalue, overload resolution to select the constructor for the copy is first performed

Using function arguments as local variables

蓝咒 提交于 2019-11-30 06:48:39
Something like this (yes, this doesn't deal with some edge cases - that's not the point): int CountDigits(int num) { int count = 1; while (num >= 10) { count++; num /= 10; } return count; } What's your opinion about this? That is, using function arguments as local variables. Both are placed on the stack, and pretty much identical performance wise, I'm wondering about the best-practices aspects of this. I feel like an idiot when I add an additional and quite redundant line to that function consisting of int numCopy = num , however it does bug me. What do you think? Should this be avoided? As a

Where are Java final local variables stored?

我是研究僧i 提交于 2019-11-30 06:45:41
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 doing this (rather than simply creating env as a member variable of the class and referencing it as such

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

醉酒当歌 提交于 2019-11-30 04:11:42
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: "{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc) There is now an

jQuery: how to access outside variable?

梦想的初衷 提交于 2019-11-29 18:18:37
I am in a situation that needs be solve with this way; need convert a local variable to a global variable . There is an example returning image's real width and height which i found these method from this answer. . Need to convert local varialbes pic_real_height and pic_real_width to global variables with returning their true values. Here is jsFiddle. CSS : img { width:0px; height:0px; }​ jQuery : console.log($('.imgCon img').height());//returns 0 var img = $('.imgCon img')[0]; // Get my img elem var pic_real_width, pic_real_height; $('<img/>').attr('src', $(img).attr('src')).load(function() {

Ruby variable assignment in a conditional “if” modifier

試著忘記壹切 提交于 2019-11-29 15:55:07
I have a question about how the Ruby interpreter assigns variables: I use this quite often: return foo if (foo = bar.some_method) where some_method returns an object or nil. However, when I try this: return foo if (true && (foo = bar.some_method)) I get: NameError: undefined local variable or method foo for main:Object. What is the difference in evaluation between the first and second lines that causes the second line to error? Read it carefully : Another commonly confusing case is when using a modifier if: p a if a = 0.zero? Rather than printing true you receive a NameError, “undefined local

Why default value of local variables in C is same?

廉价感情. 提交于 2019-11-29 13:06:26
#include <stdio.h> int main() { int i,j=3; i=4+2*j/i-1; printf("%d",i); return 0; } It will print 9 every time,though i is not initialized, So, it must print any garbage value. Please Explain... The value of an uninitialized local variable in C is indeterminate and reading it can invoke undefined behavior. Now, repeatedly executing a particular program compiled with a particular compiler in a particular environment (as you are doing) is likely to yield the same (still undefined, of course) behavior. This could be because the OS will usually give your process the same range of logical memory