[英]True memory locations of C variables
為了更多地了解C,過去兩天我一直在玩它。 我想開始研究C在運行時是如何構造的,所以,我構建了一個糟糕的程序,要求用戶輸入兩個整數值,然后輸出整數變量的內存位置。 然后我想驗證數據是否真的存在,我將程序暫停了getchar()以打開GDB並挖掘內存段以驗證數據但是,這些位置的數據沒有多大意義我。 有人可以解釋這里發生了什么。
#include <stdio.h>
void pause();
int main() {
int a, b;
printf("Please enter number one:");
scanf("%d", &a);
printf("Please enter number two:");
scanf("%d", &b);
printf("number one is %d, number two is %d\n", a, b);
// find the memory location of vairables:
printf("Address of 'a' %pn\n", &a);
printf("Address of 'b' %pn\n", &b);
pause();
}
void pause() {
printf("Please hit enter to continue...\n");
getchar();
getchar();
}
[josh@TestBox c_code]$ ./memory
Please enter number one:265
Please enter number two:875
number one is 265, number two is 875
Address of 'a' 0x7fff9851314cn
Address of 'b' 0x7fff98513148n
Please hit enter to continue...
(gdb) dump memory ~/dump2.hex 0x7fff98513148 0x7fff98513150
[josh@TestBox ~]$ xxd dump2.hex
0000000: 6b03 0000 0901 0000 k.......
6b030000
和09010000
是little-endian(最低有效字節優先)。 要以更自然的方式讀取它們,請顛倒字節的順序:
6b030000
=> 0000036b
=> 875
十進制
09010000
=> 00000109
=>十進制的265
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.