为什么带有 %p 指针格式说明符的 printf 在 AVR 上打印垃圾字符而不是地址?
Why would printf with %p pointer format specifier print garbage chars on AVR instead of an address?
我看过 How to find the address of a variable when using AVR? - 那里的结论基本上是:
printf()-debugging on embedded targets is a bit complicated, you need to dig through and figure out if there's code to connect printf() with a serial port, and so on. Also, a full printf() can be a bit heavy, so perhaps you're already using a trimmed-down standard library. It's not the compiler's responsibility, you need to figure out how it's implemented.
...但是我真的想不通为什么会出现我的这个问题。
我有一个变量,我想将其用作 "array of strings",并且我想对其进行 malloc 和 realloc:
char** my_array = malloc(sizeof(char*)*1);
然后,我将 printf
重定向到 "print" 到 USB 串行端口,然后我在串行终端中读取打印输出。
据我所知,printf 的%p
格式说明符应该将变量的地址打印为十六进制数。我有这样的声明:
printf("my_array %p\r\n", (void *)&my_array);
我也试过:
printf("my_array %p\r\n", &my_array);
printf("my_array %p\r\n", my_array);
在所有情况下,我得到的打印输出都是这样的:
my_array ▒▒▒ꓣ▒▒▒▒/▒▒f'w'▒`$▒▒W*▒▒X▒f'w'▒`$▒▒Y*▒▒Z▒f'w'▒`▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒#▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒j▒{▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒i▒z▒▒`$▒▒
...这显然不是十六进制数。
为什么会这样,我做错了什么?我怎样才能得到一个在 AVR 中打印为十六进制数的变量地址?如果重要的话,我使用 CodeVisionAVR 编译器...
编辑:发现这个:
https://www.avrfreaks.net/forum/format-specifier-pointers-0
But do people actually use %p and especially on an AVR. On an AVR a pointer is a 16bit unsigned number. I'd generally just use %04X or something myself.
(OK just realised this forum might mean 32bit ARM or UC3 where I guess the pointers are 32 bit? Even so %08X in that case)
...所以我只是改用:
printf("my_array %08X\r\n", (void *)&my_array);
...现在我得到打印输出:
my_array 00003FFB
...但现在我不知道这是否是真实地址 :)
您可以将指针转换为 uintptr_t
并打印:
printf("my_array %llu\r\n", (unsigned long long)(uintptr_t)&my_array);
我看过 How to find the address of a variable when using AVR? - 那里的结论基本上是:
printf()-debugging on embedded targets is a bit complicated, you need to dig through and figure out if there's code to connect printf() with a serial port, and so on. Also, a full printf() can be a bit heavy, so perhaps you're already using a trimmed-down standard library. It's not the compiler's responsibility, you need to figure out how it's implemented.
...但是我真的想不通为什么会出现我的这个问题。
我有一个变量,我想将其用作 "array of strings",并且我想对其进行 malloc 和 realloc:
char** my_array = malloc(sizeof(char*)*1);
然后,我将 printf
重定向到 "print" 到 USB 串行端口,然后我在串行终端中读取打印输出。
据我所知,printf 的%p
格式说明符应该将变量的地址打印为十六进制数。我有这样的声明:
printf("my_array %p\r\n", (void *)&my_array);
我也试过:
printf("my_array %p\r\n", &my_array);
printf("my_array %p\r\n", my_array);
在所有情况下,我得到的打印输出都是这样的:
my_array ▒▒▒ꓣ▒▒▒▒/▒▒f'w'▒`$▒▒W*▒▒X▒f'w'▒`$▒▒Y*▒▒Z▒f'w'▒`▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒#▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒j▒{▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒i▒z▒▒`$▒▒
...这显然不是十六进制数。
为什么会这样,我做错了什么?我怎样才能得到一个在 AVR 中打印为十六进制数的变量地址?如果重要的话,我使用 CodeVisionAVR 编译器...
编辑:发现这个:
https://www.avrfreaks.net/forum/format-specifier-pointers-0
But do people actually use %p and especially on an AVR. On an AVR a pointer is a 16bit unsigned number. I'd generally just use %04X or something myself.
(OK just realised this forum might mean 32bit ARM or UC3 where I guess the pointers are 32 bit? Even so %08X in that case)
...所以我只是改用:
printf("my_array %08X\r\n", (void *)&my_array);
...现在我得到打印输出:
my_array 00003FFB
...但现在我不知道这是否是真实地址 :)
您可以将指针转换为 uintptr_t
并打印:
printf("my_array %llu\r\n", (unsigned long long)(uintptr_t)&my_array);