Re: Local variables - quick question

From: Stefan Turalski \(stic\) (stefan.turalski_at_kruk-inkaso.com.pl)
Date: 02/27/04


Date: Fri, 27 Feb 2004 09:12:26 +0100


Uzytkownik "Jon Skeet [C# MVP]" <skeet@pobox.com> napisal w wiadomosci
news:MPG.1aa8ffe6ede9c68a98a27a@msnews.microsoft.com...
> Stefan Turalski (stic) <stefan.turalski@kruk-inkaso.com.pl> wrote:
> > > > btw. Is there a way to read ahead variable scope?
> > > What exactly do you mean by "read ahead variable scope"?
> > What should I do if I want to examine one of my variables if it is in
its
> > scope or no ?
> In what context? You can't use a variable which isn't in scope within
> code.

Yes, that is why i can't write:
for(int i=0;i<10;i++){}
Console.Write("{0}",i);

And this I undestood well, but what if I want to check if there is
possiblity to declare class argument int i - only signal for me is compiler
errors ?
I really try but I don't get it (yet ;) why compiler don't allow me to use
'child' scope variable on this level ?
for(int i=0;i<10;i++){}
int i=0;

This is as you said variable which isn't in scope - 'child' scope is this
for-loop scope..

> > Debbuger could do it in some why ?
> I don't believe even the debugger will show you a variable out of its
scope.
But there have to be a way to go over this and see what is happening with
this all 'non-used' objects ? I have to go over garbage collector ?

> > Variables declared localy as far as I know should be destroyed by GC
after
> > their scope ends.. I'm I right ? (rather not, but I have no idea what
the
> > answer is)
> Variables aren't "destroyed", but they will no longer be treated as
> being alive by the garbage collector. In other words, just because a
> variable comes to the end of its scope doesn't mean that an object it
> refers to will immediately be garbage collected, but it means that the
> variable will no longer stop if from being garbage collected. (In fact,
> when not in debug mode, it's actually after the last use that it's no
> longer "used".)

So when I still try to do something like for-loop in which I use i =
iterator, and after that I would like to use int i - new declaration. That
could not be done, becouse of this obcjet already could be somewhere in
memory ? I don't get this idea of "A local variable named 'i' cannot be
declared in this scope because it would give a different meaning to 'i',
which is already used in a 'child' scope to denote something else"

> > - another good link (this one I have reed before writing on group - and
then
> > I was wondering what: "Scopes can be nested, and an inner scope may
redeclare the meaning of a
> > name from an outer scope."
> It means you can have, say, a local variable with the same name as an
> instance variable.

Ok, so why my int i; isn't instance variable ??

> > mean ? I asume that when I write another for() loop with the same int i
> > iterator I only redeclare it ?
> No - you should read onto the next sentence:
> <quote>
> (This does not, however, remove the restriction imposed by Section 3.3
> that within a nested block it is not possible to declare a local
> variable with the same name as a local variable in an enclosing block.)
> </quote>

I was thinking about something like this
for(int i=0;i<10;i++){ do sth A..}
for(int i=10;i>0;i--){ do sth B..}

And this works well, both 'child' scopes are using the same name for
variable and there is no errors, so another time I ask -> why class variable
(I think instance ?) isn't allowed as well ?

> > What is the dipper (memory?) backgroud of this 'redeclare' ?
> Not sure what you're after here. Names are just names - by the time
> everything's compiled there may still be a mapping of name to (eg)
> member location or place in the stack for a local variable, but unless
> you're using reflection, the runtime itself won't be using that map
> much.
I was wondering how my previous thing works, if I have two declarations of
int i - there must be a way to forget about this first name and make another
object for second 'i' - and why the same thing couldn't be done for int i -
not in local for-loop scope but at class level.

I really try to understood this... really..

--
greatly thanks for your time
stic


Relevant Pages

  • Re: scope and linkage rule, very confusing!
    ... >> function scope ... >> int f{ ... Labels that can be the targets of a goto statement, ... > both linkage and storage duration. ...
    (comp.lang.c)
  • scope, linkage and storage duration
    ... /* Scope, linkage and storage duration in C ... struct t3 y10; ...
    (comp.lang.c)
  • Re: Porting from visual C to gcc problem
    ... the compiler must have come across the typedef ... >>function declaration, ... then the scope lasts to through the body to ... >>int func; ...
    (comp.lang.c.moderated)
  • Re: In which scope should variables be declared?
    ... >specific scope I can. ... >int foo ... of this loop to the next, DON'T declare it inside the for loop. ... > In this case, if the declaration maps to any cpu time when compiled, ...
    (comp.lang.c)
  • class variables: scoping
    ... static int x; ... scope, but is reinitialized each time the method is called. ... Now, I would like to have a variable which has function scope, ... time I instantiate an instance of Foo a new such variable is created. ...
    (comp.lang.cpp)

Loading