小编Jon*_*ely的帖子

为什么glibc和pthread库都定义了相同的API?

为什么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)

linux gcc glibc pthreads

12
推荐指数
1
解决办法
6454
查看次数

不要打印尾随分隔符stream_iterator C++

在您看来,在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)

c++ iterator ostream c++11

11
推荐指数
1
解决办法
1356
查看次数

访问unordered_map时的SIGFPE

我有一个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)

c++ gcc clang libstdc++

10
推荐指数
2
解决办法
3583
查看次数

将忽略std :: vector <char>的自定义分配器

我试图使用自定义分配器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. …

c++ allocator c++11

10
推荐指数
2
解决办法
1576
查看次数

可以线程尝试std :: lock [_unique] std :: shared_mutex被调用std :: lock_shared的线程饿死吗?

关于std::shared_mutex和获得的问题unique_lock.

假设有3个主题:

  • 2名读者(努力lock_shared()std::shared_mutex),和
  • 1名作家(努力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)

c++ multithreading mutex c++11 c++14

10
推荐指数
1
解决办法
657
查看次数

g ++:使用ZIP文件作为输入

我们这边有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++ boost annotations g++

9
推荐指数
2
解决办法
768
查看次数

为什么我的程序在堆栈上限为10MB时分配11MB字符串数组时不会溢出堆栈?

我有两个简单的C++程序和两个问题.我在CentOS 5.2工作,我的开发环境如下:

  • g ++(GCC)4.1.2 20080704(Red Hat 4.1.2-50)
  • "ulimit -s"输出:10240(千字节),即10MB

计划#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)

c linux stack-overflow callstack

8
推荐指数
2
解决办法
582
查看次数

C++中的异常处理:使用"throw(int)"时抛出一个double

以下程序始终输出"错误:双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)

c++ double int try-catch

8
推荐指数
1
解决办法
1736
查看次数

在派生类中使用基类Copy CTOR

我知道这个主题有很多帖子,但我找不到任何完全回答我的问题.

假设我有一个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)

现在我对这个设计有一些疑问.

  1. 这是适合这种情况的合适设计吗?
  2. 如果我有一个第三类,在基类和派生类之间,但它只包含原始类型,我在哪里复制它们?EG使用第二类的默认赋值运算符?建立一个新的?只在第三级复制它们?
  3. 我可以类似地在派生类CCtor中调用基类CCtor,而不是赋值运算符.有什么不同?如果我将它们放在两种方法中,它会尝试将值复制两次吗?

编辑:只是为了澄清,设计是我在项目中给出的.我有指针所以我必须使用深层复制.

c++ oop

8
推荐指数
1
解决办法
152
查看次数

未明确引用运算符>>

我正在尝试运算符重载,我的头文件包括:

#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)

c++ undefined-reference

7
推荐指数
1
解决办法
7675
查看次数