如何确定指针是否等于数组的元素? [英] How to determine if a pointer equals an element of an array?

查看:93
本文介绍了如何确定指针是否等于数组的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Code Reveiw中的代码 有效" ,但是可能具有 UB

I have code in Code Reveiw that "works" as expected, yet may have UB .

代码具有大小相同的char数组,称为GP2_format[].为了检测指针format是否具有与元素GP2_format[][0]之一的地址相同的值,下面的代码简单地测试了指针>=是最小的元素而<=是最大的元素.由于元素的大小为1,因此无需进一步检查.

Code has an array of same-sized char arrays called GP2_format[]. To detect if the pointer format has the same value as the address of one of the elements GP2_format[][0], the below code simple tested if the pointer was >= the smallest element and <= the greatest. As the elements are size 1, no further checking needed.

const char GP2_format[GP2_format_N + 1][1];
const char *format = ...;

if (format >= GP2_format[0] && format <= GP2_format[GP2_format_N]) Inside()
else Outside();


C11§6.5.8/5当从数组外部比较指针时,关系运算符< > <= >=似乎将其定义为可怕的 Undefined Behavior .


C11 §6.5.8/5 Relational operators < > <= >= appears to define this as the dreaded Undefined Behavior when comparing a pointer from outside the array.

比较两个指针时,结果取决于所指向对象在地址空间中的相对位置.如果两个指向对象类型的指针都指向 相同的对象,...相同的数组对象,它们比较相等. ...(相同的对象正常)....(相同的联合确定)....(相同的数组正常)...在所有其他情况下,行为未定义.

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, ... of the same array object, they compare equal. ...(same object OK) .... (same union OK) .... (same array OK) ... In all other cases, the behavior is undefined.


Q1是否在GP2_get_type() UB中比较代码的指针?
Q2如果是这样,什么是定义明确的替代方法,将O(1)搜索到有问题的GP2_get_type()?

Q1 Is code's pointer compare in GP2_get_type() UB?
Q2 If so, what is a well defined alternate, search O(1), to the questionable GP2_get_type()?

较慢的解决方案
代码可以针对每个GP2_format[]依次测试format或将值转换为intptr_t,进行一次排序,然后执行O(ln2(n))搜索.

Slower solutions
Code could sequentially test format against each GP2_format[] or convert the values to intptr_t, sort one time and do a O(ln2(n)) search.

类似
...如果指针是集合的一部分,但是此集合"不是随机的,则是一个数组.
intptr_t方法-也许是UB. >

Similar
...if a pointer is part of a set, but this "set" is not random, it is an array.
intptr_t approach - maybe UB.

#include <stdio.h>

typedef enum {
  GP2_set_precision,
  GP2_set_w,
  GP2_setios_flags_,
  GP2_string_,
  GP2_unknown_,
  GP2_format_N
} GP2_type;

const char GP2_format[GP2_format_N + 1][1];

static int GP2_get_type(const char *format) {
  // candidate UB with pointer compare
  if (format >= GP2_format[0] && format <= GP2_format[GP2_format_N]) {
    return (int) (format - GP2_format[0]);
  }
  return GP2_format_N;
}

int main(void) {
  printf("%d\n", GP2_get_type(GP2_format[1]));
  printf("%d\n", GP2_get_type("Hello World"));  // potential UB
  return 0;
}

输出(如预期,但可能是UB)

Output (as expected, yet potentially UB)

1
5

推荐答案

如果要遵守C标准,则可以选择:

If you want to comply with the C Standard then your options are:

  • 针对目标范围内的每个指针执行单独的==!=测试
    • 如果集合很大,则可以使用哈希表或搜索树或其他方法来加快速度
    • Perform individual == or != tests against each pointer in the target range
      • You could use a hash table or search tree or something to speed this up, if it is a very large set

      可能可行"的方法是将所有值都转换为uintptr_t,然后进行关系比较.如果系统具有带有绝对顺序的内存模型,则应定义uintptr_t并保留该顺序;如果没有这样的模型,那么关系比较的想法就永远不会奏效.

      A "probably works" method would be to cast all of the values to uintptr_t and then do relational comparison. If the system has a memory model with absolute ordering then it should define uintptr_t and preserve that ordering; and if it doesn't have such a model then the relational compare idea never would have worked anyway.

      这篇关于如何确定指针是否等于数组的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