为什么glibc和pthread库都定义了相同的API?这是快照
ubuntu@ubuntu:/lib$ objdump -T /lib/i386-linux-gnu/libc.so.6 |grep pthread_cond_signal
000f8360 g DF .text 00000039 GLIBC_2.3.2 pthread_cond_signal
0012b940 g DF .text 00000039 (GLIBC_2.0) pthread_cond_signal
ubuntu@ubuntu:/lib$ objdump -T /lib/i386-linux-gnu/libpthread.so.0 |grep pthread_cond_signal
0000b350 g DF .text 0000007c (GLIBC_2.0) pthread_cond_signal
0000af90 g DF .text 000000fc GLIBC_2.3.2 pthread_cond_signal
Run Code Online (Sandbox Code Playgroud) 在您看来,在C++ 11中打印std::cout使用std::ostream_iterator并避免打印尾随分隔符时,最优雅的方法是什么?
我正在打印的对象具有双向迭代器,但不是随机访问迭代器.
std::list<double> x{1,2,3,4,5,6};
std::copy(x.begin(), std::prev(x.end()),
std::ostream_iterator<int>(std::cout, ",") );
if ( x.size() != 0 )
std::cout << *(--x.end()) << std::endl;
Run Code Online (Sandbox Code Playgroud) 我有一个unordered_map<Block, int>块是一个简单的结构定义如下:
struct Block {
size_t start;
size_t end;
bool operator==(const Block& b) const {
return start == b.start && end == b.end;
}
};
namespace std {
template<>
struct hash<Block> {
size_t operator()(const Block& b) const {
return b.start;
}
};
}
Run Code Online (Sandbox Code Playgroud)
当试图访问地图时,我确实在gdb中收到以下错误消息(对于g ++ 4.7.1以及clang ++ 3.1都是如此):
Program received signal SIGFPE, Arithmetic exception.
0x0000000000401e0b in std::__detail::_Mod_range_hashing::operator() (this=0x7fffffffd8e0, __num=0, __den=0)
at /usr/include/c++/4.7/bits/hashtable_policy.h:245
245 { return __num % __den; }
Run Code Online (Sandbox Code Playgroud)
我的libstdc ++版本是3.4.17(即GCC 4.7的版本)
相关回溯:
#0 0x0000000000401e0b in std::__detail::_Mod_range_hashing::operator() (this=0x7fffffffd8e0, …Run Code Online (Sandbox Code Playgroud) 我试图使用自定义分配器std::vector<char>,但我注意到std::vector不需要/使用我的分配器中的任何成员函数.这怎么可能?
#include <vector>
struct A : private std::allocator<char> {
typedef std::allocator<char> alloc;
using alloc::value_type;
using alloc::pointer;
using alloc::const_pointer;
using alloc::difference_type;
using alloc::size_type;
using alloc::rebind;
// member functions have been removed, since the program compiles without them
};
int main() {
std::vector<char, A> v;
v.resize(4000);
for (auto& c : v)
if (c)
return 1; // never happens in my environment
return 0; // all elements initialized to 0. How is this possible?
}
Run Code Online (Sandbox Code Playgroud)
我正在使用在线C++ 11编译器(LiveWorkSpace)尝试上述程序,提供g ++ 4.7.2,4.8和4.6.3. …
关于std::shared_mutex和获得的问题unique_lock.
假设有3个主题:
lock_shared()的std::shared_mutex),和lock[_unique]()的std::shared_mutex)这位作家是否有可能lock[_unique]()被迫饿死?例如:在任何时候至少有一个读者拥有一个std::shared_lock,并且lock[_unique]()永远不会成功.
更多或更少:将lock[_unique]()一对std::shared_mutex块中的任何企图进一步lock_shared()呢?
(相当肯定boost::upgrade_lock可以在这里工作,但我想知道是否有任何保证裸露std::unique_locka std::shared_mutex)
我们这边有Boost库.它由大量文件组成,这些文件永远不会改变,只使用它的一小部分.如果我们要更改版本,我们会交换整个boost目录.目前我们的SVN中有Boost源,逐个文件,这使得结账操作非常慢,特别是在Windows上.
如果有一个符号/插件来解决ZIP文件中的C++文件会很好,例如:
// @ZIPFS ASSIGN 'boost' 'boost.zip/boost'
#include <boost/smart_ptr/shared_ptr.hpp>
Run Code Online (Sandbox Code Playgroud)
在g ++中是否支持编译器挂钩?有关ZIP支持的任何努力吗?其他想法?
我有两个简单的C++程序和两个问题.我在CentOS 5.2工作,我的开发环境如下:
计划#1:
main.cpp中:
int main(int argc, char * argv[])
{
char buf[1024*1024*11] = {0};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(用"g ++ -g main.cpp"编译)
程序在堆栈上分配1024*1024*11字节(即11MB),但不会崩溃.将分配大小更改为1024*1024*12(即12MB)后,程序崩溃.我认为这应该是由堆栈溢出引起的. 但是为什么程序在分配大小为11MB时不会崩溃,这也大于10MB的上限?
计划#2:
main.cpp中:
#include <iostream>
int main(int argc, char * argv[])
{
char buf[1024*1024*11] = {0};
std::cout << "*** separation ***" << std::endl;
char buf2[1024*1024] = {0};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(用"g ++ -g main.cpp"编译)
该程序将导致程序崩溃,因为它在堆栈上分配12MB字节.但是,根据核心转储文件(见下文),崩溃发生在buf但不是buf2. 不应该崩溃发生在buf2上,因为我们从程序#1知道char buf [1024*1024*11]的分配是正常的,因此在我们分配另一个1024*1024字节后,堆栈会溢出吗?
我认为必须有一些非常基本的概念,我没有建立一个坚实的理解.但是他们是什么?
附录:程序#2生成的核心转储信息:
Core was generated by `./a.out'.
Program terminated with signal …Run Code Online (Sandbox Code Playgroud) 以下程序始终输出"错误:双10.2".
我不懂为什么.根据我的说法,如果fun1()只允许抛出int,程序应该(1)崩溃(2)或将double更改为int然后抛出.这意味着,输出应为"Error:int 10".然而,事实并非如此.谁能解释一下?
void fun1() throw (int)
{
cout<<"3";
throw 10.2;
cout<<"4";
}
int main()
{
try { fun1(); }
catch(int i) { cout<<"Error:int" <<i <<endl;}
catch(double i) { cout << "Error:double" << i << endl; }
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道这个主题有很多帖子,但我找不到任何完全回答我的问题.
假设我有一个Base类和一个Derived类,我为它实现了CCtor和赋值运算符,如下所示:
class Base {
char * name;
....
Base(const Base& other) :name(nullptr) { *this = other }
void operator=(const Base& other) { ... Deep copy of name }
}
class Derived : public Base {
....
Derived(const Derived& other) { *this = other ; }
void operator=(const Derived& other) {
Base::operator=(other);
.....
}
Run Code Online (Sandbox Code Playgroud)
现在我对这个设计有一些疑问.
编辑:只是为了澄清,设计是我在项目中给出的.我有指针所以我必须使用深层复制.
我正在尝试运算符重载,我的头文件包括:
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include<iostream>
#include<string>
using namespace std;
class Phonenumber
{
friend ostream &operator << ( ostream&, const Phonenumber & );
friend istream &operator >> ( istream&, Phonenumber & );
private:
string areaCode;
string exchange;
string line;
};
#endif // PHONENUMBER_H
Run Code Online (Sandbox Code Playgroud)
和类的定义
//overload stream insertion and extraction operators
//for class Phonenumber
#include <iomanip>
#include "Phonenumber.h"
using namespace std;
//overloades stram insertion operator cannot be a member function
// if we would like to invoke it with
//cout<<somePhonenumber
ostream …Run Code Online (Sandbox Code Playgroud)