打印函数指针的便携式方法?

发布于 2025-01-10 03:36:14 字数 563 浏览 5 评论 0原文

我想知道这是否是打印函数指针的便携式方法。
将函数指针直接转换为 unsigned char * 是不可移植的。

/* writehex() prints the given byte as its hex representation to the given FILE * */
extern void writehex(FILE *, const unsigned char);

/* Theoretical printf() implementation. */
int printf(const char *format, ...) {

    ...

    void (*fptr)(void);
    const unsigned char *bytes = (const unsigned char *) &fptr;
    size_t i;

    fptr = va_arg(ap, ((void)(*)(void)));
    for (i = 0; i < sizeof(fptr); ++i)
        writehex(stdout, *bytes++);

    ...
}

I am wondering if this is a portable way to print function pointers.
Casting the function pointer directly to unsigned char * is non-portable.

/* writehex() prints the given byte as its hex representation to the given FILE * */
extern void writehex(FILE *, const unsigned char);

/* Theoretical printf() implementation. */
int printf(const char *format, ...) {

    ...

    void (*fptr)(void);
    const unsigned char *bytes = (const unsigned char *) &fptr;
    size_t i;

    fptr = va_arg(ap, ((void)(*)(void)));
    for (i = 0; i < sizeof(fptr); ++i)
        writehex(stdout, *bytes++);

    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-01-17 03:36:14

将函数指针转换为 void * 或其他指向对象的指针可能会丢失信息,因为函数指针可能更宽。

没有很好的方法来可移植地打印函数指针 - 这是 C 的缺点。

替代方案:

转换为宽整数

尝试最宽的整数。

if (sizeof function_pointer <= sizeof(uintmax_t)) {
  printf("0x%jX\n", (uintmax_t) function_pointer);
}

内存转储(OP 的方法)

请注意,填充和字节序问题正在发挥作用。输出可能包括额外的垃圾和无例外的订单,但这将始终“有效”,因为它是可移植的且兼容的。

“将函数指针直接转换为 unsigned char * 是不可移植的。”此处不适用,因为可以将函数指针的地址转换为void *

else {
  unsigned char buf[sizeof(function_pointer)];
  memcpy(buf, &function_pointer, sizeof buf);
  printf("0x");
  for (unsigned i=0; i<sizeof buf; i++) {
    printf("%02X", buf[i]);
  }
  printf("\n");
}

  printf("0x");
  for (unsigned i = 0; i

  printf("0x%jX\n", (uintmax_t) function_pointer);
}
  

考虑到更广泛的体系结构,打印函数指针的文本输出具有非常不同的实现有不同的解释。

Casting a function pointer to void * or other pointer to objects may lose information as the function pointer may be wider.

There is no great way to portably print function pointers - a shortcoming of C.

Alternatives:

Convert to a wide integer

Try the widest.

if (sizeof function_pointer <= sizeof(uintmax_t)) {
  printf("0x%jX\n", (uintmax_t) function_pointer);
}

Memory dump (OP's approach)

Note that padding and endian issues are in play. Output may include additional junk and unexcepted order, yet this will always "work" in that it is portable and compliant.

"Casting the function pointer directly to unsigned char * is non-portable." does not apply here as it is OK to cast the address of a function pointer to a void *

else {
  unsigned char buf[sizeof(function_pointer)];
  memcpy(buf, &function_pointer, sizeof buf);
  printf("0x");
  for (unsigned i=0; i<sizeof buf; i++) {
    printf("%02X", buf[i]);
  }
  printf("\n");
}

  printf("0x");
  for (unsigned i = 0; i

  printf("0x%jX\n", (uintmax_t) function_pointer);
}
  

Consider the wider variety of architectures, the text output from printing function pointers has very different interpretations across implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文