Tag: restrict qualifier

为什么restrict限定符仍允许memcpy访问重叠内存?

我想看看restrict是否会阻止memcpy访问重叠内存。 memcpy函数将n个字节从内存区域src 直接复制到内存区域dest。 内存区域不应重叠。 memmove使用缓冲区,因此不存在重叠内存的风险。 restrict限定符表示,对于指针的生命周期,只有指针本身或直接来自它的值(如pointer + n )才能访问该对象的数据。 如果未遵循意图声明并且该对象由独立指针访问,则将导致未定义的行为。 #include #include #define SIZE 30 int main () { char *restrict itself; itself = malloc(SIZE); strcpy(itself, “Does restrict stop undefined behavior?”); printf(“%d\n”, &itself); memcpy(itself, itself, SIZE); puts(itself); printf(“%d\n”, &itself); memcpy(itself-14, itself, SIZE); //intentionally trying to access restricted memory puts(itself); printf(“%d\n”, &itself); return (0); } 输出() […]

限制结构中的关键字和指针

通过使用restrict关键字,如下所示: int f(int* restrict a, int* restrict b); 我可以指示编译器数组a和b不重叠。 说我有一个结构: struct s{ (…) int* ip; }; 并编写一个带有两个struct s对象的函数: int f2(struct sa, struct sb); 在这种情况下,我怎样才能类似地指示编译器a.ip和b.ip不重叠?

人类可以通过限制限定符做出什么?

如果我正确地获得了C99 restrict关键字,那么用它来限定指针是一个承诺,它引用的数据不会在编译器的后面通过别名修改。 相比之下,我理解const限定符的方式是编译器强制执行的文档,即在人类编写代码的背后不会修改给定对象。 编译器可能会得到一个提示作为副作用,但作为程序员我并不在乎。 以类似的方式,将函数原型中的restrict限定符视为要求用户在调用期间确保独占访问(“避免别名”或可能更强的东西)是否合适? 它应该用作“文件”吗? 另外,有一些事情要理解, restrict限定指针而不是它指向的数据(如const那样)? 编辑:我原本认为restrict可能会影响线程代码,但这似乎是错误的,所以我从问题中删除线程的引用,以避免混淆读者。

C / C ++ __restrict类型

有没有办法定义使用typedef integral / float类型,这意味着没有别名? 相当于(但原始构造)的东西: template struct restrict { T* __restrict data; }; 作为相关的问题,是否有可能问gcc它确定别名/指针的别名是什么?

`const T * restrict`是否保证指向的对象不被修改?

请考虑以下代码: void doesnt_modify(const int *); int foo(int *n) { *n = 42; doesnt_modify(n); return *n; } 其中, doesnt_modify的定义对编译器不可见。 因此,它必须假设, doesnt_modify将对象n指向更改并且必须在return之前读取*n (最后一行不能被return 42;替换return 42; )。 假设, doesnt_modify不会修改*n 。 我考虑了以下内容以允许优化: int foo_r(int *n) { *n = 42; { /* New scope is important, I think. */ const int *restrict n_restr = n; doesnt_modify(n_restr); return *n_restr; } } […]