Where can I find an implementation for operator ++?

Where can I find an implementation of the C / C ++ operator ++for numbers and pointers?

I looked around the web but did not find much ...

0
source share
4 answers

Maybe my answer (for a number) is useful to some extent:
you can control the response for a pointer

How it i++works at a low level.

int main(){
    int i=0,j=0;
    j=i++;  // expresion includes two operations `+, =      
    printf("%d %d",j,i);
}

You can parse it using a flag -Swith gcc (g++):
My codenamem.c

$ gcc -S m.c

It will create an assembly file m.s.

Read comments I added:

pushl   %ebp
movl    %esp, %ebp
andl    $-16, %esp
subl    $32, %esp

movl    $1, 28(%esp)            // i due to declarations 
movl    $0, 24(%esp)            // j

movl    28(%esp), %eax          // this two lines are j = i
movl    %eax, 24(%esp)

addl    $1, 28(%esp)            // increment to i, i++

movl    $.LC0, %eax
movl    28(%esp), %edx
movl    %edx, 8(%esp)
movl    24(%esp), %edx
movl    %edx, 4(%esp)
movl    %eax, (%esp)
call    printf

This is how it is executed =, and then ++. If ++- postfix.

How the compiler works:

(according to my compiler)

j = i ++.

:

j = i++;  

( ):
  j =    ++

i++ i = i + 1

// abstract syntax tree

       + (post)
      / \
     /   \  
    =     1 
   / \       
  /   \
 j     i  

CASE OF PREFIX ++ (++ i):

, j = ++i, :

++, =;

// abstract syntax tree for j = ++i  

    =
   / \       
  /   \
 j     \ 
       + (prefix)
      / \
     /   \  
    i     1 

j = ++i j=0 i=1 :

movl    $1, 28(%esp)          // i declaration 
movl    $0, 24(%esp)          // j  
addl    $1, 28(%esp)          // First  Add 1 to i because i++ (before = perform)
movl    28(%esp), %eax        // Below two steps: = performed  j = i 
movl    %eax, 24(%esp)
+3

.

, :

  • i.
  • i - , , ( ) +1.
  • i , , ( ), , sizeof(*i) +1.
  • i - , operator++() , , .

. i++ add inc ++.

+6

, , . :

//sg
int main()
{
    int i=0;
    ++i;
    return 0;
}

: gcc -S -fverbose-asm -masm=intel test.c

( ):

mov DWORD PTR [ebp-4], 0    # i,
add DWORD PTR [ebp-4], 1    # i,

//sg
int main()
{
    int i=0,j;
    j=i+1;
    return 0;
}

asm:

mov DWORD PTR [ebp-4], 0    # i,
mov eax, DWORD PTR [ebp-4]  # tmp62, i
add eax, 1  # tmp61,
mov DWORD PTR [ebp-8], eax  # j, tmp61

, i=i+1 i++.

+3

, , .

++ 1, . , . , ++x, postfix, x++.

, . ++x x x ( ++, , x, ). x++ x x.

, " 1"

for a type pointer T*pointing to the i-th element of array T, after increasing this pointer, it points to i + 1-th element of the array.

this means that the machine code level address is incremented by sizeof(T).

expression p+n, where nis an integer, gives the same final value as nif the final value was determined correctly. it can also be written as n+p.

+1
source

All Articles