[NXC][Bug] Decrementing char in string

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

BTW:
this:

Code: Select all

while(r=p>2&r<0?-r:-o[++j]) {...}
is definetly different from this:

Code: Select all

while(r=p>2&r<0?-r:-o[j++]) {...}
(edit: tried in my chess program)
Last edited by HaWe on 01 Jun 2011, 18:21, edited 1 time in total.
bullestock
Posts: 27
Joined: 29 Sep 2010, 19:34
Location: Denmark
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by bullestock »

doc-helmut wrote:I'm not sure if the both following do the same (in C, not in C++):

Code: Select all

for(int i = 0; i < 42; i++) {printf("%d\n", i}
// vs.
for(int i = 0; i < 42; ++i) {printf("%d\n", i}
(not at home, can't test it)
They do, since the value of the increment operation is not used (only the side effect).
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by linusa »

bullestock wrote:
doc-helmut wrote:I'm not sure if the both following do the same (in C, not in C++):

Code: Select all

for(int i = 0; i < 42; i++) {printf("%d\n", i}
// vs.
for(int i = 0; i < 42; ++i) {printf("%d\n", i}
(not at home, can't test it)
They do, since the value of the increment operation is not used (only the side effect).
And the (++i) version can be faster in this case than the (i++) version. At least in C++ when i is an object :-) (but I can't be bothered to lookup the quote in Stroustrup / Meyers now).
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

the prefix ++i version usually uses temporary variables to store the old index in case the old value (i) is needed while i already has been incremented to (i+1).
On the other hand, we are talking about C and NXC, not C++
I doubt that ANSI C prefix ++i is faster than postfix i++ which is simply the fastest i=i+1 without any temp variable not even using the RAM for calculation (in C it's a direct register operation).

So please CMIIW, but honestly, I'm not sure if your post helps a lot along, Linus ;)
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by linusa »

doc-helmut wrote:the prefix ++i version usually uses temporary variables to store the old index in case the old value (i) is needed while i already has been incremented to (i+1).
You got that the wrong way round....

Think of the ++i and i++ operators as functions like this

Code: Select all

int increment_before(int& i) // ++i
{
i = i+1;
return i;
}
// and
int increment_after(int& i) // i++
{
int temp= i; // remember old value
i = i+1;
return temp;
}

If you use i++/++i inside loops, i coud be an Iterator in C++:

Code: Select all

std::vector<int> integerVector;
// Fill vec with values
std::vector<int>::iterator i = integerVector. begin();
for ( ; i != integerVector. end(); ++i)
{
// Do something with current value
cout << (*i) << endl;
}
The choice for "++i" had a reason in this case. If you don't need the return value of an expression like this (such as in for-loops), use the ++i version.

While this doesn't matter with integers, it might matter with iterators or objects (if you overloaded ++). Looking at example-functions:

Code: Select all

MyClass increment_before(MyClass& k) // ++k
{
// increment k here
return k;
}
// and
MyClass increment_after(MyClass& k) // k++
{
MyClass temp = MyClass(k); // remember old value
// increase k here
return temp;
}
As we're using objects here, in the 2nd case an object clone has to be created.

That is why there is a simple beauty of this rule:
If you just want to increment something (and you don't need the expression), always use ++i. It's always at least as fast as i++, but in C++ for special cases with iterators or objects, ++i is faster.

Always prefer ++i for increments in C and C++, and you never go wrong. Simple :-)
On the other hand, we are talking about C and NXC, not C++
Yes, but if there is a rule which doesn't matter in the worst case but helps in the best case and applies for C and C++, you might as well remember it.
I doubt that ANSI C prefix ++i is faster than postfix i++
The standard only specifies how the language has to work. Whether the compiler optimizes away the temp-variable or not is a choice of implementation. So it indeed depends on the compiler and optimization-level!
which is simply the fastest i=i+1 without any temp variable not even using the RAM for calculation (in C it's a direct register operation).
Wrong, as we've seen above. The temp-variable is indeed needed with i++ and not with ++i. Whether the compiler makes the actual increment-step a register operation, and if it optimizes away the temp-variable when it's not needed, is compiler-specific.
So please CMIIW, but honestly, I'm not sure if your post helps a lot along, Linus ;)
You may decide that for yourself, but 1) now you know your understanding of ++i vs i++ was wrong, and 2) now everybody can safely trust the "in general, ++i is equal or faster than i++ for incrementations only, always" rule.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

of course for C++ I already found your examples which you have been quoting ( although you didn't post your reference http://board.raidrush.ws/showthread.php?t=498122).
C++ uses copies of objects.
But I'm still not sure about C - I recalled it other way round from any a C tutorial. I'll try to find my reference, but it's long ago and I admit I may be wrong.

edit:
I found something, you were right:
i+=1 is the "quick" increment and it's the same as ++i, not i++
Last edited by HaWe on 01 Jun 2011, 15:43, edited 1 time in total.
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Decrementing char in string

Post by linusa »

My original source is "B. Stroustrup - The C++ programming language" or "Scott Meyers - Effective C++" or "Scott Meyers - More effective C++" (great books, I recommend them), as well as common sense. Many examples can be derived from that, and the net is full of tutorials, code snippets, blog posts, and insights (both wrong and/or right). The link you quoted wasn't my original source btw...
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Decrementing char in string

Post by HaWe »

ok, I thought because your examples reminded me exactly of the ones from the link I recalled:http://board.raidrush.ws/showthread.php?t=498122

Code: Select all

std::vector<int> integerVector;
// Vektor mit Werten füllen
std::vector<int>::iterator i = integerVector.begin();
for ( ; i != integerVector.end(); ++i)
{
   // Mache irgendwas mit dem aktuellen Element, z.B.
   cout << (*i) << endl;
}

Code: Select all

MyClass increment_before(MyClass& k) // ++k
{
   //Erhöhen von k
   return k;
}
// und
MyClass increment_after(MyClass& k) // k++
{
   MyClass temp = MyClass(k); // Den alten Wert sichern
   //Erhöhen von k
   return temp;
}
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: [NXC][Bug] Decrementing char in string

Post by ricardocrl »

Because I was not aware at all about this prefix "++i", I've been just reading your posts.
Thanks a lot, even though it's a detail in most of the simple programs we develop with Mindstorms, I believe this is a good forum to give this kind of tips, as a lot of people works with programming for professional uses or will end working with it in a near future. I'm proud of it. :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests