我试图显示特定内存位置的内容,并且我遇到了一个令人费解的错误。以下是我使用的功能:
int mem_display(Cmd *cp, char *arguments)
{
int x = 10;
char c = 'c';
int input = 0;
sscanf(arguments, " %x", &input)
int *location = (int*)input;
printf("location of int x: %p\n", (void*)&x);
printf("location of char c: %p\n", (void*)&c);
printf("%x\n", *location);
return 0;
}
当我输入正确的命令(md)后跟x(ffbef54c)的位置时,它会正确显示。但是,当我再试一次或少一次时,我收到一个错误:
UNIX-tutor> md ffbef54c
location of int x: ffbef54c
location of char c: ffbef54b
ffbef54c
a
UNIX-tutor> md ffbef54b
location of int x: ffbef54c
location of char c: ffbef54b
ffbef54b
Bus error (core dumped)
尝试将char显示为int时是否存在问题?我需要能够显示我输入的位置中存储的内容的十六进制值。任何帮助表示赞赏!
答案 0 :(得分:1)
您的系统需要正确对齐取消引用指针。确保输入正确对齐的值(在您的情况下可能为sizeof(int)
)。或者,只需使用char
指针。您应该能够以这种方式访问任何地址 - 只需确保您的程序已映射并允许其访问!