为什么对char*和std :: string的优化结果有所不同?
void f1(int* count, char* str) { for (int i = 0; i < *count; ++i) str[i] = 0; } void f2(int* count, char8_t* str) { for (int i = 0; i < *cou…
如何使用C关键字restrict来正确修饰返回指针?
在一些文档中,我了解到我们应该使用“restrict”来修饰函数参数或内存分配语句。像这样: void(int*restrict paraA){} int*restrict A = (int*)mallo…
__restrict__ 可以应用于shared_ptr吗?
智能指针是底层的指针,那么有没有什么方法可以将函数的 shared_ptr 参数定义为不别名另一个 shared_ptr 或任何类型的另一个指针? 或者由于某种原因…
“易失性限制”指针有实际用途吗?
我可以看到 const volatile 限定变量的实际用途,就像 const volatile uint64_t seconds_since_1970; 底层硬件机制每秒更新该值一样,但该变量在(可…
CUDA:如何在指向数组的指针数组上应用 __restrict__ ?
使用两个 __restrict__ int 数组的内核可以正常编译: __global__ void kerFoo( int* __restrict__ arr0, int* __restrict__ arr1, int num ) { for (…
__restrict 和 shared_ptr 黑客
以下安全吗? struct K { ... } struct A { A(int psize) : size(psize), foo(nullptr), bar(nullptr) { auto dataptr = (K*)_aligned_malloc(sizeof(…
如何区分 C 或 C++编译器认为指针没有别名
我有一个接收指针数组的函数,如下所示: void foo(int *ptrs[], int num, int size) { /* The body is an example only */ for (int i = 0; i < size…
防止两个对象内部出现混叠
我有一个与此类似的函数签名 void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out); 在内部,矩阵类有一个表示 mx n 组件的 float* d…
C 用 typedef 限制
我现在正在做一些代码,但使用restrict关键字遇到了一些问题。 typedef int* pt; int foo(pt a, pt b) { ... /* stuff */ } 如果我想限制a和b怎么办?…
在 C 中使用限制指针时,可以使用其初始标识符来更改变量吗?
在 C 中使用 restrict 指针时,是否可以使用其初始标识符更改变量?例如: int foo = 0; int * restrict fooPtr = &foo; ++(*fooPtr); // Part 1: foo…
这是限制指针的无效使用吗?
假设我有一个大数组,我计算索引并将其传递给第二个函数。举一个简单的例子,例如: void foo(float* array, float c, unsigned int n) { for (unsign…
GCC C++ 吗?编译器考虑 __restrict - 语句吗?
我已经研究了在通过 GCC 编译器编译 C++ 代码时 __restricting 某些指针的影响。 事实证明,不仅运行时保持完全相同,而且可执行文件似乎也没有改变,…