好吧,我一直在和一位朋友谈论编译器和程序的优化,他建议这样n * 0.5做比n / 2.我说编译器会自动执行这种优化,所以我编写了一个小程序来查看n / 2和之间是否存在差异n * 0.5:
师:
#include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[]) {
int i, m;
float n, s;
clock_t t;
m = 1000000000;
t = clock();
for(i = 0; i < m; i++) {
n = i / 2;
}
s = (float)(clock() - t) / CLOCKS_PER_SEC;
printf("n = i / 2: %d calculations took %f seconds (last calculation = %f)\n", m, …Run Code Online (Sandbox Code Playgroud) 这似乎应该非常简单,但出于某种原因,我没有让它工作.我有一个名为seq的字符串,如下所示:
ala
ile
val
Run Code Online (Sandbox Code Playgroud)
我想取前3个字符并将它们复制到不同的字符串中.我使用命令:
memcpy(fileName, seq, 3 * sizeof(char));
Run Code Online (Sandbox Code Playgroud)
这应该是fileName = "ala",对吧?但出于某种原因,我明白了fileName = "ala9".我正在解决这个问题fileName[4] = '\0',但我想知道为什么我会得到那个9.
注意:将seq更改为
ala
ile
val
ser
Run Code Online (Sandbox Code Playgroud)
并重新运行相同的代码,fileName成为"alaK".不再是9,但仍然是一个错误的角色.
我在返回语句之前打印我正在返回的值,并告诉我的代码打印函数调用后立即返回的值.但是,在我的第一个print语句之后和第二个print语句之前,我得到了一个分段错误(也有趣的是,这总是在第三次调用函数时发生;从不是第一次或第二次,从不第四次或更晚).我尝试打印出我正在处理的所有数据,看看我的其余代码是否正在做一些它可能不应该做的事情,但到目前为止我的数据看起来很好.这是功能:
int findHydrogen(struct Amino* amino, int nPos, float* diff, int totRead) {
struct Atom* atoms;
int* bonds;
int numBonds;
int i;
int retVal;
int numAtoms;
numAtoms = (*amino).numAtoms;
atoms = (struct Atom *) malloc(sizeof(struct Atom) * numAtoms);
atoms = (*amino).atoms;
numBonds = atoms[nPos].numBonds;
bonds = (int *) malloc(sizeof(int) * numBonds);
bonds = atoms[nPos].bonds;
for(i = 0; i < (*amino).numAtoms; i++)
printf("ATOM\t\t%d %s\t0001\t%f\t%f\t%f\n", i + 1, atoms[i].type, atoms[i].x, atoms[i].y, atoms[i].z);
for(i = 0; i < numBonds; i++)
if(atoms[bonds[i] - totRead].type[0] == …Run Code Online (Sandbox Code Playgroud) 我有一个指向类中定义的成员函数的指针,例如:
class Example {
void (Example::*foo)();
void foo2();
};
Run Code Online (Sandbox Code Playgroud)
在我的主代码中,我将foo设置为:
Example *a;
a->foo = &Example::foo2;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调用foo时:
a->foo();
Run Code Online (Sandbox Code Playgroud)
我得到以下编译时错误:"错误:明显调用的括号前面的表达式必须具有(指向 - ) - 函数类型".我假设我在某处弄错了语法,有人可以指出我吗?
我正在尝试使用OpenMP在数组中添加数字.以下是我的代码:
int* input = (int*) malloc (sizeof(int)*snum);
int sum = 0;
int i;
for(i=0;i<snum;i++){
input[i] = i+1;
}
#pragma omp parallel for schedule(static)
for(i=0;i<snum;i++)
{
int* tmpsum = input+i;
sum += *tmpsum;
}
Run Code Online (Sandbox Code Playgroud)
这不会产生正确的结果sum.怎么了?
在性能方面,为矩阵分配连续内存块与单独内存块相比有什么好处?即,而不是像这样编写代码:
char **matrix = malloc(sizeof(char *) * 50);
for(i = 0; i < 50; i++)
matrix[i] = malloc(50);
Run Code Online (Sandbox Code Playgroud)
给我 50 个不同的块,每个块 50 个字节和一个块 50 个指针,如果我改为写:
char **matrix = malloc(sizeof(char *) * 50 + 50 * 50);
char *data = matrix + sizeof(char *) * 50;
for(i = 0; i < 50; i++) {
matrix[i] = data;
data += 50;
}
Run Code Online (Sandbox Code Playgroud)
给我一个连续的数据块,有什么好处?避免缓存未命中是我唯一能想到的,甚至这也仅适用于少量数据(小到足以放入缓存),对吗?我已经在一个小型应用程序上对此进行了测试,并且注意到了一个小的加速并且想知道为什么。
我最近开始尝试学习 MPI 以用 C 进行编码。我正在尝试编写非常小的测试代码,以确保我知道自己在做什么。不幸的是,我似乎对其中一个有问题,它将矩阵与向量相乘并输出结果向量。具体来说,当我打电话时:
MPI_Reduce(c, myc, 3, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
Run Code Online (Sandbox Code Playgroud)
其中 myc 是每个处理器计算的向量的一部分,我的最终结果是对于所有 i,c[i] = 0。计算 myc 的代码是正确的(使用一个处理器进行检查并输出 myc 而不是 c)。我想我在这里做了一些非常愚蠢的事情,但不知道是什么。
我正在尝试熟悉C++中的运算符.我想我会用一个简单的矢量加法案例来做到这一点.不幸的是,我似乎遇到了一些问题.我的班级定义如下:
#ifndef _MVEC_H_
#define _MVEC_H_
#include "Error.h" //I define things like throw(message) here, it works and is not the issue
class MVec {
private:
double vec[3];
public:
MVec();
MVec(double &);
MVec(double *);
MVec(MVec &);
MVec & operator=(MVec &);
inline double & operator[](const int i);
inline const double & operator[](const int i) const;
MVec operator+(const MVec &) const;
~MVec();
};
MVec::MVec() {}
MVec::MVec(double &a) {
for(int i = 0; i < 3; i++)
vec[i] = a;
}
MVec::MVec(double *a) {
for(int …Run Code Online (Sandbox Code Playgroud) 我正在使用K&R书籍在Windows机器上学习C. 我正在尝试bare bones Unix word count计算行,字符和单词的程序().虽然这个程序正确计算了字符数,但是没有.我的输出中的行和单词总是0和1,无论我输入什么.我也对程序的一部分感到有些困惑,我将接下来 -
#include<stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c,state, nc,nw,nl;
nl=nw=nc=0;
state=OUT;
while(c=getchar()!=EOF)
{
++nc;
if(c=='\n')
++nl;
if(c=='\n'||c=='\t'||c==' ')
state=OUT;
else if(state==OUT)
{
state=IN;
++nw;
}
}
printf("\n No. of characters, lines and words are : %d,%d,%d\n",nc,nl,nw);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从它的外观,该程序正在使用nc,nl并nw分别以计数的字符数,线和文字输入流中输入.到目前为止,我对程序逻辑的理解是 -
IN并且OUT是用于指示程序当前状态的两个变量.IN表示程序当前在一个单词内部 - 换句话说 - 到目前为止在输入的字符中没有遇到空格,换行符或制表符.或者我认为.while循环之前,将STATE设置为out.这表明现在没有遇到任何消息.Ctrl+Z),字符 …我正在使用C.我遇到了使用fscanf函数指针的问题.当我尝试做的时候:
int *x;
/* ... */
fscanf(file, "%d", x[i]);
Run Code Online (Sandbox Code Playgroud)
我的编译器给了我一个警告说"格式参数不是一个指针",代码就没有运行(我得到一条消息说"Water.exe已经停止工作").如果我用*替换x,它只是不编译...这只是一个语法问题?