问题标签 [pointer-aliasing]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
538 浏览

rust - 在不安全的代码中,可变引用的别名是否正确?

不安全的代码中,对同一个数组有多个可变引用(不是指针)是否正确,只要它们不用于写入相同的索引?

语境

我想产生一个底层数组的几个(不同的)可变视图,我可以从不同的线程进行修改。

如果不相交的部分是连续的,这很简单,只需调用split_at_mut切片即可:

但我也想暴露不连续的不相交部分。为简单起见,假设我们要检索偶数索引的可变“视图”,以及奇数索引的另一个可变“视图”。

与 相反split_at_mut(),我们无法检索两个可变引用(我们想要一个安全的抽象!),所以我们使用两个结构实例,只公开对偶数(或奇数)索引的可变访问:

使用一些不安全的代码,很容易得到这样一个安全的抽象。这是一个可能的实现

到现在为止还挺好。

问题

但是,访问原始指针非常方便:与切片相反,我们不能使用运算符[]或迭代器。

显而易见的想法是将原始指针本地转换为不安全实现中的切片:

然后我们可以,例如,以函数式风格重写我们的实现:

对于这个示例,它可能不值得,但对于更复杂的代码,使用切片而不是原始指针可能更方便。

问题

它是否正确?

这显然违反了借用规则:两个线程可能同时持有对同一内存位置的可变引用。但是,它们可能永远不会写入相同的索引。

可变引用别名没有被列为不安全的超级大国,但该列表并不详尽。

0 投票
1 回答
34 浏览

c - 为什么我的基本情况会(错误地)立即触发?

我正在尝试在 C 中实现合并排序算法。在递归数组拆分函数中,尽管有 return 语句,但我的基本情况会无限发生,并且永远不会调用合并函数。这是我的代码:

为什么在达到基本情况后不会调用合并函数?对不起,如果我没有方便地表达这个问题,希望有人可以在这里帮助我,谢谢。

0 投票
2 回答
111 浏览

c++ - Aliasing and pointer-interconvertability

Given the following code:

The generated code is:

Here the compiler assumes aliasing.

If member t is not present, the types X and int are pointer-interconvertible. As so, the compiler must generate code as if the references could alias.

But if member t is present, they should no longer be pointer-interconvertible and code for the non-aliased case should be generated. But in both cases the code is identical except the relative address of member k.

The assembler:

As an counter-example

in the above version the generated code assumes no aliasing, but the types are pointer-interconvertible.

Can anyone explain this?

0 投票
2 回答
222 浏览

python - Python 别名

我知道给定这段代码

然后通过这样做

我将更改 a 和 c 的第一个位置。有人可以解释为什么当我这样做时这不适用:

IE。为什么c不等于b?

0 投票
2 回答
92 浏览

c++ - C++ template/aliasing - Type/value mismatch at argument 1 in template parameter list

I'm getting my feet wet with aliasing in templated classes, and haven't been able to compile the following code:

I get two compile errors, a type-value mismatch at argument 1 in template parameter list, and a template argument 2 is invalid. However, if I write the following code instead:

then it compiles without errors. According to my understanding, these two statements should do the same thing. What am I not understanding correctly?

0 投票
2 回答
89 浏览

c - 在 C 中复制 mem 块时防止内存混叠

我想我生锈了,所以请和我一起裸露。我会尽量简短。

Q1。当尝试复制缓冲区时,比如buf2buf1,以下检查是否足以防止混叠?

Q2。如果是这样,我们是否可以像这样选择性地使用其中一个 memcopy()memmove()视情况而定?

它可以工作,但我对切换到memmove(). 我认为该标准证实了我的怀疑(当我需要它们时永远找不到这些该死的东西......使用C99顺便说一句),但由于代码有效,我想更加确定,因为如果它可以这样它会拯救我从复制buf2开始,使用副本并在完成后释放它。

0 投票
1 回答
60 浏览

c - 如何使用联合或可能使用枚举进行别名?

我已经浏览了这篇非常好的文章

https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule

但是我不了解使用 union 的别名,我对别名的了解是 with char/ unsigned char,根据这篇文章,这是一个编译器异常。所以,我过去是如何做混叠的char/ unsigned char

如果我在某些地方错了,请纠正我:)

问:那么我怎样才能对 union 做同样的事情呢?

注意我没有遇到多少联合,所以我也不知道它的正确用途。

我也搜索过别名enum,但不太了解,

问:如果可能的话,我们将如何使用枚举?

对不起我的英语,不是那么好,请公开建议纠正我在问题中的误解或术语以纠正......

0 投票
1 回答
119 浏览

c++ - Clang 的 __restrict 不一致?

我正在研究高度“可向量化”的代码,并注意到关于 C++ __restrict 关键字/扩展 ~,即使在简单的情况下,Clang 的行为与 GCC 相比也是不同且不切实际的。

对于编译器生成的代码,减速大约是 15 倍(在我的具体情况下,不是下面的示例)。

这是代码(也可在https://godbolt.org/z/sdGd43x75获得):

C++ (__restrict) 和 C 代码(使用 std 限制)都会发生这种情况。

我怎样才能让 Clang 明白指针总是指向不相交的存储?

0 投票
0 回答
24 浏览

c++ - 使用 Eigen 的 SparseLU 就地解决时是否存在混叠问题?

我正在使用 Eigen,试图最小化内存分配,并想做这样的事情:

文档没有说太多。我已经用许多矩阵A和向量对它进行了测试X,这似乎有效,但对于矩阵还没有X。我也没有测试 Eigen 是否检测到这一点,然后默默地分配一个临时的。

此外,知道这应该有效还是不应该有效,我会感到更安全。

由于 SparseLU 有方法matrixL(),而matrixU()后者又有方法solveInPlace(),我想答案是肯定的。但不幸的是,我不能简单地使用这些方法,因为我还必须应用排列。