
c/c++
junlon2006
Coding for fun.
展开
-
ffmpeg av_seek_frame
转载于:https://blog.youkuaiyun.com/xujaiwei/article/details/77651562av_seek_frame使用时需要使用四个参数av_seek_frame(fmt_ctx, -1 , 20 * AV_TIME_BASE, AVSEEK_FLAG_ANY);参数一: fmt_ctx为容器内容;参数二: 流索引, stream_index参数三...转载 2019-04-10 10:09:15 · 1214 阅读 · 1 评论 -
C++成员变量初始化列表执行顺序
#includeclass MyCppExample{public: MyCppExample(int x):b(x),a(b){x=x;}//初始化列表的执行顺序是成员变量申明的顺序 ~MyCppExample(){} void get_a() { cout<<a<<endl; } void get_b() { cout<<b<<endl; }pri原创 2017-02-27 15:14:43 · 408 阅读 · 0 评论 -
C++智能指针
转载于:http://blog.youkuaiyun.com/xt_xiaotian/article/details/5714477一、简介由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete。程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 delete 的情况并不罕见。用智能指针便可以有效缓解这类问题,本文转载 2017-02-15 09:30:20 · 170 阅读 · 0 评论 -
值得推荐的C/C++框架和库
转置:http://ezlippi.com/blog/2014/12/c-open-project.html值得学习的C语言开源项目Libeventlibev是一个开源的事件驱动库,基于epoll,kqueue等OS提供的基础设施。其以高效出名,它可以将IO事件,定时器,和信号统一起来,统一放在事件处理这一套框架下处理。基于Reactor模式,效率较高,并且代码精简(4.1转载 2017-02-23 13:18:05 · 290 阅读 · 0 评论 -
如何确定一个变量是否为有符号型
#define IS_UNSIGNED_VALUE(value) (value>=0&&~value>=0)#define IS_UNSIGNED_TYPE(type) ((type)0 - 1 > 0)原创 2017-01-21 20:43:04 · 315 阅读 · 0 评论 -
C/C++内存模型
转载于:http://c.biancheng.net/cpp/html/2857.html我们知道,C程序开发并编译完成后,要载入内存(主存或内存条)才能运行(请查看:载入内存,让程序运行起来),变量名、函数名都会对应内存中的一块区域。内存中运行着很多程序,我们的程序只占用一部分空间,这部分空间又可以细分为以下的区域:内存分区说明程序代码区(co转载 2017-02-13 19:08:17 · 877 阅读 · 0 评论 -
String构造函数实现
class Cstring {public: Cstring(char *p = NULL); Cstring(const Cstring&); Cstring& operator=(const Cstring&); ~Cstring();private: char *m_data;};Cstring::Cstring(char*p){ i原创 2017-02-13 20:09:51 · 751 阅读 · 0 评论 -
浅析const与指针的组合
char a = 1, b = 2;const char *p = &a;p = &b;//sucess*p = 3;//errorchar a = 1, b = 2;char const *p = &a;p = &a;//sucess*p = 3;//errorchar a = 1, b = 2;char * const p = &a;p = &a;//error*p原创 2016-12-28 10:49:17 · 257 阅读 · 0 评论 -
用加法实现两个整数乘法操作
int get_multi_value(int a,int b){ int res = 0; if(a < b) { std::swap(a, b); } while(b) { if(b&0x1)res+=a; a<<=1; b>>=1; } return res;}int main(){ cout<<get_multi_value(5, 6)<<end原创 2016-12-27 14:06:44 · 1561 阅读 · 0 评论 -
浅谈补码与二进制加法
1、C语言之父,谭浩强老司机曾经教导我们,正数在计算机中的二进制表示,原码、反码、补码都是相同的,等于原码。2、负数在计算机中,以其补码的形式表示。3、以signed short举例,为什么计算机中,它的范围是-32768~32767呢,负数怎么比正数多了一个呢? 我们知道,signed short 16位中,第一位是用于表示符号的,0表示正,1表示负。 比如:原创 2016-12-27 11:18:52 · 2870 阅读 · 1 评论 -
typedef与#define
1、#define宏是纯文本扩展。比如#define a (x) DoSomething(x)则a(y)展开后变成a (x) DoSomething(x)(y)2、typedef是定义类型名,但是它没有扩展性。如typedef int apple;unsigned apple a;//error原创 2017-01-04 10:15:25 · 242 阅读 · 0 评论 -
无锁队列的实现
转载于:http://coolshell.cn/articles/8239.html关于无锁队列的实现,网上有很多文章,虽然本文可能和那些文章有所重复,但是我还是想以我自己的方式把这些文章中的重要的知识点串起来和大家讲一讲这个技术。下面开始正文。关于CAS等原子操作在开始说无锁队列之前,我们需要知道一个很重要的技术就是CAS操作——Compare & Set,或是转载 2017-04-13 23:08:48 · 221 阅读 · 0 评论 -
stupid coding
工作丑陋代码流程,鞭策前行的路:原创 2017-05-05 19:09:23 · 228 阅读 · 0 评论 -
list_head
#ifndef LIST_HEAD_H#define LIST_HEAD_Htypedef struct list_head { struct list_head *next, *prev;} list_head;#define LIST_HEAD_INIT(obj) { &(obj), &(obj) }/* For the entry to be added , INIT原创 2017-06-16 21:17:51 · 334 阅读 · 0 评论 -
滑稽的char打印
切记用unsigned char c = 254;printf("%02x\n", c);原创 2018-04-10 11:53:31 · 965 阅读 · 0 评论 -
C语言运算中的数据类型自动转换原则
转载于:http://blog.youkuaiyun.com/cherish_2012/article/details/212430471、隐式转换 C在以下四种情况下会进行隐式转换: 1、算术运算式中,低类型能够转换为高类型。 2、赋值表达式中,右边表达式的值自动隐式转换为左边变量的类型,并赋值给他。 3、函数调用中参数传递时,系统隐式地将实参转换为形参的类型后,赋...转载 2018-03-20 11:46:15 · 318 阅读 · 0 评论 -
Go语言的工作空间和GOPATH环境变量
转载于:http://www.linuxidc.com/Linux/2015-02/113676.htmGo语言并没有强制一定要使用一定的工作空间和项目结构,对于小型的Go程序依靠Go run等命令就可以直接编译运行。然而,保持良好的工作空间和文件结构,对于管理源代码和发布程序都是非常有帮助的。对于大型的Go语言项目,工作空间则是一定要的。1、Go语言的工作空间结构转载 2017-09-11 17:23:08 · 2130 阅读 · 0 评论 -
CAS锁与MUTEX锁性能测试
C源码:#include #include #include #include #define lock(lkp) do{ \ while(!__sync_bool_compare_and_swap(lkp, 0, 1)){ \ usleep(1000); \ } \}while(0)#define unlock(lkp) do{原创 2017-08-13 20:03:11 · 1062 阅读 · 1 评论 -
CAS锁c源码实现
#define lock(lkp) do{ \ while(!__sync_bool_compare_and_swap(lkp, 0, 1)) \ sched_yield(); \} while(0)#define unlock(lkp) do{ \ *(lkp) = 0; \} while(0)原创 2017-08-13 19:07:49 · 1239 阅读 · 0 评论 -
cJSON array
static result_t __config_parse_dns(char *config_buf){ int i, count; cJSON *root = cJSON_Parse(config_buf); if (NULL == root) { goto L_ERROR; } cJSON *dns_domain = cJSON_Ge原创 2017-07-18 16:05:56 · 1616 阅读 · 0 评论 -
fopen code
static result_t __read_dns_config(const char *file_name, char *output_buf, size_t output_buf_len){ FILE *fp = NULL; size_t flen; LOGD(C_DNS_TAG, "read livepush config, file_name=%s", fil原创 2017-07-18 16:04:42 · 256 阅读 · 0 评论 -
malloc底层实现
转载于:本文大致讲解一下Linux下malloc的底层实现原理。首先malloc肯定是从堆中分配内存,而堆又在用户空间中占据什么位置?通过下面这张图可以看出来:很明显是32位系统,寻址空间是4G,linux系统下0-3G是用户模式,3-4G是内核模式。而在用户模式下又分为代码段、数据段、.bss段、堆、栈。各个segment所含内容在图中有具体说明。其中bss段转载 2017-07-02 09:09:48 · 356 阅读 · 0 评论 -
how to performance better to handle multi-thread initialize
static void __sps_dns_ip_pairs_initialize(){ if (!running) { pthread_mutex_lock(&g_mutex); if (!running) { list_init(&g_dns_head); __sps_dns_launch();原创 2017-06-28 16:31:52 · 251 阅读 · 0 评论 -
stupid char[0] malloc code
原创 2017-05-21 21:48:09 · 223 阅读 · 0 评论 -
Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.点击打开链接int getSum(int a, int b) { int sum = a;原创 2017-01-03 16:24:33 · 234 阅读 · 0 评论 -
char *(*c[10])(int **p);
char *(*c[10])(int **p);1、c[10]是一个数组。2、*c[10]是一个指针数组。3、(*c[10])(int **p)是一个函数指针的数组,函数有一个int **p形参。4、char *(*c[10])(int **p);c是一个数组,该数组用于存放一个函数指针,该函数,有一个int**的形参,返回char*类型。char *(*c[10])(int原创 2017-01-03 16:16:53 · 2230 阅读 · 0 评论 -
char * const *(*next)();
char * const *(*next)();1、(*next)是一个指针。2、(*next)()是一个函数指针。3、 *(*next)();是一个返回指针的函数指针。4、char * const 是一个常字符串指针,即一个常量指针,该指针指向一个字符串。5、char * const *(*next)();next是一个函数指针,该函数形参列表为空返回一个指针,返回的指针原创 2017-01-03 15:49:55 · 1360 阅读 · 1 评论 -
C++类的虚函数表
#include using namespace std;typedef void(*Fun)();class CExample{private: void virtual vir_function1(){cout<<"this is virtual fuction1"<<endl;} void virtual vir_function2(){cout<<"this is virt原创 2016-11-10 17:19:53 · 338 阅读 · 0 评论 -
C++类的virtual继承关系
class X{public: void getX() { cout<<"getX"<<endl; } int i,j,k,m;};class Y: virtual public X{public: void getY() { cout<<"getY"<<endl; }};class Z:virtual public X{public: void原创 2016-11-10 15:25:08 · 734 阅读 · 0 评论 -
C++派生类改变基类的成员访问类型
class Books{public : void getName();};class CodeBook:public Books{private: using Books::getName;};int main(){ CodeBook cpp; cpp.getName();//compile error. return 0;原创 2016-11-10 13:28:37 · 499 阅读 · 0 评论 -
几种常见的排序算法(C++版)
1、冒泡法#include void bubble_sort(int a[], int n){ int loop_times = 0; int changes = 0; for(int i = 0; i < n - 1; i++) { for(int j = 0; j < n - i -1; j++) { if(a[j] > a[j+1]) { swa原创 2016-11-09 13:38:21 · 334 阅读 · 0 评论 -
C++类sizeof计算规则
class CExample{public: CExample(){} //不占用sizeof大小 ~CExample(){}//不占用sizeof大小 void commonFun(){} //不占用sizeof大小 virtual void virtualFun(){}//存在虚函数,生成virtual table,故占用一个指针空间private: static int m_原创 2016-11-08 20:46:00 · 487 阅读 · 0 评论 -
C++类const成员函数
class Book{public: Book(double price) { this->price = price; } void SetPrice(double price) { this->price = price; } double GetPrice()const { this->price = this->price + 1;//error thi原创 2016-11-08 20:23:04 · 245 阅读 · 0 评论 -
C++浅拷贝和深拷贝
#include using namespace std;class CExample{public: CExample(int a = 0, int b = 0) { this->m_a = a; this->m_b = b; this->m_char = new char[10]; } CExample(CExample& other) { this->m_原创 2016-11-08 13:45:36 · 260 阅读 · 0 评论 -
C++类的构造函数可以private吗
#include using namespace std;class CExample{public: CExample(int b, int c = 0):m_a(1) //带默认参数的,必须放在尾巴上,否则编译失败,如(int c = 0, int b)编译报错 { m_b = b; m_c = c; } CExample(const CExample&c):m_原创 2016-11-08 17:29:32 · 1524 阅读 · 0 评论 -
C++构造函数执行过程
#include using namespace std;class CExample{public: CExample() { cout<<"普通构造函数"<<this<<endl; } CExample(const CExample&c) { cout<<"拷贝构造函数"<<this<<endl; } ~CExample() { cout<<"析构函数"原创 2016-11-08 10:16:48 · 473 阅读 · 0 评论 -
C++虚析构函数总结
#include using namespace std;class Base{public: ~Base() { cout<<"~Base"<<endl; }};class Derived :public Base{public: Derived() { m_char = new char[10]; } ~Derived() { delete[原创 2016-11-07 19:54:12 · 306 阅读 · 0 评论 -
C++运算符重载
#include using namespace std;class Base{public: Base() { x=y=0; cout<<"Base()"<<this<<endl; } Base(int x, int y) { this->x = x; this->y = y; cout<<"Base(int x, int y)"<<this<<endl;原创 2016-11-11 15:37:14 · 199 阅读 · 0 评论 -
函数返回const引用和普通引用的区别
#include using namespace std;int&fun(int&n){ return n;}const int fun1(int&n){ return n;}int main(){ int n = 10; fun(n) = 5 ; cout<<n<<endl; fun1(n); fun1(n) = 10;//conpile error.原创 2016-11-13 15:05:07 · 1535 阅读 · 0 评论 -
C++类const成员变量初始化
class CExample{public: CExample():m_a(1),m_b(2){/*m_a = 1; compile error*/} CExample(const CExample&c):m_a(1){/*m_a = 1; compile error*/} ~CExample(){}private: const int m_a; int m_b;};原创 2016-11-08 16:37:23 · 12954 阅读 · 2 评论