C programming Pointers
C programming Pointers
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1
0x108
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1
0x108
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200
} px:
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200
} px: 0x108
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200
} px: 0x108
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py:
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1
{
0x308
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1
{
0x308
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 1 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 2
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 1
0x108 0x104
void swap(int *px, int *py) x: 1 y: 2
{
0x308 0x304
int x = *px;
int y = *py;
*px = y;
*py = x; 0x200 0x204
} px: 0x108 py: 0x104
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 1
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
*px = y;
*py = x;
}
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 1
0x108 0x104
void swap(int *px, int *py)
{
int x = *px;
int y = *py;
int main()
{
int x = 1;
int y = 2;
swap(&x, &y);
}
x: 2 y: 1
0x108 0x104
Assembly
main() function
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address %esp
subl $16, %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address %esp
subl $16, %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
Saved %ebp %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
Saved %ebp %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp %esp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp)
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) 1
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax 2
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax %esp
call swap
addl $8, %esp
leave
ret
-4(%ebp) = -8 +%eax
R[%ebp] = 0x108
0x104
&y
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y %esp
addl $8, %esp
leave
ret
%eax
0x104
&y
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y %esp
addl $8, %esp
leave
ret
%eax
0x108
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y %esp
addl $8, %esp
leave
ret
%eax
0x108
&x
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y
addl $8, %esp 0x108 %esp
leave
ret
%eax
0x108
&x
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y
addl $8, %esp px: 0x108
&x %esp
leave
ret
%eax
0x108
&x
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y
addl $8, %esp px: 0x108
&x %esp
leave
ret
%eax
0x108
&x
main:
pushl %ebp caller’s frame
movl %esp, %ebp return address
subl $16, %esp
%ebp Saved %ebp
movl $1, -4(%ebp)
movl $2, -8(%ebp) x: 1 0x108
leal -8(%ebp), %eax y: 2 0x104
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap py: 0x104
&y
addl $8, %esp px: 0x108
&x
leave
return address %esp
ret
%eax
0x108
&x
swap() function
swap:
pushl %ebp x: 1 0x108
movl %esp, %ebp y: 2 0x104
subl $16, %esp
movl 8(%ebp), %eax
py: 0x104