473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

STL: Scalar increment inside insert() - bad?

Hi,

I've got a section of code that basically goes like so:

int l_Index = 0;
std::set<intl_Set;
while(l_Index < 10)
{
l_Set.insert(l_Index++);
}

This USUALLY works okay (insert() doesn't appear to have
side-effects, so l_Index goes up by one each time), but at some point,
my app gets into a state where, by breaking into it with WinDBG, I can
see that the loop is repeating, but l_Index is not incrementing. I
know the insert() is working because I can step through it AND I can
see that memory is increasing.

QUESTION: Is this code not a good idea? If not, why not? The
easy solution is to move the increment OUT of the insert(), but I want
to be sure that it will solve the problem, since this state is
difficult to reproduce, and just because the new code SEEMS to work
doesn't mean I've solidly removed the problem.

Thanks in advance for any help on this one.

Allan Stirrett.

Jan 18 '07 #1
6 1430
"Allan Stirrett" <as******@tandbergtv.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
: Hi,
:
: I've got a section of code that basically goes like so:
:
: int l_Index = 0;
: std::set<intl_Set;
: while(l_Index < 10)
: {
: l_Set.insert(l_Index++);
: }
This is totally valid code, as far as I can tell.
l_Index is a local variable, is not modified within the 'insert' call.
The post-increment will be applied anytime after the parameter to
insert() has been copied (may be before or after the actual call
of the function, but this won't matter here).

: This USUALLY works okay (insert() doesn't appear to have
: side-effects, so l_Index goes up by one each time), but at some point,
: my app gets into a state where, by breaking into it with WinDBG, I can
: see that the loop is repeating, but l_Index is not incrementing. I
: know the insert() is working because I can step through it AND I can
: see that memory is increasing.
Could it be that you were looking at an optimized build? What is then
observed in a debugger can then often be unexpected/confusing.

: QUESTION: Is this code not a good idea? If not, why not?
Well, there sure are better ways to write it...
For example:
std::set<intl_Set;
for( int l_Index=0 ; l_Index<10 ; ++l_Index )
l_Set.insert(l_Index);

: The
: easy solution is to move the increment OUT of the insert(), but I want
: to be sure that it will solve the problem, since this state is
: difficult to reproduce, and just because the new code SEEMS to work
: doesn't mean I've solidly removed the problem.
There is no technical problem in the code fragment that you posted
(although its style/readability could be improved IMHO).

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jan 19 '07 #2
Ivan Vecerina wrote:
"Allan Stirrett" <as******@tandbergtv.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
: Hi,
:
: I've got a section of code that basically goes like so:
:
: int l_Index = 0;
: std::set<intl_Set;
: while(l_Index < 10)
: {
: l_Set.insert(l_Index++);
: }
This is totally valid code, as far as I can tell.
l_Index is a local variable, is not modified within the 'insert' call.
The post-increment will be applied anytime after the parameter to
insert() has been copied (may be before or after the actual call
of the function, but this won't matter here).
Thank you for the affirmation. The code is so simple I'm starting to
get the feeling it's a debugger issue more than a coding problem.
: This USUALLY works okay (insert() doesn't appear to have
: side-effects, so l_Index goes up by one each time), but at some point,
: my app gets into a state where, by breaking into it with WinDBG, I can
: see that the loop is repeating, but l_Index is not incrementing. I
: know the insert() is working because I can step through it AND I can
: see that memory is increasing.
Could it be that you were looking at an optimized build? What is then
observed in a debugger can then often be unexpected/confusing.
That's always a possibility. I'll double-check that it is debug code
or at least release with symbols.
: QUESTION: Is this code not a good idea? If not, why not?
Well, there sure are better ways to write it...
For example:
std::set<intl_Set;
for( int l_Index=0 ; l_Index<10 ; ++l_Index )
l_Set.insert(l_Index);
That is true. The example I gave is quite simplified: the l_Index is
actually set one of several ways in a large if() block, hence the
while() instead of for(). Alternatively, I could use:

... set l_Value at some point somehow ...
... set l_MaxIndex at some point somehow ...
for( ; l_Index < l_MaxIndex; ++l_Index )
l_Set.insert(l_Index);

though that would stir the whole "for() statement with missing parts"
debate!

Thank you for the input & suggestions.

Allan.

Jan 19 '07 #3
On Fri, 19 Jan 2007 08:10:14 +0100, Ivan Vecerina <_I*******************@ivan.vecerina.comwrote:
"Allan Stirrett" <as******@tandbergtv.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
: Hi,
:
: I've got a section of code that basically goes like so:
:
: int l_Index = 0;
: std::set<intl_Set;
: while(l_Index < 10)
: {
: l_Set.insert(l_Index++);
: }
....
There is no technical problem in the code fragment that you posted
(although its style/readability could be improved IMHO).
Agreed. There's nothing magic here, and set<T>::insert() isn't special in
any way. You evaluate the expression l_Index++ every time you pass through
the loop, simple as that.

Just because I'm curious: what are those annoying 'l_' prefixes for?
For me, they break the flow as I'm reading -- plus, the 'l' looks a lot like '1'.
I'd prefer this code (plus the switch to 'for' suggested by I.V.):

std::set<inttheset;

int i = 0;
while(i < 10) {
theset.insert(i++);
}

BR,
Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jan 19 '07 #4
Jorgen Grahn wrote:
On Fri, 19 Jan 2007 08:10:14 +0100, Ivan Vecerina <_I*******************@ivan.vecerina.comwrote:
There is no technical problem in the code fragment that you posted
(although its style/readability could be improved IMHO).

Agreed. There's nothing magic here, and set<T>::insert() isn't special in
any way. You evaluate the expression l_Index++ every time you pass through
the loop, simple as that.
Indeed, checking back, the loop DID eventually exit after several hours
of execution (which should NOT have taken that long). However, my
variables never changed value, so it looks like WinDBG wasn't
displaying things correctly.
Just because I'm curious: what are those annoying 'l_' prefixes for?
For me, they break the flow as I'm reading -- plus, the 'l' looks a lot like '1'.
I'd prefer this code (plus the switch to 'for' suggested by I.V.):

std::set<inttheset;

int i = 0;
while(i < 10) {
theset.insert(i++);
}
It's a simplified Hungarian Notation we've adopted. Particularly in
large C++ methods, it helps to be able to see at a glance which are
local variables (l_XXX) versus class members (m_XXX) versus globals
(g_XXX). Once you get used to the look (it took me a while myself), it
DOES help. I hope this doesn't spark a war on HN ;-)

Allan.

Jan 19 '07 #5
On 19 Jan 2007 13:10:42 -0800, Allan Stirrett <as******@tandbergtv.comwrote:
....
>Just because I'm curious: what are those annoying 'l_' prefixes for?
For me, they break the flow as I'm reading -- plus, the 'l' looks a lot like '1'.
....
It's a simplified Hungarian Notation we've adopted. Particularly in
large C++ methods, it helps to be able to see at a glance which are
local variables (l_XXX) versus class members (m_XXX) versus globals
(g_XXX). Once you get used to the look (it took me a while myself), it
DOES help.
Well, I need something on my class members myself -- m_foo or foo_. It would
take a lot to convince me that locals need l_foo -- or that I need globals
at all.
I hope this doesn't spark a war on HN ;-)
Agreed. We've all been through it a hundred times, after all.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jan 20 '07 #6

Jorgen Grahn wrote:
On 19 Jan 2007 13:10:42 -0800, Allan Stirrett <as******@tandbergtv.comwrote:
...
Just because I'm curious: what are those annoying 'l_' prefixes for?
For me, they break the flow as I'm reading -- plus, the 'l' looks a lot like '1'.
...
It's a simplified Hungarian Notation we've adopted. Particularly in
large C++ methods, it helps to be able to see at a glance which are
local variables (l_XXX) versus class members (m_XXX) versus globals
(g_XXX). Once you get used to the look (it took me a while myself), it
DOES help.

Well, I need something on my class members myself -- m_foo or foo_. It would
take a lot to convince me that locals need l_foo -- or that I need globals
at all.
Just my two cents: if you're going to prefix some, might as well
prefix them all for consistency. At a glance, is "sequenceNum" a local
variable, or a member variable someone forgot to prefix with "m_"? If
members, locals, and globals (which I avoid at all costs BTW) are ALL
consistently prefixed, then there's no question about scope.

I doubt that is enough to convince you, but just my thoughts!

Jan 21 '07 #7

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

Similar topics

8
by: JDT | last post by:
Hi, The last statement at the end of this post is to delete a set member 7.7. I set a break point inside ltstr::operator(). I found out that STL binary-seach the set twice. How come? I have...
4
by: Rares Vernica | last post by:
Hi, How can I save a reference inside a container? For example I have: map<string, unsignedX; I would like to be able to save a reference to a position inside X. For a vector, the...
4
by: Stephan Rose | last post by:
Hi everyone, I have a little problem I'm not sure what to do about or if anything can even be done about it. Situation is as follows: I had previous implemented a class called Scalar that...
4
by: pavanponnapalli | last post by:
hi, I need to send some values to a subroutine where i use insert query. suppose say my scalar is as follows: my ($name,$number,$address) etc and i get the values into those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.