我有一个只有静态成员的类.
我想在退出时使用"atexit"库函数注册其成员函数之一(下面的代码中的VerifyClean).
在C++常见问题解答说,我必须指定为extern"C"为我要注册这样,就像在下面的例子中的功能.
class Example
{
public:
static void Initialize();
static void DoDirtyStuff {++dirtLevel;}
static void CleanUpStuff {--dirtLevel;}
private:
static void VerifyClean();
// DOESN'T COMPILE: extern "C" static void VerifyClean();
static int dirtLevel;
}
int Example::dirtLevel;
extern "C" void Example::VerifyClean() // DO I NEED extern "C" HERE?
{
assert(dirtLevel == 0);
}
void Example::Initialize()
{
dirtLevel = 0;
atexit(&VerifyClean);
}
Run Code Online (Sandbox Code Playgroud)
我真的必须使用extern"C"吗?
如果我将"atexit"替换为非库函数(在纯C中实现),答案是否会改变?
如果函数VerifyClean是公共的,我决定直接从C++代码调用它,我会得到链接错误或运行时崩溃吗?我问这个是因为声明根本没有提到extern"C",所以常规的C++代码可能会错误地处理函数调用.这在我的MS Visual Studio 2005系统上运行正常.
我经常使用"较长"类型变量赋值为"较短"变量,例如intto short或uint32_tto uint8_t.有一天,我决定使用gcc在我的代码中找到所有这些情况,但令我惊讶的是gcc没有输出任何警告!
int long_value;
short short_value;
std::cin >> long_value; // Example input: 32769
short_value = long_value; // MS Visual Studio complains here at warning level 4
std::cout << "Long: " << long_value << '\n'; // My example output: 32769
std::cout << "Short: " << short_value << '\n'; // My example output: -32767
Run Code Online (Sandbox Code Playgroud)
使用gcc -Wall或gcc -Wconversion没有帮助(gcc没有输出任何警告).实际上,它从不输出任何输入和输出类型的警告(例如long和unsigned char).
我从未在gcc中发现过一个真正的错误,所以我几乎可以肯定这种行为是有道理的.
那么为什么没有警告?
更新:我使用gcc 4.1.2.
我目前正在使用popen由两个编译器编译的代码中的函数:MS Visual Studio和gcc(在linux上).我可能想稍后添加gcc(在MinGW上).
该函数被调用popen为gcc,但_popen对于MSVS,所以我在源代码中添加了以下内容:
#ifdef _MSC_VER
#define popen _popen
#define pclose _pclose
#endif
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我想了解是否存在针对此类问题的标准解决方案(我记得与stricmp/ 的类似情况strcasecmp).具体来说,我想了解以下内容:
_MSC_VER正确的旗帜依赖吗?我选择它是因为我觉得linux环境"更标准".#define放在一些头文件中,那么#include它是在之前还是之后stdio.h(对于这种情况popen)是否重要?_popen被定义为宏本身,我有可能#define失败吗?我应该使用"新"代币my_popen,因为这个或那个原因?我试图使用pimpl模式并在匿名命名空间中定义实现类.这在C++中是否可行?我失败的尝试描述如下.
是否可以在不将实现移动到具有名称(或全局名称)的命名空间的情况下解决此问题?
class MyCalculatorImplementation;
class MyCalculator
{
public:
MyCalculator();
int CalculateStuff(int);
private:
MyCalculatorImplementation* pimpl;
};
namespace // If i omit the namespace, everything is OK
{
class MyCalculatorImplementation
{
public:
int Calculate(int input)
{
// Insert some complicated calculation here
}
private:
int state[100];
};
}
// error C2872: 'MyCalculatorImplementation' : ambiguous symbol
MyCalculator::MyCalculator(): pimpl(new MyCalculatorImplementation)
{
}
int MyCalculator::CalculateStuff(int x)
{
return pimpl->Calculate(x);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用MS Visual Studio中的SSE2指令集.我用它来做16位数据的计算.
假设我有8个值加载到SSE寄存器中.我想为42所有这些添加常量(例如).这是我希望我的代码看起来如何.
__m128i values; // 8 values, 16 bits each
const __m128i my_const_42 = ???; // What should i write here?
values = _mm_add_epi16(values, my_const_2); // Add 42 to the 8 values
Run Code Online (Sandbox Code Playgroud)
现在,我该如何定义常数?以下两种方式有效,但一种方法效率低下,另一方面很难看.
my_const_42 = _mm_set_epi16(42, 42, 42, 42, 42, 42, 42, 42) - 编译器生成8个命令来"构建"常量my_const_42 = {42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0, 42, 0} - 很难理解发生了什么; 改变42到如-42不是小事有没有办法更方便地表达128位常数?
我对编程很陌生,在为一个夏季项目做了一周的努力之后,我真的很感激一些帮助!
我正在尝试读取一个长文本文件,它只是一个长字符串(NB:不是一个实际的编程字符串)的字母,然后将每个字母放入网格中的位置(该程序的目标最终是解决一个wordsearch)到目前为止,我已经提出了下面的程序,它似乎没有生成网格,而只是重新打印文本文件,前面有以下内容:
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;}
\paperw11905\paperh16837\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720
\f0\fs24 \cf0
Run Code Online (Sandbox Code Playgroud)
我写的程序是这样的:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
int main()
{
int i,j;
char myarray[26][26],x;
FILE *myfile;
for (j=0; j<26; j++) //initialise array elements all to zero
{
for (i=0; i<26; i++)
{
myarray[i][j]=0;
}
}
myfile=fopen("*redacted*","r");
if (myfile!=NULL) //check file actually opened
{
for (i=0; i<26; i++)
{
for(j=0; j<26; j++)
{
fscanf(myfile,"%c",&x); //read the values in
myarray[i][j]=x;
}
}
// data is now in …Run Code Online (Sandbox Code Playgroud) 为什么Oracle Java API文档的add()方法TreeSet和HashSet说明:
仅当集合中没有e2时才添加元素e
(e==null ? e2==null : e.equals(e2))
然而,TreeSet使用compareTo(),同时HashSet使用hashCode()确定的平等.两者都忽略了它的价值equals().我担心文档是不准确的,还是我对常规或算法的理解是错误的?
有谁知道对数组进行 k 近似排序的算法吗?
我们被要求找到 k 近似排序的算法,它应该在 O(n log(n/k)) 中运行。但我似乎找不到任何。
K-大约。排序意味着数组和任何 1 <= i <= nk 使得 sum a[j] <= sum a[j] i<=j<= i+k-1 i+1<=j<= i+k
我需要获取文件夹的大小以进行比较。但我通过路径获取文件夹大小。如何只获取我的文件夹的大小?我在中使用以下命令bash:
size=`du -b --max-depth=0 ./main_folder/data`
Run Code Online (Sandbox Code Playgroud)
输出:
12260550 ./main_folder/data
Run Code Online (Sandbox Code Playgroud)
预期的:
12260550
Run Code Online (Sandbox Code Playgroud) 我如何设计一个接受平衡括号和括号的PDA([] []),我很难入门.我需要帮助编写此问题的转换函数.任何帮助表示赞赏
c++ ×4
c ×3
arrays ×2
algorithm ×1
bash ×1
gcc ×1
hashset ×1
intrinsics ×1
java ×1
namespaces ×1
optimization ×1
pimpl-idiom ×1
popen ×1
portability ×1
sorting ×1
sse ×1
treeset ×1