我有以下模式:
std::vector包含对象的原始指针(我知道原始指针是"邪恶的",但它是需要维护的遗留软件).伪代码:
for each pointer in vector
{
if (SomeTest(pointer))
{
DoSomething(pointer)
delete pointer
remove pointer from vector
}
}
Run Code Online (Sandbox Code Playgroud)
我无法想出一些干净利落的代码.
这个链接提供了不同的方法,但它们看起来或多或少都很麻烦.
我现在使用的繁琐解决方案:
for(auto & p : v)
{
if (SomeTest(p))
{
DoSomething(p);
delete p;
p = nullptr;
}
}
v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
Run Code Online (Sandbox Code Playgroud) 我需要一种跨平台的方式来获取当前的工作目录(是的,getcwd做我想要的).我认为这可能会成功:
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd // stupid MSFT "deprecation" warning
#elif
#include <unistd.h>
#endif
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s_cwd(getcwd(NULL,0));
cout << "CWD is: " << s_cwd << endl;
}
Run Code Online (Sandbox Code Playgroud)
我读到了这个:
应该没有内存泄漏,它也可以在Mac上运行,对吗?
更新:我担心这里仍然存在错误(我试图避免创建一个具有确定长度的char数组,因为没有正确的方法来获得getcwd的合适长度):
char* a_cwd = getcwd(NULL,0);
string s_cwd(a_cwd);
free(a_cwd); // or delete a_cwd?
Run Code Online (Sandbox Code Playgroud) 代码:
#define OPPOSITE(c) (*((typeof(x) *)&(x)))
int foo(volatile int x)
{
OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);
return x;
}
int bar(volatile int x)
{
OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
结果(-Os):
foo:
mov DWORD PTR [rsp-4], edi
mov eax, DWORD PTR [rsp-4]
mov edx, DWORD PTR [rsp-4]
add eax, edx
mov DWORD PTR [rsp-4], eax
mov eax, DWORD PTR [rsp-4]
ret
bar:
mov DWORD PTR [rsp-4], edi
mov eax, DWORD PTR [rsp-4]
add eax, eax
ret
Run Code Online (Sandbox Code Playgroud)
或ARM gcc。( -O3 …
当我使用theSendMessage函数时HWND_BROADCAST,应用程序挂起.申请长时间没有回复.
有谁能解释为什么?
抱歉尴尬的标题,但我找不到更好的标题.
考虑这个示例代码(除了说明问题之外没有任何目的):
#include <vector>
void FooBar(int);
void func1()
{
static std::vector<int> vec {1, 2, 3, 4};
for (auto & v : vec)
FooBar(v);
}
void func2()
{
for (auto & v : std::vector<int> {1, 2, 3, 4})
FooBar(v);
}
Run Code Online (Sandbox Code Playgroud)
可以在此处找到对此的反汇编
在func1静态vec向量中应该在启动时一劳永逸地构造.实际上上面提到的godbolt上的反汇编表明静态的初始化vec只在第一次调用时完成,func1而不是在启动时完成,但这不是重点.
现在考虑func2:这里向量直接在for语句中声明为"内联"(不确定这是如何实际调用的),但当然每次func2调用时都会构造该向量.
是否有静态声明向量的方式与里面for的语句,像for (auto & v : static std::vector<int> { 1, 2, 3, 4})这是不幸的是没有合法的C++.
我正在使用基于的二维数组std::array。
基本上代替:
MyType myarray[X_SIZE][Y_SIZE];
Run Code Online (Sandbox Code Playgroud)
我有:
std::array<std::array<MyType, Y_SIZE>, X_SIZE> myarray;
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但是IMO的声明不是很可读。
有没有一种方法可以使用一些聪明的C ++模板机制来声明,所以声明可能看起来像这样?
My2DArray<Mytype, X_SIZE, Y_SIZE> myarray;
Run Code Online (Sandbox Code Playgroud) 我想创建类库,一个函数,其参数是一个未知大小的矩阵,用户将创建自己的矩阵并使用自己的大小并将其传递给此函数,对其矩阵进行一些操作,如下所示,将是功能
calculateDeterminantOfTheMatrix( int matrix[][])
{
some Operations to do on matrix
}
Run Code Online (Sandbox Code Playgroud) 鉴于C++ 11标准现在得到了大多数体面编译器的良好支持,2014年使用原始指针的主要原因有哪些?
我确定了几个场景:
您正在扩展大量使用原始指针的遗留代码库,并且您希望保持样式的一致性.
您正在使用仅导出原始指针的库,但我猜您仍然可以使用强制转换.
您希望利用指针的功能来提供多个级别的间接.(我不太清楚C++ 11是否足以知道是否可以使用智能指针或使用其他技术来实现.)
您认为哪些其他场景适合使用指针?
你今天甚至会建议学习一般的指针吗?
我收到以下gcc格式 - 截断警告:
test.c:8:33: warning: ‘/input’ directive output may be truncated writing 6 bytes into a region of size between 1 and 20 [-Wformat-truncation=]
snprintf(dst, sizeof(dst), "%s-more", src);
^~~~~~
test.c:8:3: note: ‘snprintf’ output between 7 and 26 bytes into a destination of size 20
snprintf(dst, sizeof(dst), "%s-more", src);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
像这样的代码:
char dst[20];
char src[20];
scanf("%s", src);
snprintf(dst, sizeof(dst), "%s-more", src);
printf("%s\n", dst);
Run Code Online (Sandbox Code Playgroud)
我知道它可能会被截断 - 但这正是我首先使用snprintf的原因.有没有办法让编译器明白这是预期的(不使用编译指示或-Wno格式截断)?
C代码是否有任何方法可以判断它是否在乘法快速的架构上编译?是否有一些宏__FAST_MULT__或在这些架构上定义的东西?
例如,假设您正在实现一个函数,通过shift-and-add方法确定64位整数的汉明权重.有两种最佳算法:一种需要17次算术运算,而另一种只需要12次,但其中一种是乘法运算.因此,如果您在硬件上运行,第二种算法的速度提高了30%,其中乘法所需的时间与添加时间相同 - 但是,在将乘法实现为重复加法的系统上,要慢得多.
因此,在编写这样的函数时,能够在编译时检查是否是这种情况并在适当时在两种算法之间切换是有用的:
unsigned int popcount_64(uint64_t x) {
x -= (x >> 1) & 0x5555555555555555; // put count of each 2 bits into those 2 bits
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); // put count of each 4 bits into those 4 bits
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; // put count of each 8 bits into those 8 bits
#ifdef __FAST_MULT__
return (x …Run Code Online (Sandbox Code Playgroud)