> in below giving statements which one is efficient.
They're the same.
Most modern compilers will generate the same code for both constructs.
Even if the generated code is different (for some rare compiler), it's only going to be one instruction more. Telling which form will generate the extra instruction however is a completely different matter.
In short, forget about it and go with whatever you feel expresses your intent with the greatest clarity.
> Then Allocated memory will be immedately freed by OS
> or OS will free this memory after some time period.
Well that really depends on your OS.
All the modern 32-bit desktop operating systems and those which employ virtual memory techniques will have no trouble at all cleaning up such errant programs.
Crusty old DOS might have some trouble though, depends on how badly memory was trashed.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> using increment operation is far more effective than that of using assignment and addition operation
Where's your evidence ?
Your use of 'far' suggests that the performance difference is vast, and that simply isn't true. It's not like one is always inline code and the other is a function call for example. Both are extremely primitive operations which are transparent to any half-decent compiler.
Compiled with GCC and VC6, with and without optimisation. In all cases, the like-for-like code produces exactly the same results.
int foo ( void ) {
int i = 0;
i = i + 1;
return i;
}
int bar ( void ) {
int i = 0;
i++;
return i;
}
$ gcc -S foo.c
$ more foo.s
_foo:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl $0, -4(%ebp)
leal -4(%ebp), %eax
incl (%eax)
movl -4(%ebp), %eax
leave
ret
_bar:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl $0, -4(%ebp)
leal -4(%ebp), %eax
incl (%eax)
movl -4(%ebp), %eax
leave
ret
$ gcc -S -O2 foo.c
$ more foo.s
_foo:
pushl %ebp
movl $1, %eax
movl %esp, %ebp
popl %ebp
ret
_bar:
pushl %ebp
movl $1, %eax
movl %esp, %ebp
popl %ebp
ret
$ cl /Fafoo.s foo.c
$ more foo.s
_foo PROC NEAR
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _i$[ebp], 0
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
mov eax, DWORD PTR _i$[ebp]
mov esp, ebp
pop ebp
ret 0
_bar PROC NEAR
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _i$[ebp], 0
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
mov eax, DWORD PTR _i$[ebp]
mov esp, ebp
pop ebp
ret 0
$ cl /Fafoo.s /O2 foo.c
$ more foo.s
_foo PROC NEAR
mov eax, 1
ret 0
_bar PROC NEAR
mov eax, 1
ret 0
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>using increment operation is far more effective than
>that of using assignment and addition operation..
You're probably confusing yourself with the general guideline of using prefix increment when it's an overloaded operator in C++. In that case, the prefix increment may be more efficient due to the creation of fewer temporary objects. In C and for built-in types in C++, if your compiler isn't smart enough to convert both into virtually identical object code, I would trash it. We're talking about a trivial optimization that even the first C compiler performed.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401