有时我在C++中看到不同的数组访问样式,并认为它可能与程序集寻址模式有关:
C++:
int * aa=new int[2];
0[aa]=15; //a little different than aa[0]
1[aa]=15;
aa[0]=15;
aa[1]=15;
printf("%d %d \n",aa[0],aa[1]);
Run Code Online (Sandbox Code Playgroud)
部件:
__asm
{
mov aa[0],ebx
mov aa[1],eax
mov 0[aa],ebx
mov 1[aa],eax
}
Run Code Online (Sandbox Code Playgroud)
这个C++数组访问符号是标准吗?如果是,它是从汇编寻址模式派生的吗?
当我尝试时[aa]1=5;,编译器给出
"左操作数必须是l值".
//当我尝试指针算术时,
*(aa + 1)= 0 //没有错误
*(aa + 0)= 0 //没有错误:)
对于运算符[]重载,此规则是否相同?
MSVC++ 2010
谢谢.
我正在学习使用内在函数而不是asm-inlining.昨天,他们正在工作,但我今天总是得到错误.一无所获.
#include <iostream>
#include <intrin.h> // immintrin.h, smmintrin.h ... tried all, never worked
using namespace std;
int main()
{
_m256_zeroupper(); // __mm256_zeroupper(); does not work too
_mm128 x; // __mm128 x; does not work too
_mm256 y; // __mm256 y; does not work too
_m256_zeroupper(); // __mm256_zeroupper(); does not work too
cout << "Hello world!" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是错误.我尝试了不同内在函数的所有头文件,但错误是相同的.还重新安装了gcc但没有奏效.
我哪里错了?我需要添加什么来实际声明这些内部变量和函数?
C:\indirmeDenemesi\hello_intrin\main.cpp||In function 'int main()':|
C:\indirmeDenemesi\hello_intrin\main.cpp|8|error: '_mm256_zeroupper' was not declared in this scope|
C:\indirmeDenemesi\hello_intrin\main.cpp|9|error: '_mm128' was not declared in …Run Code Online (Sandbox Code Playgroud) #ifdef DOUBLE_PRECISION_COMPUTE
#define MAKE_PRECISE(N) ...
#endif
// if not defined, leave as it is. MAKE_PRECISE(N) (N)
Run Code Online (Sandbox Code Playgroud)
上面的宏(用右边的redifiniton替换"......")可以进行下面的操作吗?
double PI=MAKE_PRECISE(3.14159265359f);
Run Code Online (Sandbox Code Playgroud)
变
double PI=3.14159265359;
double area= MAKE_PRECISE(3.14159265359f)*r*r;
Run Code Online (Sandbox Code Playgroud)
变
double area= 3.14159265359*r*r;
Run Code Online (Sandbox Code Playgroud) 我正在使用本地函数定义来运行代码块N次,就像C#的并行版本一样,但是f当我在同一范围内使用两个或更多个函数时,它会给函数带来多个定义错误.如何在相同范围内每次使用后逐步更改功能名称,如f1,f2,f3,..
定义:
#ifndef PARALLEL_FOR
#define PARALLEL_FOR(N,O) \
struct LocalClass \
{ \
void operator()(int i) const O \
} f; \
std::thread threads[N]; \
for(int loopCounterI=0; loopCounterI<N; loopCounterI++) \
{ \
threads[loopCounterI]=std::thread(f,loopCounterI); \
} \
for(int loopCounterI=0; loopCounterI<N; loopCounterI++) \
{ \
threads[loopCounterI].join(); \
} \
#endif
Run Code Online (Sandbox Code Playgroud)
用法:
PARALLEL_FOR(
5,
{std::cout<<100*i<<std::endl;}
);
Run Code Online (Sandbox Code Playgroud)
输出:
0
300
200
100
400
Run Code Online (Sandbox Code Playgroud)
通常加入{\和}\将解决随之而来的电话,但如果我这样做了嵌套并行?
#include <iostream>
#include<eigen3/Eigen/src/Core/Matrix.h> // does not define Eigen::StorageOptions
// need something like this
#include<eigen3/Eigen/src/ everything_in_here >
int main()
{
Eigen::Matrix<double,2,2> mat;
std::cout << mat(0,0) <<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在那里,我试图构建一个矩阵对象,它总是询问 6 个模板参数并带有错误消息:
模板参数数量错误(3,应该是 6)
所以我开始添加它们,第四个是Eigen::StorageOptions并且没有在Matrix.h标头中定义。另外,还有太多的标题需要搜索。那么,我可以将所有文件包含在一个文件中吗#include?
为了轻松地重写一个大的 switch-case,我正在尝试像这样的 constexpr 函数模板(它只是一个只有 cout 的简化版本):
#include <iostream>
template<int T>
constexpr void bigSwitchCase(const int idx)
{
if (T == 1 && T == idx)
{
std::cout << 1 <<std::endl;
return;
}
if (T == idx)
{
std::cout << T << std::endl;
}
else if (T > 1)
{
bigSwitchCase<T - 1>(idx);
}
return;
}
template<>
void bigSwitchCase<1>(const int idx)
{
if (1== idx)
{
std::cout << 1 <<std::endl;
return;
}
}
int main()
{
bigSwitchCase<64>(15);
//bigSwitchCase<4096>(15);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但问题是它生成的代码不如以下版本那么快: …
当我使用螺旋规则时,我在10个螺旋步骤内的下线混淆.有更快的方法吗?
// compiles in VC++ 2010
const void * const ** const volatile *** const **** _foo_;
Run Code Online (Sandbox Code Playgroud)
例如ptr是指向指向指针的指针的指针,它们都是const void但是其中2个是volatile void const
假设我正在使用一个结构在堆上分配并用作
然后我可以简单地重载它的新数组运算符,不要触摸任何delete []运算符
struct alignas(256) MyStruct
{
Item i1,i2;
void * operator new[](unsigned long int size)
{
return aligned_alloc(256,size);
}
void * operator new (unsigned long int size)
{
return aligned_alloc(256,size);
}
};
Run Code Online (Sandbox Code Playgroud)
并认为它没有任何泄漏?
GCC 6.3和c ++ 0x.
这个"基础"类构造了b,medium1和medium2.当我使用"顶级"课程时,它构建了2倍以上.完全构建5次(写入屏幕5次).
问题: 我可以在"基础"类中进行哪些更改,使其仅构建一次.
#include "stdafx.h"
#include<stdlib.h>
class base
{
public:
base(){x=5;printf(" %i ",x);}
int x;
}b;
class medium1:public base{}m1;
class medium2:public base{}m2;
class top:public medium1,medium2{};
int main() {
top ten;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是虚拟基类的陷阱?谢谢你回答我的琐碎问题.
Free()知道要释放多少字节的内存但可以删除[]做同样的事情?如果我们从堆栈而不是堆分配,它们是否可以使用free()和delete []完美地工作?最后一个问题:我们需要在结尾分配NULL吗?
#include <stdio.h>
#include <stdlib.h>
char * malloc2()
{
char * m = (char *)malloc(100);
//so malloc(10000000) cannot cause stack-overflow?
//cast from void * to char *
return m;
}
char * malloc3()
{
static char m[100];
//can [1000000] cause stack overflow?
return m;
}
char * newX()
{
char * output = new char[100];
return output;
}
int main(){
char * p = malloc2();
//sizeof(p) gives 8 because this is on 64 bit OS/CPU
free(p);
//free() knows the …Run Code Online (Sandbox Code Playgroud) 在这里,我从浮点变量中减去128.0和129.0倍.
#include "stdafx.h"
#include<stdlib.h> //is this the problem? Or am i doing something wrong?
int main()
{
float d1=3.0e9;
printf("\n before: %f \n",d1);
for(int i=0;i<2000000;i++) d1=d1-128.0; //doesnt change!
printf("\n after : %f \n",d1);
for(int i=0;i<2000000;i++) d1=d1-129.0; //does change!
printf("\n after2: %f \n",d1);
//is 129 is the minimum step for sub/add ? Isnt this wrong?
//Is this about exponential part 10^9 ?
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:

问题:为什么这个浮点数不会因为小于129的操作数添加/ subbing而改变?因为我选择初始浮动值3.0e9?
当我选择初始值3.0e10时,初始化和两次减法都不起作用.
当我选择初始值3.0e8时,最小变化为17.所以16不会改变.:(
所以,谢谢你的回答.当初始值变小时,最小步长会根据精度变小.
VC++ 2010表达.windows xp 32位.奔腾-M