这可能只是我在某种程度上是愚蠢的,但我对C++相对较新,所以请原谅我白痴.我正在努力教自己如何使用运算符.我已经定义了一个非常基本的运算符如下:
Matrix::Matrix operator+(Matrix a1, Matrix a2) {
if(a1.rows != a2.rows || a1.cols != a2.cols) {
std::cout << "ERROR: Attempting to add matrices of non-equal size." << std::endl;
exit(6);
}
int i, j;
Matrix c(a1.rows, a1.cols);
for(i = 0; i < a1.rows; i++)
for(j = 0; j < a1.cols; j++)
c.val[i][j] = a1.val[i][j] + a2.val[i][j];
return c;
}
Run Code Online (Sandbox Code Playgroud)
Matrix类表示一个矩阵,并且有一个构造函数,它将两个int作为输入(分别是矩阵中的行数和列数),并创建一个具有适当大小的双精度数组(名为val).此函数的作用应该是因为c的值是正确的,但它似乎也会破坏a1和a2.也就是说,如果我写的话
Matrix c = a + b;
Run Code Online (Sandbox Code Playgroud)
它给出了正确的结果,但是a和b不再可用,并且在代码结束时我得到一个glibc错误声称我试图在它们已经被破坏时破坏a和b.为什么是这样?
我正处于编写相当大的代码的开始阶段.我已经定义了一个类:
class GPUMD {
private:
double xhi, xlo, yhi, ylo, zhi, zlo;
int numAtoms;
Atom *atoms;
public:
GPUMD();
~GPUMD();
};
Run Code Online (Sandbox Code Playgroud)
析构函数定义如下:
GPUMD::~GPUMD() {
if(atoms != NULL)
delete [] atoms;
}
Run Code Online (Sandbox Code Playgroud)
现在,代码执行此操作:
int main(int argc, char *argv[]) {
GPUMD gpumd;
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我得到一个glibc检测到错误:尝试释放无效指针.使用valgrind,我看到这个错误跟踪到我的析构函数GPUMD.由于某种原因,即使我没有为该指针指定任何内容,atoms!= NULL测试也会返回true.这是为什么?
编辑:构造函数定义为:
GPUMD::GPUMD() {}
Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个非常基本的CUDA代码(向量添加)来自学CUDA编程的基础知识.当我写一个.cu文件时,我已经有了它的工作,但现在我正在尝试使用链接在一起的.c和.cu文件.我的main.c文件如下:
#include "Test.h"
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n = 1000;
size_t size = n * sizeof(float);
int i;
float *h_a = malloc(size), *h_b = malloc(size), *h_c = malloc(size);
for(i = 0; i < n; i++) {
h_a[i] = h_b[i] = i;
}
addVec(h_a, h_b, h_c, n);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
这里,Test.h简单地说:
void addVec(float *, float *, float *, int);
Run Code Online (Sandbox Code Playgroud)
我的vecAdd.cu文件说:
#include "Test.h"
__global__ void vecAdd(float *a, float *b, float *c, int n) {
int i = blockDim.x …Run Code Online (Sandbox Code Playgroud) 我正在维护一些代码并遇到了这个片段:
int num = 0;
float sum = 0.0;
bool value[rows][cols]; //initialized elsewhere in the code
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
if(value[i][j] == true) {
sum += value[i][j]; //shouldn't this be equivalent to "sum += 1;"
//as it is in the if block?
num++;
}
float ans = 1.0;
if(num > 12)
ans = sum / num;
Run Code Online (Sandbox Code Playgroud)
编写这段代码的人最初在这里做了一些非常聪明的事情,或者应该ans永远是1?据我所知,num并且sum应该始终是完全相同的值,不是吗?
我用Java作为编程语言有相当多的练习,但我对C完全是新手.我理解头文件包含方法和变量的前向声明.这与Java中的抽象类有什么不同?
好的...所以我有一个相当有趣的错误...我声明一个名为FileWriter的文件,我让它通过以下for循环:
for (int i = 0; i < a.radtot; i++) {
file.write("" + i * a.rstep);
for (int n = 0; n < a.timetot; n++) {
file.write("\t " + T[n][i]);
System.out.println(T[n][i] + " " + n + " " + i);
}
file.write("\n");
}
Run Code Online (Sandbox Code Playgroud)
在它结束时,System.out.println命令打印出我期望的内容,但文件中途中断...就像在,而不是打印出System.out所做的一切......它停在中间.有谁碰巧知道为什么会这样做?难道我做错了什么?
有没有办法在C中创建一个可变大小的双脚本数组(不是C++,只是C)?我知道要创建一个可变大小的单脚本数组,你只需使用一个指针,例如
float *array;
array = (float *) calloc(sizeof(float), n);
Run Code Online (Sandbox Code Playgroud)
创建一个单一脚本的大小为n的浮点数组.是否有类似于我可以为双重脚本数组做的事情?
我很难过.真的无法想出这一个.
int main(){
string blah = "text";
example(&blah);
}
void example(string *h){
*h[3]='l';
}
Run Code Online (Sandbox Code Playgroud)
我在上面的函数中尝试做的是编辑原始字符串的第4个字符而不使用全局变量.我原以为这会有效,因为我知道我可以做一些与int类似的东西.猜测它与字符串/字符转换有关,但我无法在网上找到太多信息.