-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path01-plain.cpp
More file actions
78 lines (65 loc) · 1.8 KB
/
01-plain.cpp
File metadata and controls
78 lines (65 loc) · 1.8 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Find out the memory layout of a class
A class with three private variables and two functions.
Compile:
(GCC)
$ g++ plain.cpp -std=c++11 -o plain
(LLVM)
$ clang++ plain.cpp -o plain
(MSVC)
$ cl plain.cpp
Run:
$ plain
*/
#include "util.hpp"
/*
Observe the address of each of them.
Question:
Variables
- Are the address of variables close to each other? See also their size.
- Do the addresses come in certain order? In what order?
- Are the variables close to the start of class?
- In what segment do the variables reside?
Functions
- Are the address of functions close to each other?
- Do the address come in certain order? In what order?
- Are the functions close to the start of class?
- Are the functions close to the variables?
- In what segment do the functions reside?
*/
//======== Type Definitions =========================================
class Example
{
int x;
int y;
int z;
public:
Example()
{
x = 135;
y = 182;
}
void printVars()
{
printf("Location x: [%p] | Value: %d\n", &x, x);
printf("Location y: [%p] | Value: %d\n", &y, y);
printf("Location z: [%p] | Value: %d\n", &z, z);
}
void printFuncs()
{
printf("Location of printVars : [%p]\n", &Example::printVars);
printf("Location of printFuncs : [%p]\n", &Example::printFuncs);
}
};
//======== Helper Functions =========================================
//======== Main Function ============================================
int main()
{
Example kelas;
dump_instance("Plain Object without any decoration", kelas);
kelas.printVars();
printf("\n");
kelas.printFuncs();
return 0;
}
//======== Implementations ==========================================