vearne的代码世界
vearne · 共 15 篇文章
将博客搬至CSDN
此博客即将废弃,所有内容将迁往CSDN 以下是新地址 http://blog.csdn.net/woshiaotian 博客有了新站 http://vearne.cc/
2012-12-22 21:35 · 评论(0)
linux线程同步--条件变量练习(2)
注意:消息是由主线程产生的,而消息这时候在堆中,两个线程通过全局变量获取访问消息。 #include <pthread.h> #include <stdlib.h> #include <stdio.h> struct msg { int data; struct msg *m_next; /* ... more stuff here ... */ }; struct msg *workq; pthread_cond_t qready = PTHREAD_COND_INITIALIZER; pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; v…
2012-08-07 18:00 · linux · 评论(0)
linux线程同步--条件变量练习(1)
注意:消息是由主线程产生的,而消息这时候在栈中,两个线程通过全局变量获取访问消息。 Unix环境高级编程P288 进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本、程序的全局变量和堆内存、栈以及文件描述符。 #include <pthread.h> #include <stdio.h> struct msg { int data; struct msg *m_next; /* ... more stuff here ... */ }; struct msg *workq; pthread_cond_t qready = PTHREAD_COND_INITIALIZER; pt…
2012-08-06 23:27 · linux · 评论(0)
使用linux函数模拟HTTP 请求过程
非常好的参考资料 1.HTTP 协议资料 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2 2.使用telnet模拟浏览器访问网页 http://pcwanli.blog.163.com/blog/static/4531561120091115105240732/ #include "apue.h" #include <netdb.h> #include <errno.h> #include <sys/socket.h> #include <iostream> using namespace std; #define…
重复删除指针
#include <iostream> using namespace std; class Base{ public: void f(){ cout<<"base"<<endl; } private: int a; }; int main(){ Base* b = new Base; delete b; delete b; return 0; } 经过试验表明重复删除会导致运行时报错,但如果加上b = NULL;第二次delete操作将不会执行,程序可以正常执行。
2012-07-27 22:10 · C/C++ · 评论(0)
继承关系中的早绑定
#include <iostream> using namespace std; class Base{ public: void f(){ cout<<"base"<<endl; } }; class Derived:public Base{ public: void f(){ cout<<"derived"<<endl; } }; int main(){ Base b; Derived d; b = d; b.f(); d.f(); return 0; } 如果不使用虚函数,则使用编译时的类型以决定调用基类的函数,还是派生类的函数。
2012-07-27 09:00 · C/C++ · 评论(0)
关于僵尸进程的练习。
请参考linux c一站式学习,此节内容 http://learn.akae.cn/media/ch30s03.html#id2867242 可以设置父进程忽略SIGCHLD信号,或者在SIGCHLD信号的处理函数中调用wait函数,即可获取子进程的退出状态,且销毁僵尸进程。 1)调用wait函数 #include <unistd.h> #include <stdlib.h> #include <iostream> #include <signal.h> #include <sys/wait.h> using namespace std; void sig_deal(int signo){ …
2012-07-19 10:17 · linux · 评论(0)
has virtual functions but non-virtual destructor
#include <iostream> using namespace std; class based{ public: based(){ } ~based(){ cout<<"flag 1"<<endl; } virtual void f(){ cout<<"ok1"<<endl; } }; class derived:public based{ public: ~derived(){ cout<<"flag 2"<<endl; } void f(){ cout<<"ok2"<<endl; } }; int main(){ based* p = new derived(); p->f()…
2012-07-14 13:36 · C/C++ · 评论(0)
二叉树的中序遍历
#include <iostream> #include <stack> using namespace std; struct node{ int data; struct node* lchild; struct node* rchild; }; typedef struct node Node; void inOrderTraverse(Node* root){ Node* p = root; stack<Node*> s; while(p||!s.empty()){ if(p){ s.push(p); p = p->lchild; }else{ p = s.top(); s.pop(…
2012-07-13 10:04 · C/C++ · 评论(0)
两个有序数组求中位数的O(logn)算法
#include <iostream> using namespace std; int get_median(int a[],int begin1,int end1,int b[],int begin2,int end2,int k){ if(begin1>end1){ //在数组A中无法找到 return get_median(b,begin2,end2,a,begin1,end1,k); } int t = (begin1+end1)/2; if(a[t]>=b[k-t-1]&&a[t]<=b[k-t]){//got it return a[t]; }else if(a[t]<b[k-…
2012-07-13 00:16 · C/C++ · 评论(0)
智能指针的简易实现
#include <iostream> using namespace std; class smart_count{ private: int use_count; public: smart_count(int c=0):use_count(c){ } ~smart_count(){} int addref(){ return ++use_count; } int release(){ return --use_count; } }; template<class T> class smart_ptr{ private: T* p; smart_count* u; public: sma…
2012-07-12 16:26 · C/C++ · 评论(0)
C++中把多维数组传递给函数可使用模版的办法
#include <iostream> using namespace std; template<int m,int n> void print(int a[m][n]){ for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ cout<<a[i][j]<<" "; } } cout<<endl; } int main(){ int a[][3]={{1,2,3},{4,5,6}}; print<2,3>(a); }
2012-07-11 23:30 · C/C++ · 评论(0)
图的广度深度遍历(邻接矩阵)
#include <iostream> #include <queue> #include <algorithm> #include <stack> using namespace std; int a[5][5]={{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,0,1,0,0},{1,0,1,0,0}}; template<int n> void bfs(int i){ queue<int> q; bool flag[n]; memset(flag,false,n); q.push(i); flag[i]=true; while(!q.empty()){ i…
2012-07-11 23:25 · C/C++ · 评论(0)
堆排序代码实现
void heap_adjust(int a[],int i,int size){ int temp; int j = i*2 +1; if(i<=size/2-1){ //保证其为非叶子节点----这里请注意 if(j+1<size&&a[j]<a[j+1]){ //找出子节点中值最大的 j++; } if(a[i]<a[j]){ temp = a[i]; a[i] = a[j]; a[j] = temp; heap_adjust(a,j,size); } } } void make_heap(int a[],int size){ //i = (size -2)/2 但是这样写的话,对于s…
2012-07-01 23:37 · C/C++ · 评论(0)
java中两种有趣的匿名类实现
java中的匿名类除了还有两种有趣的用法 /** * TODO Comment of AnonymousTest * * @author aotian.zhuw */ class EntryInterface { public void print() { System.out.println("hello world"); } } public class AnonymousTest { private EntryInterface a; public void setA(EntryInterface a) { this.a = a; } public void print() { a.p…
2011-10-30 14:52 · java · 评论(0)