Hi All,

Please expain me, in below giving statements which one is efficient.
int i=0;
i = i + 1;
or
i++;

2. In We Allocate the memory using malloc. between somewhere after allocating memory if our programming crashed.Then Allocated memory will be immedately freed by OS or OS will free this memory after some time period.

Thanks in Advance.

> 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.

answer for you question #1:

using increment operation is far more effective than that of using assignment and addition operation..

> 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

>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.

This article has been dead for over six months. Start a new discussion instead.