Topic: scope of variables


Author: claus@faerber.muc.de (=?ISO-8859-1?Q?Claus_Andr=E9_F=E4rber?=)
Date: 1996/12/06
Raw View
Hallo Dr, hallo everybody,

Dr J I Vousden <psran@csv.warwick.ac.uk> wrote:

> void foo()
> {
>  int n;
>  int tmp[20];
>
>  for( n = 0; n < 20; n++ )
>   tmp[n] = n;

Here tmp[] is an array local to foo(), which is initialized.

> }
>
> void foo1()
> {
>  int n;
>  int tmp[20];
>
>  for( n = 0; n < 20; n++ )
>   printf("%d ", tmp[n]);
>  printf("\n");

Here it is a completly different array, which has never been
initialized. The ouptput of the program is undefined in C++. The
fact that it produces the ouptut you described is PURE LUCK. Just
try declaring foo1 as foo1(int a) and watch what happens.

--
Claus Andre Faerber <claus@faerber.muc.de>, <http://www.muc.de/~cfaerber/>
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Randy Charles Morin" <rmorin@atlantis.com>
Date: 1996/12/06
Raw View
This code works only because the run-time allocated both arrays in the
exact same location of memory.  Therefor the second array assumed the same
state as the first.  This code works only because of coincidence.

Randy Charles Morin
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: psran@csv.warwick.ac.uk (Dr J I Vousden)
Date: 1996/12/02
Raw View
I have an elementary question about scope.
Perhaps someone could clarify the scope of declared varaibles for me?
Below is a bit of code, which compiles and produces the output :
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

void foo();
void foo1();

void main()
{
 foo();
 foo1();
}

void foo()
{
 int n;
 int tmp[20];

 for( n = 0; n < 20; n++ )
  tmp[n] = n;
}

void foo1()
{
 int n;
 int tmp[20];

 for( n = 0; n < 20; n++ )
  printf("%d ", tmp[n]);
 printf("\n");
}
----------------------------------------------------

But is this really what should happen?  If I declare & assign values
to the tmp[] array in the main function, I don't get the same output.
What is going on?

Thanks,
Janet.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Jesper Dybdal <jd@pip.dknet.dk>
Date: 1996/12/02
Raw View
psran@csv.warwick.ac.uk (Dr J I Vousden) wrote:
>I have an elementary question about scope.
>Perhaps someone could clarify the scope of declared varaibles for me?
>Below is a bit of code, which compiles and produces the output :
>0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 [snip]

What happens is that foo1 prints out 20 numbers from an uninitialized
array.

"Uninitialized" means that the array elements have no values that can
predicted by reading the C standard.  However, the array elements
exist; you print out the values that happen to be in the memory
locations allocared to "tmp".

The reason you see the numbers assigned by the previous call of foo is
simply that the array in foo1 happens to re-use the memory formerly in
use by the array in foo.  If you interchange the declarations of "n"
and "tmp" in foo1 or add a formal parameter to foo1, you'll probably
(but not necessarily) see a slightly different behaviour.

In short: the two "tmp" arrays have nothing to do with each other;
they are in different scopes; they do not exist at the same time; they
just happen to use the same physical memory locations, one after the
other.  Do not count on them doing do!
--
Jesper Dybdal, Denmark <jd@pip.dknet.dk>


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Matt Seitz <mseitz@meridian-data.com>
Date: 1996/12/03
Raw View
C++ does not define what the output from this program "should" be.  As
far as C++ is concerned:

1.  the "tmp" in foo and the "tmp" in foo1 are two entirely separate
variables.

2.  the values of tmp[n] in foo1 are not defined
(C++ Refernce Manual section r.8.4: "The initial values of automatic and
register variables that are not initialized are undefined.").

Since foo1 does not initialize tmp, the compiler may choose whatever
values it wants to store there.  In this case, it appears that your
compiler is choosing to store the foo1 "tmp" variable in the same memory
location as the foo "tmp" variable.  The result is that when you read
the values in foo1, you see the values that you stored during foo.  That
is strictly a conincidence, C++ does not guarantee that it will always
be true.

I suspect you would see the same result if you edited foo1 and changed
the name of "tmp" to something else.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]