今天逆向並找出指標的特徵。指標意即記憶體位置,系統在傳資料的時候大多使用指標進行資料傳遞。
disassemble main
跟上圖大同小異。
解答公布:
#include<stdio.h>
int main(){
int box = 10;
int *boxPtr;
boxPtr = &box;
printf("Address of box is %p\n", &box);
printf("Address of boxPtr is %p\n", &boxPtr);
printf("Address stored in boxPtr is %p\n", boxPtr);
printf("Value of box is %d\n", box);
printf("Value of *boxPtr is %d\n", *boxPtr);
return 0;
}
disassemble main
很長一段(如下圖):
簡單來說,就是:cPtr -> bPtr -> aPtr -> a
解答公布:
#include<stdio.h>
int main(){
int a = 16;
int *aPtr;
aPtr = &a;
printf("Address of a is %p\n", &a);
printf("Address of aPtr is %p\n", &aPtr);
printf("Address stored in aPtr is %p\n", aPtr);
printf("Value of a is %d\n", a);
printf("Value of *aPtr is %d\n", *aPtr);
printf("\n");
int **bPtr;
bPtr = &aPtr;
printf("Address of bPtr is %p\n", &bPtr);
printf("Address stored in bPtr is %p\n", bPtr);
printf("Value of **&aPtr is %d\n", **&aPtr);
printf("Value of **bPtr is %d\n", **bPtr);
printf("\n");
int ***cPtr;
cPtr = &bPtr;
printf("Address of cPtr is %p\n", &cPtr);
printf("Address stored in cPtr is %p\n", cPtr);
printf("Value of ***&bPtr is %d\n", ***&bPtr);
printf("Value of ***cPtr is %d\n", ***cPtr);
printf("\n");
printf("Address of cPtr is %p\n", &cPtr);
printf("Address stored in cPtr is %p\n", cPtr);
printf("Address of bPtr is %p\n", &bPtr);
printf("Address stored in bPtr is %p\n", bPtr);
printf("Address of aPtr is %p\n", &aPtr);
printf("Address stored in aPtr is %p\n", aPtr);
printf("Address of a is %p\n", &a);
printf("Value stored in a is %d\n", a);
return 0;
}
disassemble main
下圖的 call 0x10b0 就是呼叫 malloc()
解答公布:
#include<stdio.h>
int main(){
int *aPtr = (int *)malloc(sizeof(int));
*aPtr = 16;
printf("The Address is :%p\n", &aPtr);
printf("Address stored in aPtr is %p\n", aPtr);
printf("The value is : %d\n", *aPtr);
free(aPtr);
return 0;
}
指標就是記憶體的位址。明天接著介紹應用!