-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrick.c
36 lines (31 loc) · 956 Bytes
/
trick.c
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
/*
* Copyright (c) 2017-2018 SYZ
*
* gcc trick.c -o release/trick_c
*/
#include <linux/kernel.h>
#include <stdio.h>
#include <stdlib.h>
#define offsetof(type, member) (size_t) & (((type *)0)->member)
#define container_of(ptr, type, member) \
({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
typedef struct sample {
int id;
char name[20];
int age;
} sample;
int main(int argc, char *argv[]) {
// get struct instance pointer with it's member pointer
sample *p_sample_ins = (sample *)malloc(sizeof(sample));
p_sample_ins->age = 10;
sample *p_sample_tmp = container_of(&(p_sample_ins->age), sample, age);
printf("p_sample_ins address: %p\n", p_sample_ins);
printf("p_sample_tmp address: %p\n", p_sample_tmp);
free(p_sample_ins);
p_sample_ins = NULL;
p_sample_tmp = NULL;
return 0;
}