473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple scope question


I was paging through Coplien's book "Advanced C++ Programming Styles
and Idioms" this afternoon and found some code that looked something
like

void sort(vector<foo> a)
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a[i]) {
swap(a[i],a[j]);
++flip;
}
}
} while (flip > 0);
return;
}
Now the question: This code doesn't work on my compiler, because
the 'flip = 0' shadows the declaration of 'flip' outside the 'do'
loop. The 'for' loop increments one 'flip', while the 'do' loop
evaluates another.

Is this how it's supposed to work? (I think yes.)
Did it work a different way when Coplien wrote the book? (I think no.)
And are there online errata for Coplien's book? (I couldn't find any.)

Thanks,
-Arthur
Jul 19 '05 #1
6 2333
This code doesn't work on my computer, either, but it's not because of
shadowing. It doesn't work because 'vector<foo> a' is passed as a copy
instead of as a reference. You can see this by tracing with the
debugger. Change it to 'vector<foo>& a' and it should work; it does on
my computer.

But...

** How can anyone explain the following scoping weirdness?

I found that the do-loop looks at the outermost of these two 'flip's
that is *initialized*. When I changed the outermost from 'int flip;' to
'int flip = 1;' I got an infinite loop because then (apparently) the
do-loop always looked at the outermost 'flip'.

Suzanne

Arthur J. O'Dwyer wrote:
I was paging through Coplien's book "Advanced C++ Programming Styles
and Idioms" this afternoon and found some code that looked something
like

void sort(vector<foo> a)
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a[i]) {
swap(a[i],a[j]);
++flip;
}
}
} while (flip > 0);
return;
}
Now the question: This code doesn't work on my compiler, because
the 'flip = 0' shadows the declaration of 'flip' outside the 'do'
loop. The 'for' loop increments one 'flip', while the 'do' loop
evaluates another.

Is this how it's supposed to work? (I think yes.)
Did it work a different way when Coplien wrote the book? (I think no.)
And are there online errata for Coplien's book? (I couldn't find any.)

Thanks,
-Arthur


Jul 19 '05 #2

"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
** How can anyone explain the following scoping weirdness?

I found that the do-loop looks at the outermost of these two 'flip's
that is *initialized*. When I changed the outermost from 'int flip;' to
'int flip = 1;' I got an infinite loop because then (apparently) the
do-loop always looked at the outermost 'flip'.

Suzanne

void sort(vector<foo> a)
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a[i]) {
swap(a[i],a[j]);
++flip;
}
}
} while (flip > 0);
return;
}

Suzanne, it has nothing to do with the first flip variable being initialized
or not (although it DOES have to do with the value it is is being
initialized TO...more on that later). The do itself loop can only 'see' the
first flip instance, because the flip declared inside the the do loop is not
visible at the (outer) scope of the do loop. Everything inside the {} of
the do loop is in an inner scope (if that's the right term), and anything
declared there is out-of-scope outside the {}, including in the while
portion of that loop.

The reason for the infinite loop is that, when you declare it as 1, it will
always be 1 (which is > 0), because the ++flip is operating on the second,
inner flip declaration. If you set it to -1 initially, then the do loop
will excute exactly once, because "-1 > 0" is false.

Howard


Jul 19 '05 #3

On Thu, 3 Jul 2003, Howard wrote:

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message...

I was paging through Coplien's book "Advanced C++ Programming Styles
and Idioms" this afternoon and found some code that looked something
like

void sort(vector<foo> a)
[Re Suzanne's comment: This wasn't how Coplien actually wrote the
function, of course. It *was* a bubblesort implementation, but
Coplien's focus was on data structures, while my focus was on that
weird nested-scope thing. So I had just been going to put "..."
everywhere, except that "..." is a token, and then once I started
putting pseudo-C++ everywhere it was needed, it ended up being
a whole function. So the type of 'a' is completely irrelevant, and
it's my fault for trying to make the function look complete. :-) ]
{
int flip;
do {
for (int i=0, flip = 0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a[i]) {
swap(a[i],a[j]);
++flip;
}
}
} while (flip > 0);
return;
}

Now the question: This code doesn't work on my compiler, because
the 'flip = 0' shadows the declaration of 'flip' outside the 'do'
loop. The 'for' loop increments one 'flip', while the 'do' loop
evaluates another.

Is this how it's supposed to work? (I think yes.)
Yes, this is how it is supposed to work.

<snip>
Did it work a different way when Coplien wrote the book? (I think no.)


Yes, for loop variables used to have scope that extended to the whole scope
in which the for loop resided. In this case, the sort function itself.
That made the "flip = 0" simply an assignment (as you intended) and not a
declaration.


So what happened to the first definition of 'flip' under these old rules?
Was it treated as a tentative definition? If it had been initialized
originally, and then "re-initialized" in the for loop, would that be an
error? (Just curious.)
The fix is to initialize flip outside (before) the for loop. Looking at
your code, I image you want to initialize it to 0 where you declared it,
outside the do loop, and simply leave it out of the for loop's statement
altogether.


Actually, the correct place for the initialization is right where Coplien
put it - as the first statement executed after (or before, or concurrently
with) the initialization of 'i' to 0. If you initialize 'flip' outside
the 'do' loop, the bubblesort never terminates.

-Arthur

Jul 19 '05 #4


Howard wrote:
[snip]
The fix is to initialize flip outside (before) the for loop. Looking at
your code, I image you want to initialize it to 0 where you declared it,
outside the do loop, and simply leave it out of the for loop's statement
altogether.


That would break the bubble sort !

But he could move the definition of flip inside the do loop
and initialize it there, thus dropping the second definition
from the for loop:

void sort(vector<foo> a)
{
do {

int flip = 0;

for (int i=0; i < a.size(); ++i)
{
for (int j=0; j < i; ++j)
if (a[j] < a[i]) {
swap(a[i],a[j]);
++flip;
}
}
} while (flip > 0);
return;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message
news:Pi***********************************@unix45. andrew.cmu.edu...

So what happened to the first definition of 'flip' under these old rules?
Was it treated as a tentative definition? If it had been initialized
originally, and then "re-initialized" in the for loop, would that be an
error? (Just curious.)


It was actually the definition. The difference comes at the for loop
clause, where the flip=0 was treated as an assignment instead of a
definition. Similar to this:

int a;
....
a = 0;

There you have a declaration, and a later assignment. In the old standard,
it did not matter that the assignment was part of a for loop initializer
clause. That's been changed so that the for loop initializer clause is
consistent with a multiple-declaration statement, such as

int b, a = 0;

Since that's a declaration of a, not a simple assignment, the for loop was
changed to behave the same way.

-Howard
Jul 19 '05 #6

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
altogether.


That would break the bubble sort !
--
Karl Heinz Buchegger
kb******@gascad.at

Quite right. The code was obviously wrong somehow, but I didn't bother to
try to analyze how to fix it properly. (What I was pointing out as a fix
was really how to fix the for loop problem in general, not to solve the
problem with the algorithm implementation.)

-Howard
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6699
by: dp | last post by:
I am new to VB.NET and I have a simple question. How do I show a form from a command button click event? The code I have below is not working. I am trying to show the form frmAgent. What am I...
9
2010
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
12
4382
by: Jeff B. | last post by:
If an object implements the IDisposable interface, should I always call the Dispose method or is just setting it to null and letting the GC handle it sufficient? Here is the pattern I've been...
4
14868
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
3
1289
by: Ryan Pedersen | last post by:
I have a small program that executes a single very long running background thread today. The scope of all variables in the method that is running in this thread today are all local to the...
7
2164
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
24
3403
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
5
2221
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
26
1740
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.