13 Replies - 2481 Views - Last Post: 11 April 2012 - 06:56 PM Rate Topic: -----

#1 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

? ++value vs. value++ ?

Posted 10 April 2012 - 04:30 PM

Whats the difference between:

++value;



and

value++;



i know that the ++ before the variable is supposed to increment the variables value "before an operation"(i think thats the right way to word it) and ++ after the variable means "after an operation".

i tend to use value++ instead of writting value += 1. So what exactly is the difference? Maybe i just havent ran into a situation where it mattered. I dont know.

So thanks in advance for your time and help, have a great day.

Is This A Good Question/Topic? 0
  • +

Replies To: ? ++value vs. value++ ?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 04:45 PM

Please run this code:

#include <iostream>
using namespace std;

int main()
{
	int i = 0;
	int j;

	j = ++i;

	cout << j << endl;

	{
		int i = 0;
		int j;

		j = i++;

		cout << j << endl;
	}

	return 0;
}



What do you notice between pre- and post-increment?
Was This Post Helpful? 1
  • +
  • -

#3 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:00 PM

ok so in your example it changes the value of i pre and post assightment. but to be more specific would it be true to say that the pre-increment(or decrement too) happens before the variable is used in the expression and the post-increment is after the variable is used in the expression?
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:06 PM

For the pre-increment the output is 1, and the post-increment the output is 0, telling you that the post-increment to i is not stored to j; the original value of zero is stored prior to incrementing.
Was This Post Helpful? 1
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:08 PM

#include <iostream>
using namespace std;

int main()
{
	{
		int i = 0;
		int j;

		j = ++i;

		cout << j << endl;
	}
	{
		int i = 0;
		int j;

		j = i++;

		cout << j << endl;
	}

	return 0;
}


sorry, shadowing irks me.

This post has been edited by ishkabible: 10 April 2012 - 05:09 PM

Was This Post Helpful? 1
  • +
  • -

#6 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:15 PM

@ishkabible
not to steer this in a different direction but "shadowing"?
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:21 PM

if a variable is declared twice in the same scope the second variables is said to "shadow" the first becuase it is used instead of the first. hence this is know as "shadowing". Its behavior is well defined so butch's program is 100% valid but it's a source of bugs and confusion and there really isn't any reason for it other than to eliminate a few easily avoidable library incompatibilities.
Was This Post Helpful? 1
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:27 PM

It's not declared twice in the same scope! Where did that come from?!

Secondly, the method is also used to keep code inline avoiding a second function call. Don't discredit a perfectly good method.

This post has been edited by ButchDean: 10 April 2012 - 05:29 PM

Was This Post Helpful? 1
  • +
  • -

#9 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:27 PM

ok that makes since but i thought that since he had the second set of i and j ints in a seperate {} group i didnt think it mattered even though i wouldnt have written it like that i still see the reason he did it like that i doubt he writes like that in his real code but for the sake of the example it compiled correctly and still taught the lesson that was needed.

So thanks to both of you i appreciate it.
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 05:30 PM

Yes, code like this does appear in the real world.
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: ? ++value vs. value++ ?

Posted 10 April 2012 - 08:13 PM

Quote

It's not declared twice in the same scope! Where did that come from?!


the second declarations of 'i' and 'j' in your original code, shadow the first. I was a being a bit misleading by saying 'same', I should have said when the same variable is declared once in a parent scope and again in a child scope, the variable declared in the child scope shadows the variable declared in the parent scope. I've always thought of the parent scope as a subset of its child scopes which is why I used that phrasing.

Quote

Secondly, the method is also used to keep code inline avoiding a second function call. Don't discredit a perfectly good method.


I don't understand how that works. could you elaborate?
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 11 April 2012 - 03:57 PM

View Postishkabible, on 11 April 2012 - 04:13 AM, said:

Quote

Secondly, the method is also used to keep code inline avoiding a second function call. Don't discredit a perfectly good method.


I don't understand how that works. could you elaborate?

The code I originally posted gives this assembler sequence:

Spoiler


Now, if I revise the code to include a function call...

#include <iostream>
using namespace std;

void TestFunc(int p)
{
	int j;

	j = p;

	cout << j << endl;
}

