gpt4 book ai didi

c++ - 如果我知道只使用现有元素,我可以在数组开头之前传递一个指针吗?

转载 作者:行者123 更新时间:2023-12-03 18:00:53 25 4
gpt4 key购买 nike

我有一个代码,我在其中执行如下操作:

double computeSometing(const double * parameters)
{
return useValues(parameters - 10);
// in this special case, useValues only uses values that are
// at least at parameters[0] or after
}

这看起来很糟糕,但只有在我知道 useValues 的情况下才会调用此代码不会在第 10 个之前使用任何值(情况并非总是如此),因此所有使用的值都在“内部” parameters .那是未定义的行为吗?我知道它只适用于我使用过的所有编译器/平台,但这并没有定义它。

我这样做是为了避免复制 parameters 的内容到具有 10 个以上元素的新数组,因为此函数对性能很敏感。

最佳答案

减法具有未定义的行为。

[表达式添加]:

If the expression P points to element x[i] of an array object x with n elements, [...] the expression P - J points to the (possibly-hypothetical) element x[i − j] if 0 ≤ i − j ≤ n; otherwise, the behavior is undefined.



请注意,生成值的行为本身是未定义的 - 您甚至不需要使用结果。

关于c++ - 如果我知道只使用现有元素,我可以在数组开头之前传递一个指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58990117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com