所以今天我认为(int foo()
事实上第一次)实际上不同于int foo(void)
第一个允许任意数量的输入而第二个允许零.
是否int foo()
只是忽略任何给定的输入?如果是这样,那么允许这种形式的功能有什么意义呢?如果没有,你怎么能访问它们,这与变量参数列表(例如类似的东西int foo (int argc, ...)
)有何不同?
所以,我知道C
你需要将代码链接到数学库libm
,以便能够使用它的功能.今天,当我试图向朋友证明这一点,并解释为什么你需要这样做时,我遇到了以下我不理解的情况.
请考虑以下代码:
#include <math.h>
#include <stdio.h>
/* #define VARIABLE */
int main(void)
{
#ifdef VARIABLE
double a = 2.0;
double b = sqrt(a);
printf("b = %lf\n",b);
#else
double b = sqrt(2.0);
printf("b = %lf\n",b);
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果VARIABLE
已定义,则需要libm
按照通常的预期进行链接; 否则你得到通常的main.c:(.text+0x29): undefined reference to sqrt
链接错误,表明编译器找不到该函数的定义sqrt
.我很惊讶地看到,如果我评论#define VARIABLE
,代码运行正常,结果是正确的!
为什么libm
在使用变量时我需要链接到但是在使用文字常量时我不需要这样做?编译器如何找到sqrt
未链接库的定义?我gcc 4.4.5
在linux下使用.
我最近将我的OSX升级为山狮,因为我无法使用Qt Creator编译我的项目.我收到如下错误:
/Users/user/codes/lib/io/xdmfWriter.cpp:63: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
/Users/user/codes/lib/io/xdmfWriter.cpp:-1: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/Users/user/codes/lib/io/xdmfWriter.cpp:63: instantiated from here
// xdmfWriter.cpp -- line 63:
gridName << xdmfName_ << "." << timeStep;
Run Code Online (Sandbox Code Playgroud)
gridName
是一个std::ostringstream
对象,xdmfName_
是一个std::string
声明为类的私有成员xdmfWriter
并在类构造函数中初始化的变量.我以前没有这个问题......有什么想法吗?
我正在尝试为我的代码实现一个简单的序列化/反序列化方法,以便能够使用 MPI 通过网络传递对象。在一个理想的世界,我会用Boost.Serialization
和Boost.MPI
为,但他们无法在一些集群的安装我有机会获得,所以我考虑这个做我自己。
我的策略是将每个对象序列化为一个std::stringstream
对象,然后通过MPI_Send
usingMPI_CHAR
作为数据类型发送消息。在这种情况下,我将std::stringstream::str()::c_str()
作为指针和std::streaingstream::str()::size()*sizeof(char)
消息的大小传递。
我已经想出了如何将所有内容序列化为一个std::stringstream
对象。我的反序列化方法也接受一个std::stringstream
对象并将所有内容反序列化。这很好用,除非我不知道如何std::stringstream
从char
s数组创建对象并避免从数组到流中的额外副本。我应该更改我的反序列化方法以直接char
使用 using数组memcpy
吗?
我一直认为像const int *a
means 这样的语句a
是int
指向const
数据的指针,因此不应该修改它指向的值.事实上,如果你这样做const int a [] = {1,2,3}
,然后发出a[0] = 10
你将得到编译器错误.
然而,令我惊讶的是,以下编译没有任何警告,运行得很好.
#include <stdio.h>
#include <string.h>
int main (){
const int a [] = {1, 1, 1};
const int b [] = {2, 2, 2};
memcpy((void*) &a[0], (const void*)&b[0], 3*sizeof(int));
int i;
for (i=0; i<3; i++) printf("%d\n",a[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么允许这样做?这是由于演员吗?当我做memcpy(&a[0], (const void*)&b[0], 3*sizeof(int));
编译时,及时生成以下警告:
cpy.c: In function ‘main’:
cpy.c:9:3: warning: passing argument 1 of ‘memcpy’ discards …
Run Code Online (Sandbox Code Playgroud) 我有一个关于一个问题void*
,并void**
和我知道这是种一个老问题,并已要求(有些)在计算器之前.所以问题如下:
当我在ubuntu 10.10下使用gcc 4.4.3编译此代码时,我收到以下警告:
zz.c: In function ‘main’:
zz.c:21: warning: passing argument 1 of ‘bar’ from incompatible pointer type
zz.c:9: note: expected ‘void **’ but argument is of type ‘float **’
Run Code Online (Sandbox Code Playgroud)
为什么将变量x作为foo()的参数传递是可以的,但它不能将变量y作为bar()的参数传递.我可以通过将两个变量显式地转换为void*
和void**
预期的方式来解决这个问
void foo (void* a){
}
void bar(void **a){
*a = (float *) malloc(100*sizeof(float));
}
int main (){
float *x = (float*) malloc(100*sizeof(float));
foo(x);
free(x);
float *y;
bar(&y);
free(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 好吧,所以我搞砸了!我正在使用具有以下结构的存储库
master
|
v
A--B--C--D--E--F--G
\
\
H--I
^
|
feature
Run Code Online (Sandbox Code Playgroud)
我正在feature
分支上工作,一旦完成,我就合并到master
. 这导致了我必须手动修复的合并冲突......我认为我做得正确。然而今天,我的同事告诉我,我破坏了他们在 中开发的东西F
。这些损坏的部分与我添加的内容无关——显然在解决冲突时我删除了一堆东西。
我如何“恢复”存储库,以便在保留我的更改的同时恢复它们的更改?更糟糕的是,我已经删除了feature
本地存储库上的分支,并且它没有被推送到原点。这就是仓库现在的样子
master
|
v
A--B--C--D--E--F--G--J
Run Code Online (Sandbox Code Playgroud)
我努力了
git reset --hard HEAD~
git merge origin/master --no-ff
Run Code Online (Sandbox Code Playgroud)
希望这能让我手动编辑合并的文件,但它总是自动提取最新的......
假设你有一个这样的简单类:
class foo{
private:
int* mData;
int mSize;
public:
foo(int size){
mSize = size;
mData = new int [mSize];
}
~foo() {
mSize = 0;
delete [] mData;
}
};
Run Code Online (Sandbox Code Playgroud)
然后在主要内部你做:
int main () {
static int HUGE = 100000000;
foo a(HUGE);
// do something useful with a
// .
// .
// .
// Now I'm done with a; I do not need it anymore ...
foo b(HUGE);
// do something useful with b
// Ok we are done …
Run Code Online (Sandbox Code Playgroud) 我为输出使用VTK数据类型.由于我的数据变得越来越大,用ASCII编写它需要相当长的时间,而这正是我迄今为止所做的.
我需要将其更改为二进制格式,但问题是该文件有一些标题(请参阅http://www.vtk.org/VTK/img/file-formats.pdf),即使对于二进制文件也需要用ASCII编写.
现在我没有足够的二进制格式经验,我的第一次尝试是通过打开两个流
ofstream asciiWriter(file_name.c_str());
ofstream binWriter(file_name.c_str(), ios::app | ios::binary);
Run Code Online (Sandbox Code Playgroud)
问题是输出似乎是无序的,asciiWriter
并且binWriter
没有以正确的顺序输出,所以我不能在ParaView中后处理我的文件.我试过的一件事是使用asciiWriter.flush()
和binWriter.flush()
每当我完成标题/数据写入,但这也没有帮助.
我该怎么办?
PS:我不想使用VTK包本身...它的巨大并增加了我的代码依赖!
这是我在生产中运行的代码的缩小版本.我发现,我的真实代码在gcc和英特尔编译器下表现不同,我最好的猜测是未定义的行为.请考虑以下示例代码:
#include <iostream>
struct baseFunctor{
virtual double operator()(double x) const = 0;
};
class mathObj{
const baseFunctor *tmp1, *tmp2;
public:
void set(const baseFunctor& b1, const baseFunctor& b2){
tmp1 = &b1;
tmp2 = &b2;
}
double getValue(double x){
return (tmp1->operator()(x) + tmp2->operator()(x));
}
};
int main () {
mathObj obj;
struct squareFunctor: public baseFunctor {
double operator()(double x) const { return x*x; }
};
struct cubeFunctor: public baseFunctor {
double operator()(double x) const { return x*x*x; }
};
obj.set(squareFunctor(), cubeFunctor());
std::cout …
Run Code Online (Sandbox Code Playgroud)