int main()
{
	int i = 0;

	TestFunc(++i);

	i = 0;

	TestFunc(i++);

	return 0;
}



The following disassembly is generated:

Spoiler


This is less efficient for several reasons:

1. call TestFunc (411253h) is of course slower than having inline code as before.
2. I'm passing by value, the value is being copied prior to the function call in the sequence:

004115A1  mov         eax,dword ptr [i] 
004115A4  mov         dword ptr [ebp-0D0h],eax 
004115AA  mov         ecx,dword ptr [i] 
004115AD  add         ecx,1 
004115B0  mov         dword ptr [i],ecx 
004115B3  mov         edx,dword ptr [ebp-0D0h] 
004115B9  push        edx  



That is my point. :)
Was This Post Helpful? 1
  • +
  • -

#13 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: ? ++value vs. value++ ?

Posted 11 April 2012 - 06:49 PM

What? how on earth does my example prevent a person from manually inlining their code?

In fact, I tested it; our two programs produce EXACTLY the same output on GCC

g++ -O3 -S test.c -o test.asm
produces the following for *both* programs
	.file	"test.c"
	.text
	.p2align 2,,3
	.def	___tcf_0;	.scl	3;	.type	32;	.endef
___tcf_0:
LFB990:
	.cfi_startproc
	subl	$28, %esp
	.cfi_def_cfa_offset 32
	movl	$__ZStL8__ioinit, (%esp)
	call	__ZNSt8ios_base4InitD1Ev
	addl	$28, %esp
	.cfi_def_cfa_offset 4
	ret
	.cfi_endproc
LFE990:
	.def	___main;	.scl	2;	.type	32;	.endef
	.section	.text.startup,"x"
	.p2align 2,,3
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB983:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	andl	$-16, %esp
	subl	$16, %esp
	call	___main
	movl	$1, 4(%esp)
	movl	$__ZSt4cout, (%esp)
	call	__ZNSolsEi
	movl	%eax, (%esp)
	call	__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
	movl	$0, 4(%esp)
	movl	$__ZSt4cout, (%esp)
	call	__ZNSolsEi
	movl	%eax, (%esp)
	call	__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
	xorl	%eax, %eax
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE983:
	.p2align 2,,3
	.def	__GLOBAL__sub_I_main;	.scl	3;	.type	32;	.endef
__GLOBAL__sub_I_main:
LFB991:
	.cfi_startproc
	subl	$28, %esp
	.cfi_def_cfa_offset 32
	movl	$__ZStL8__ioinit, (%esp)
	call	__ZNSt8ios_base4InitC1Ev
	movl	$___tcf_0, (%esp)
	call	_atexit
	addl	$28, %esp
	.cfi_def_cfa_offset 4
	ret
	.cfi_endproc
LFE991:
	.section	.ctors,"w"
	.align 4
	.long	__GLOBAL__sub_I_main
.lcomm __ZStL8__ioinit,1,1
	.def	__ZNSt8ios_base4InitD1Ev;	.scl	2;	.type	32;	.endef
	.def	__ZNSolsEi;	.scl	2;	.type	32;	.endef
	.def	__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_;	.scl	2;	.type	32;	.endef
	.def	__ZNSt8ios_base4InitC1Ev;	.scl	2;	.type	32;	.endef
	.def	_atexit;	.scl	2;	.type	32;	.endef



the only difference is that my addition to yours get's rid of shadowing.
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: ? ++value vs. value++ ?

Posted 11 April 2012 - 06:56 PM

View Postishkabible, on 12 April 2012 - 02:49 AM, said:

What? how on earth does my example prevent a person from manually inlining their code?

Actually, I didn't say that. Please reread.

View Postishkabible, on 12 April 2012 - 02:49 AM, said:

In fact, I tested it; our two programs produce EXACTLY the same output on GCC

g++ -O3 -S test.c -o test.asm
produces the following for *both* programs

... snip ...

the only difference is that my addition to yours get's rid of shadowing.


You're making a point against something I didn't say.

My point was that you don't like shadowing, choosing to modify code that works perfectly out personal preference saying that it was bad practice. I pointed out reasons why such methods are justifiable. That's all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1