这是示例代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct substruct {
int integer1;
unsigned char boolean1;
unsigned char boolean2;
char *ptr1;
char *ptr2;
} substruct_t;
typedef struct tester {
char *ptr1; //0x00
char *ptr2; //0x08
unsigned char boolean1; //0x10
unsigned char boolean2; //0x11
int integer1; //0x12
// padding bypte 0x16 -> 0x18
char *ptr3; //0x18
// It is not pointer.
substruct_t local; // 0x20
char *ptr4;
} tester_t;
int main()
{
tester_t *tester;
tester = NULL;
printf("0\n");
substruct_t *localptr = &(tester->local);
printf("1\n");
// seg fault
substruct_t test_local = tester->local;
printf("2\n");
substruct_t *test_local_ptr = &test_local;
printf("3\n");
// localptr = &(0x20); ???
// void *voidptr = 0x20;
// substruct_t *testptr = (substruct_t *)&(voidptr);
// printf("4\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么程序不会&(tester->local)在线死掉?我想知道为什么test->local会导致崩溃,但&(test->local)没有。
这是输出结果。
$ gcc -Wall -g -o asdf asdf.c
$ ./asdf
0
1
[1] 1777 segmentation fault ./asdf
Run Code Online (Sandbox Code Playgroud)
0、1 打印但 2 不打印。线路段故障tester->local。