-
Notifications
You must be signed in to change notification settings - Fork 0
/
220127_pointer_reference_2.cpp
53 lines (41 loc) · 1.63 KB
/
220127_pointer_reference_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Inspired by "Expert C Programming" Chapter 4
// Pointer vs. Reference
#include <cstdio>
int some_array[3];
int main()
{
// In chapter 4 of "Expert C Programming", the book explains why the following declaration is ill-formed:
//extern int *some_array;
// The compiler detects above statement as error with message: "conflicting declaration".
// Correct way is:
extern int some_array[3];
// I think I'm understanding more and more on references.
int a[3] = { 239, 241, 251 }; // a is a name of an array, the array starts from memory address "rbp - 20"
// [rbp - 20] = 239, [rbp - 16] = 241, [rbp - 12] = 251
int *p = a; // address of p is "rbp - 32", [rbp - 32] = rbp - 20
int (&r)[3] = a; // address of r is "rbp - 8" , [rbp - 8 ] = rbp - 20
std::printf("*a: %i\n", *a); // mov eax, [rbp - 20]
std::printf("*p: %i\n", *p); // mov eax, [[rbp - 32]]
std::printf("*r: %i\n", *r); // mov eax, [[rbp - 8 ]]
std::printf("\n");
std::printf(" a: %x\n", a); // lea eax, [rbp - 20]
std::printf(" p: %x\n", p); // mov eax, [rbp - 32]
std::printf(" r: %x\n", r); // mov eax, [rbp - 8 ]
std::printf("\n");
std::printf("&a: %x\n", &a); // lea eax, [rbp - 20] (address of a is also "rbp - 20"!!)
std::printf("&p: %x\n", &p); // lea eax, [rbp - 32]
std::printf("&r: %x\n", &r); // mov eax, [rbp - 8 ]
std::printf("\n");
/*
prints:
239
239
239
some value A
some value A
some value A
some value A
some value B (not A)
some value A
*/
}