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 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
"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
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
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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
by: Christian Christmann |
last post by:
Hi,
I've a a question on the specifier extern.
Code example:
void func( void )
{
extern int e;
//...
|
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...
|
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;
|
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;
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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 ...
| |