cout

C++ Segmentation Fault when using cout in static variable initialization

﹥>﹥吖頭↗ 提交于 2020-04-09 06:44:45
问题 I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes

STL(标准模板库)

此生再无相见时 提交于 2020-04-07 00:41:14
STL 主要分为三类: container(容器)  -  用来管理一组数据元素 lterator(迭代器)  -  可遍历STL容器内全部或部分元素的对象 algorithm(算法)  -  对数据进行处理(解决问题)步骤的有限集合。 容器和算法通过迭代器可以进行无缝连接,在STL中几乎所有的代码都采用了模板类和模板函数的方式,这相比于传统的由函数和类组成库来说提供了更好的代码重用机会。 STL最早源于惠普收益延时,早于C++存在,但是C++引入STL概念后,STL就成为C++的一部分,因为它被内建在你的编译器之内,不需要另行安装。 STL被组织为下面的13个头文件: <algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、<memory>、<numeric>、<queue>、<set>、<stack>、<utility> 下面代码简单说明STL中的一些功能: 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main() 8 { 9 //第一部分:容器 vector 10 vector<int>num; 11 12 num.push_back(1); /

c++ boost Exector

女生的网名这么多〃 提交于 2020-04-05 17:39:34
前言 在应用开发中经常要执行一些异步的函数,有些是没有返回结果,有些是有返回结果,甚至有些是定时的任务,本文在boost io_service基础上搭建一个Exector来执行这些任务 代码 #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> #include <boost/thread.hpp> #include <boost/bind.hpp> #include <boost/asio.hpp> #include <iostream> #include <unistd.h> #include <boost/function.hpp> #include <vector> class Executor { public : Executor ( int count) { _io_service = boost ::make_shared< boost :: asio :: io_service >(); _work = boost ::make_shared< boost :: asio :: io_service :: work >( * _io_service ); for ( int i = 0 ; i < count; ++i) { boost :: shared_ptr < boost ::

boost lockfree高性能无锁队列

偶尔善良 提交于 2020-04-05 17:01:51
#include <boost/thread/thread.hpp> #include <boost/lockfree/queue.hpp> #include <iostream> #include <glog/logging.h> #include <boost/atomic.hpp> boost::atomic_int producer_count( 0 ); boost::atomic_int consumer_count( 0 ); boost::lockfree::queue< int > queue( 128 ); const int iterations = 1 ; const int producer_thread_count = 4 ; const int consumer_thread_count = 4 ; void producer( void ) { for ( int i = 0 ; i != iterations; ++i) { int value = ++producer_count; sleep( 3 ); while (!queue.push(value)); } } boost::atomic< bool > done( false ); void consumer( void ) { int value; while (!done) {

File Tamper 2.0

▼魔方 西西 提交于 2020-04-04 00:51:36
main.cpp 1 #include <iostream> 2 #include <fstream> 3 #include <stdlib.h> 4 #include <windows.h> 5 6 using namespace std; 7 8 int main ( int argc, char* argv[] ) { 9 if ( argc != 4 ) { 10 cout << "Usage: ft <File> <Off> <Data>" << endl; 11 return -1; 12 } 13 fstream file ( argv[1], ios::in | ios::out | ios::binary | ios::ate ); 14 if ( file.is_open() ) { 15 long long fileSize = static_cast<long long> ( file.tellg() ); 16 cout << "Size: " << fileSize << " bytes" << endl; 17 long int yPos = strtol ( argv[2], NULL, 16 ); 18 long int yData = strtol ( argv[3], NULL, 16 ); 19 unsigned char sData =

File Split 1.0

跟風遠走 提交于 2020-04-02 15:17:28
main.cpp 1 #include <iostream> 2 #include <fstream> 3 #include <stdlib.h> 4 #include <string> 5 #include <windows.h> 6 7 using namespace std; 8 9 int main ( int argc, char* argv[] ) { 10 if ( argc != 4 && argc != 5 ) { 11 cout << "File Split 1.0" << endl; 12 cout << " Usage: fs <source file> <off begin> <off end> [<destination file>]" << endl; 13 cout << " e.g. fs app.exe 0 127 0_127.dat" << endl; 14 cout << " e.g. fs app.exe 0 127" << endl; 15 return -1; 16 } 17 fstream src ( argv[1], ios::in | ios::binary | ios::ate ); 18 if ( src.is_open() ) { 19 long long srcSize = static_cast<long long> (

sync_with_stdio和cin.tie(0); cout.tie(0);

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-31 03:43:25
sync_with_stdio 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。 应用 在ACM里,经常出现数据集超大造成 cin TLE的情况。这时候大部分人(包括原来我也是)认为这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论。其实像上文所说,这只是C++为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时混用cout和printf之类。 在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 原来而cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间,使效率与scanf与printf相差无几. 参考来源: 来源: https://www.cnblogs.com/xwh-blogs/p/12602671.html

C++ 字符流 stringstream

折月煮酒 提交于 2020-03-30 12:31:32
(转自: https://blog.csdn.net/nwpu_yike/article/details/22100615 ) 一、类型转换——数字->字符串 C++ stringstream 类是一种十分有用的类,特别是当我们需要在程序中使用字符串和数字数据互相转换的时候。要想在程序中使用 stringstream 类,我们需要在源程序文件中包含头文件include<sstream>。stringstream 对象的使用方法与cout对象的使用方法基本相同。 当我们需要按预定的格式将程序中的数据保存在一个string 中的时候,可以先创建一个stringstream 对象,并通过运算符 ”<<“ 将数据传递给 stringstream 对象。(这与通过”<<“ 使用cout 对象的方法相同。)接着,我们可以通过调用stringstream 类的函数str() 将对象所包含的内容赋给一个string对象。在下面的程序中,我们先将数据传递给一个stringstream 对象,然后通过该 stringstream 对象将数值赋给一个string 对象。注:cout能使用的所有ios格式标记也可以在stringstream 对象中使用。 #include <iostream> #include <sstream> using namespace std; int main(void) {

8.异常处理(未完)

[亡魂溺海] 提交于 2020-03-29 17:11:56
8.1.1栈展开 抛出异常时,将暂停当前函数的执行,开始查找匹配的catch子句。首先检查throw本身是否在try块内部,如果是,检查与该try相关的catch子句,看是否可以处理该异常。如果不能处理,就退出当前函数,并且释放当前函数的内存并销毁局部对象,继续到上层的调用函数中查找,直到找到一个可以处理该异常的catch。这个过程称为栈展开(stack unwinding)。当处理该异常的catch结束之后,紧接着该catch之后的点继续执行。当找不到匹配的catch时,程序将调用标准库函数terminate,负责终止程序的执行过程。 8.1.2析构函数能抛出异常吗? 不能, (1) 如果析构函数抛出异常,则异常点之后的程序不会执行,如果析构函数在异常点之后执行了某些必要的动作比如释放某些资源,则这些动作不会执行,会造成诸如资源泄漏的问题。 (2) 通常异常发生时,c++的机制会调用已经构造对象的析构函数来释放资源,此时若析构函数本身也抛出异常,则前一个异常尚未处理,又有新的异常,会造成程序崩溃的问题。 8.1.3异常对象和查找匹配的处理代码 异常对象使用异常抛出表达式来对异常对象进行拷贝初始化,因此throw语句中的表达式必须拥有完全类型,如果是类类型还必须拥有一个可访问的析构函数和拷贝或者移动构造函数。 异常的类型混合catch声明的类型匹配: (1

数据结构,算法及线性表总结

て烟熏妆下的殇ゞ 提交于 2020-03-28 20:44:20
1.思维导图 2.重要概念笔记 1.数据结构 1.数据结构定义 -我们如何把现实中大量而复杂的问题以特定的数据类型和特定的存储结构保存到主存储器(内存)中,以及在此基础上为实现某个功能(比如查找某个元素,删除某个元素,对元素进行排序等)而执行的相应操作,这个相应的操作也叫算法。 2.算法 -衡量算法的标准: -时间复杂度:程序大概要执行的次数,而非执行的时间 -空间复杂度:程序执行过程中大概所占用的最大内存空间 -难易程度:用易懂,避免过于复杂 -健壮性 3.连续存储【数组】 -什么叫数组:元素类型相同,大小相等 -数组的优缺点 --优点:存取速度很快 --缺点:插入删除元素很慢 4.离散结构【链表】 -定义:n个节点离散分配,彼此通过指针相连,每个节点只有一个前驱节点同时每个节点只有一个后续节点,首节点没有前驱节点,尾节点没有后续节点 -专业术语 --首节点:存放第一个有效数据的节点 --尾节点:存放最后一个有效数据的节点 --头结点:位于首节点之前的一个节点,头结点并不存放有效的数据,加头结点的目的主要是为了方便对链表的操作 --头指针:指向头结点的指针变量 --尾指针:指向尾节点的指针变量 -确定一个链表需要几个参数:只需要一个头指针参数,因为我们通过头指针可以推算出链表的其他所有信息 -分类: --单链表:每一个节点只有一个指针域 --双链表:每一个节点有两个指针域 -