我编译了boost lib并得到了这些.
//Shared/dynamic link libraries
24/03/2010 11:25 PM 53,248 boost_thread-vc80-mt-1_42.dll
24/03/2010 11:25 PM 17,054 boost_thread-vc80-mt-1_42.lib
24/03/2010 11:25 PM 17,054 boost_thread-vc80-mt.lib
24/03/2010 11:25 PM 73,728 boost_thread-vc80-mt-gd-1_42.dll
24/03/2010 11:25 PM 17,214 boost_thread-vc80-mt-gd-1_42.lib
24/03/2010 11:25 PM 17,214 boost_thread-vc80-mt-gd.lib
// Static libs... does not need any dlls
24/03/2010 11:25 PM 381,716 libboost_thread-vc80-mt-1_42.lib
24/03/2010 11:25 PM 381,716 libboost_thread-vc80-mt.lib
24/03/2010 11:25 PM 999,552 libboost_thread-vc80-mt-gd-1_42.lib
24/03/2010 11:25 PM 999,552 libboost_thread-vc80-mt-gd.lib
24/03/2010 11:25 PM 421,050 libboost_thread-vc80-mt-s-1_42.lib
24/03/2010 11:25 PM 421,050 libboost_thread-vc80-mt-s.lib
24/03/2010 11:25 PM 1,015,688 libboost_thread-vc80-mt-sgd-1_42.lib
24/03/2010 11:25 …Run Code Online (Sandbox Code Playgroud) 在调试崩溃时,我在一些代码中遇到了这个问题:
int func()
{
char *p1 = malloc(...);
if (p1 == NULL)
goto err_exit;
char *p2 = malloc(...);
if (p2 == NULL)
goto err_exit;
...
err_exit:
free(p2);
free(p1);
return -1;
}
Run Code Online (Sandbox Code Playgroud)
第一个malloc失败时会出现问题.因为我们跳过初始化p2,它包含随机数据和调用可能free(p2)会崩溃.
我希望/希望这将与C++中的方式相同,其中编译器不允许goto跳过初始化.
我的问题:是跳过标准允许的初始化还是这是gcc实现c99的错误?
我一直在教自己作为C++ 0x一部分的智能指针,并且遇到了一些与我不一致的东西.具体来说,如何处理unique_ptr <>和shared_ptr <>的销毁策略.
对于unique_ptr <>,您可以专门化std :: default_delete <>,从那时起,除非您明确请求其他销毁策略,否则将使用新的默认值.
考虑以下:
struct some_c_type;
some_c_type *construct_some_c_type();
void destruct_some_c_type(some_c_type *);
namespace std {
template <> struct default_delete<some_c_type> {
void operator()(some_c_type *ptr) {
destruct_some_c_type(ptr);
}
};
}
Run Code Online (Sandbox Code Playgroud)
现在,一旦到位,unique_ptr <>将默认使用适当的销毁策略:
// Because of the specialization, this will use destruct_some_c_type
std::unique_ptr<some_c_type> var(construct_some_c_type());
Run Code Online (Sandbox Code Playgroud)
现在将其与shared_ptr <>进行比较.使用shared_ptr <>,您需要显式请求相应的销毁策略,或者默认使用operator delete:
// error, will use operator delete
std::shared_ptr<some_c_type> var(construct_some_c_type());
// correct, must explicitly request the destruction policy
std::shared_ptr<some_c_type> var(construct_some_c_type(),
std::default_delete<some_c_type>());
Run Code Online (Sandbox Code Playgroud)
两个问题.
PS我关心这个的原因是我的公司做了很多混合的C和C++编程.C++代码通常需要使用C风格的对象,因此指定不同的默认销毁策略的简便性对我来说非常重要.
标准中"似乎"这个短语的确切含义是什么?当用户可以修改行为的各个部分时,它是如何工作的.
在讨论nothrow版本时,问题是关于C++标准operator new.18.4.1.1/7读(我的重点):
这个nothrow版本的operator new返回一个获得的指针,好像从普通版本获得的一样.
我的理解是,只要行为合适,"似乎"就不需要特定的实现.所以,如果operator new像这样实现(我知道这不是一个兼容的实现,因为没有循环或使用new_handler;但我正在缩短它以专注于我的问题):
// NOTE - not fully compliant - for illustration purposes only.
void *operator new(std::size_t s)
{
void *p = malloc(s);
if (p == 0)
throw std::bad_alloc();
return p;
}
Run Code Online (Sandbox Code Playgroud)
那么写下这样的nothrow版本是合法的:
// NOTE - not fully compliant - for illustration purposes only.
void *operator new(std::size_t s, const std::nothrow_t &nt)
{
return malloc(s);
}
Run Code Online (Sandbox Code Playgroud)
但是,假设程序替换operator new为使用其他分配器."似乎"意味着编译器必须自动更改nothrow版本的行为才能使用其他分配器吗?开发人员是否需要更换普通版本和非版本版本?
使用stl :: vector:
vector<int> v(1);
v[0]=1; // No bounds checking
v.at(0)=1; // Bounds checking
Run Code Online (Sandbox Code Playgroud)
有没有一种方法来禁用边界检查,而不必重写所有at()的[]?我正在使用GNU标准C++库.
编辑:我改变了at()对[]在我怀疑的瓶颈区域,并显著减少了计算时间.但是,由于我在开发代码和运行实验之间进行迭代,我想在开发过程中启用边界检查,并在运行实验时禁用它.我想安德鲁的建议是最好的解决方案.
我知道,从C++ 03标准来看,函数范围静态初始化不保证是线程安全的:
void moo()
{
static std::string cat("argent"); // not thread safe
...
}
Run Code Online (Sandbox Code Playgroud)
最终提供标准线程支持的C++ 0x标准,是否需要线程安全的函数范围静态初始化?
我需要在c ++中创建一个空格字符串,其中空格的数量是一个变量,因此我不能只输入它.如何在不循环的情况下执行此操作?
谢谢!
我正在这里阅读问题和答案几天.我已经看过Parashift中的c ++ faq lite已被多次提及.就个人而言,我一直认为这是一个很好的参考,不是我最喜欢的,但肯定是有用的.在这里,我看到有人提出建议,但许多人反而反对.有人提到它充满了错误.如果有的话,请指出那里的主要错误是哪些?
即时通讯使用keybd_event(); 我想使用SendMessage(); 将按键发送到记事本,可以这样做吗?
这是为char*分配内存的正确方法吗?
char* sides ="5";
char* tempSides;
tempSides = (char*)malloc(strlen(inSides) * sizeof(char));
Run Code Online (Sandbox Code Playgroud)