我在C++的应用程序中使用boost :: shared_ptr.内存问题非常严重,应用程序占用大量内存.
但是,因为我将每个新建对象放入shared_ptr,当应用程序退出时,不会检测到内存泄漏.
必须有一些像std::vector<shared_ptr<> >池持有资源的东西.调试时如何知道谁拥有shared_ptr?
很难逐行检查代码.代码太多......
非常感谢!
在C采访中,我被要求将最后4位的数字的前4位交换掉.(例如,1011 1110应为1110 1011.)
有人有解决方案吗?
浏览C++ 0x书中的货币并认为我会给出示例代码.它是基本的.
#include <iostream>
#include <thread>
void hello()
{
std::cout<<"Hello Concurrent World\n";
}
int main(int argc, char *argv[])
{
std::thread t(hello);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
编译:
g++ -std=c++0x -g -o pgm pgm.cpp
Run Code Online (Sandbox Code Playgroud)
热潮:
Program received signal SIGSEGV, Segmentation fault.
_dl_fixup (l=0x7ffff7b0992c, reloc_arg=<value optimized out>) at ../elf/dl-runtime.c:147
147 ../elf/dl-runtime.c: No such file or directory.
in ../elf/dl-runtime.c
Run Code Online (Sandbox Code Playgroud)
似乎是某种设置/库问题.有人熟悉这个吗?
我正在尝试元组并遇到创建元组的问题.代码示例如下.
//a.cpp
#include <tuple>
using namespace std;
int main() {
auto te = make_tuple(); //this line is ok
auto tte = make_tuple(te); //this line gives an error.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用g ++ 4.5(g ++ -std = c ++ 0x a.cpp)和MS VC++ 2010编译它.两个编译器都在main()的第二行给出了一个错误.
我的问题是:由于'te'是一个定义明确的变量,为什么不能用te作为内容来创建另一个元组.这个语义是否正确?
我想这是一种边界情况,但如果算法是正确的,那么应该允许零,恕我直言.
仅供参考,来自gcc的错误消息是:
$ gcc -std=c++0x a.cpp
In file included from a.cpp:1:0:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple: In constructor
'std::tuple<_Elements>::tuple(std::tuple<_UElements ...>&) [with _UElements = {},
_Elements = {std::tuple<>}]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple:551:62: instantiated from
'std::tuple<typename std::__decay_and_strip<_Elements>::__type ...>
std::make_tuple(_Elements&& ...) [with _Elements = {std::tuple<>&}, typename
std::__decay_and_strip<_Elements>::__type …Run Code Online (Sandbox Code Playgroud) 关于C++右值引用的Artima文章(http://www.artima.com/cppsource/rvalue.html)有一些词:这就是为什么在传递给基类时有必要说move(x)而不是x .这是移动语义的一个关键安全功能,旨在防止意外地从某个命名变量移动两次.
当这样的双重动作可以执行时,我无法想到情况.你能举个例子吗?换句话说,如果所有成员T&&都是rvalue引用而不仅仅是引用会出什么问题?
我知道我可以使用语法为矢量设置自定义分配器vector<T, Alloc>.有没有办法我可以为字符串做同样的事情?
我试图在循环中使用迭代器在字符串上执行if语句,但无法弄清楚如何获取if语句的当前字符:
for (std::string::iterator i=buffer.end()-1; i>=buffer.begin(); --i) {
if (!isalpha(*i) && !isdigit(*i)) {
if(i != "-") { // obviously this is wrong
buffer.erase(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我获取当前字符,以便我可以做一些额外的if语句吗?
我们所知道的std::advance是以下内容:
template <class InputIterator, class Distance>
void advance (InputIterator& i, Distance n);
Run Code Online (Sandbox Code Playgroud)
目的
i按n元素推进迭代器.
如果i是随机访问迭代器,函数使用一次operator+或operator-,否则,该函数使用重复的增加或减少经营者(operator++或operator--),直到n元件已经前进.
我的问题如下:如何std::advance实现它识别是否it是随机访问迭代器?怎么知道它可以operator+代替operator++?
请参阅下面的main()和两个非常简单的类.然后根据Boost序列化(以及显示的内容),我的问题是:
1)B类是否需要定义正常的重载流插入运算符'<<'和'>>'?目前在我的真实代码中它没有这些.
2)store()和load()方法中的类A是否需要明确地迭代map和multimap容器,明确地存储/加载它们的key:value对?例如:
void A::store(const char* filename){
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
std::map< std::string, B >::iterator it;
BMap.size();
oa << BMap.size();
for( it = BMap.begin(); it != BMap.end(); it++ ){
oa << it->first;
oa << it->second;
}
//similar for strMultimap
}
Run Code Online (Sandbox Code Playgroud)
我认为我不需要这样做,但我不确定.
3)假设B类只显示了两个数据成员,是否需要明确包含默认的构造函数?(与隐式默认构造函数相对)
4)B是否需要为比较运算符'>'覆盖?我认为它不是因为这是一个非常简单的类.
最后,对于我未能涵盖的任何内容的任何其他评论表示赞赏!
上述问题的示例代码:
//includes ommitted
int main() {
std::string file("test.dat");
A * pA = new A;
pA->store(file.c_str());
pA->fillMaps();
//release data
pA->load(file.c_str());
return 0;
}
//includes ommitted
class A
{
friend class boost::serialization::access;
public:
std::map< std::string, B > BMap; …Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×3
boost ×2
linux ×2
string ×2
allocator ×1
c ×1
iterator ×1
map ×1
memory-leaks ×1
shared-ptr ×1
sorting ×1
std ×1
stdadvance ×1
tuples ×1