variable-scope

Tracking automatic variable lifetime?

This may not be possible, but I figured I'd ask... Is there any way anyone can think of to track whether or not an automatic variable has been deleted without modifying the class of the variable itself? For example, consider this code: const char* pStringBuffer; { std::string sString( "foo" ); pStringBuffer = sString.c_str(); }...

Scope and return values in C++

Hey!! I am starting again with c++ and was thinking about the scope of variables. If I have a variable inside a function and then I return that variable will the variable not be "dead" when it's returned because the scope it was in has ended? I have tried this with a function returning a string and it did work. Can anyone explain this?...

Child Scope & CS0136

The following code fails to compile stating "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else": var l = new List<string>(); l.Find(st => st.EndsWith("12")); string st = "why this fail...

How to resolve name collision when using c headers?

I am currently doing some socket programming using C/C++. To be able to use a somewhat cleaner interface, and a more OO structure, I decided to write a few simple wrapper classes around parts of the C socket API, but while doing so I stumbled upon a problem: Given the following code: // Global method int foo(int x) { return x; } /...

Good APIs for scope analyzers

I'm working on some code generation tools, and a lot of complexity comes from doing scope analysis. I frequently find myself wanting to know things like What are the free variables of a function or block? Where is this symbol declared? What does this declaration mask? Does this usage of a symbol potentially occur before initialization?...

LINQ Query - Explanation needed of why these examples are different.

I'm reading the book "LINQ Pocket Reference" and there is a particular example (slightly modified below) that I'm having difficulty getting my head around... The explanation in the book is a bit brief, so I was wondering if someone could break it down step-by-step for me so that it makes sense... IEnumerable<char> query2 = "Not what...

Private member variables scope - C++

I am just starting with C++ and got some problems in understanding how the scope for private member variables in a class works. Please see the below code class Foo{ private: std::vector<int> container; public: // other methods }; int main(int argc, char* argv[]) { Foo* foo = new Foo; // other method call...

Scope of variables in a delegate

I found the following rather strange. Then again, I have mostly used closures in dynamic languages which shouldn't be suspectable to the same "bug". The following makes the compiler unhappy: VoidFunction t = delegate { int i = 0; }; int i = 1; It says: A local variable named 'i' cannot be declared in this scope because it wou...

How can one create new scopes in python

In many languages (and places) there is a nice practice of creating local scopes by creating a block like this. void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } How can I implement this in python without getti...

Global variables and scope - C++

I am having small problem in making a global variable works. I am using Visual Studio 2008 and standard C++. I have two projects, one is a static library and second one is a test program which uses this library. I have a global variable in global.h like #ifndef GLOBAL_H #define GLOBAL_H #include <string> extern std::string globalWo...

How do I edit a global variable in a JQuery $.each function?

Ok, so that title probably doesn't explain my question well. Hopefully this makes sense. This is also my first application with jQuery, so forgive me if I'm doing something dumb. I have the following function: function getRandomImages(limit) { imagesArray = new Array(); $.getJSON('createImageArray.php', {limit: limit}, function...

Inheriting aliases inside UNIX /usr/bin/script

The UNIX "/usr/bin/script" command will create a running transcript of your shell session (see "man script" for more info). However, when inside a script instance, it seems to forget the parent shell's env vars, aliases, etc. The following example demonstrates how the "ll" alias I define is ignored inside "script": zsh> mkdir temp zsh...

Implement map in javascript that supports object methods as mapped functions?

I recently tried to use an implementation of map in javascript to create a bunch of items, then apply them to an objects add method. Firstly with a bog standard implementation of map. var map = function (fn, a) { for (i = 0; i < a.length; i++) { a[i] = fn(a[i]); } } Setup. var translateMenu = new Menu; var langu...

VB.NET variable scope

Are variables declared in a case statement local to that case statement - For example, in the following code, is it safe to use z like this? Or is the compiler just placing z at the procedure scope? Select Case x Case 6 Dim z As Integer = 6 Case 7 Dim z As Integer = 7 ...

Ruby on Rails - Scope of Instance Variables with Partial Views

Hello I do not understand how to use instance variable properly with partial views, I am hoping someone here can enlighten me. For example class MainController < ApplicationController def index @item_list = Item.find_all_item end def detail_display @current_selected = @item= Item.find(params[:id]) redirect_to :action => :...

When a variable goes out of scope does that mean it doesn't exist?

I'm not sure I understand scope - does an out-of-scope variable (I'm using Ruby) exist in memory somewhere or does it stop existing (I know you can't access it). Would it be inaccurate to say that an out-of-scope variable does not exist any more? Maybe this is a philosophical question. ...

Java object reference/scope question

If I have a member variable such as this (declared in the body of a class) private Dot[] dots=new Dot[numDots]; I loop through all members of this array, and: 1) Pass every Dot object to a function of another class, which: 2) Passes it to yet another function of a 3rd class, if some conditions are met 3) And the 3rd class changes s...

What Happens to an Object That Falls Out of Scope in Delphi?

When an object that is created within a function and the function is completed, what happens to the object if it wasn't explicitly destroyed? Do all variables need to be destroyed when they fall out of scope or are they taken care of when they fall out of scope? So for example, what happens to locallist after custom_function has been c...

function arguments

Hi, if I have: function Foo(f) { var f = f; } here inside the function variable f is local to the Foo (it has a function scope) but the variable f in argument list that is named in the same way why it isn't in conflict may be because it is bound inside the Foo.arguments object? In other languages we cannot declare an argument ...

Python: Why can't I modify the current scope within a function using locals()?

Why does creating/modifying a member of locals() not work within a function? Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> # Here's an example of what I expect to be possible in a function: >>> ...