2012年12月11日
摘要: delete[]中的[]是要告诉c++,指向的是一个数组,如果在[]中填上了数组的长度信息,c++编译系统将忽略,并把它作为[]对待。但如果忘记了写[],则程序会产生运行错误。 delete只会调用一次析构函数,而delete[]会调用每一个成员的析构函数。在More Effective C++中... 阅读全文
posted @ 2012-12-11 22:32 猿人谷 阅读(361) 评论(0) 推荐(0)
摘要: 当一个对象的值可能会在编译器的控制或监测之外被改变时,例如一个被系统时钟更新的变量,那么该对象应该声明成volatile。因此编译器执行的某些例行优化行为不能应用在已指定为volatile的对象上。volatile修饰符的主要目的是提示编译器该对象的值可能在编译器未监测到的情况下被改变,因此编译器不能武断地对引用这些对象的代码做优化处理。一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。下面是volatile变量的几个例子:并行设备. 阅读全文
posted @ 2012-12-11 22:32 猿人谷 阅读(280) 评论(0) 推荐(0)
摘要: c++程序的内存格局通常分为四个区:全局数据区;代码区;栈区;堆区(即自由存储区)。 全局变量、静态数据、常量存放在全局数据区,所有类成员函数和非成员函数代码存放在代码区,为运行函数而分配的局部变量、函数参数、返回地址等存放在栈区,余下的空间都被称为堆区。new和delete,malloc和free,都从堆中分配和释放内存块,但在具体操作上两者有很大的区别。详细的内存分配情况见:http://www.cnblogs.com/heyonggang/p/3250220.html根据《C++内存管理技术内幕》一书,在C++中,内存分成5个区,他们分别是堆,栈,自由存续区,全局/静态存续区,常量存续区 阅读全文
posted @ 2012-12-11 22:26 猿人谷 阅读(987) 评论(0) 推荐(0)
摘要: 句柄和指针其实是两个截然不同的概念。Windows系统用句柄标记系统资源,用句柄隐藏系统的信息。你只要知道有这个东西,然后去调用就行了,它是个32bit的uint。指针则标记某个物理内存地址,是不同的概念。 句柄是一种指向指针的指针。 阅读全文
posted @ 2012-12-11 22:24 猿人谷 阅读(316) 评论(0) 推荐(0)
摘要: malloc和new有以下不同:new、delete是操作符,可以重载,只能在c++中使用。malloc、free是函数,可以覆盖,c、c++中都可以使用。new可以调用对象的构造函数,对应的delete调用相应的析构函数。malloc仅仅负责分配内存,free仅仅回收内存,并不执行构造和析构函数。new、delete返回的是某种数据类型指针,malloc、free返回的是void指针。注意:malloc申请的内存空间要用free释放,而new申请的内存空间要用delete释放,不要混用。因为两者实现的机理不同。有了malloc/free为什么还要new/delete?malloc/free是 阅读全文
posted @ 2012-12-11 22:23 猿人谷 阅读(534) 评论(0) 推荐(0)
摘要: 第一个例子: char *ss="0123456789"; sizeof(ss)=4,ss是指向字符串常量的字符指针。 sizeof(*ss)=1,*ss是第一个字符。第二个例子: char ss[]="0123456789";sizeof(ss)=11, ss是数组,计算到“\0”位置,因此是(10+1)。 sizeof(*ss)=1,*ss是第一个字符。第三个例子: char ss[100]="0123456789";sizeof(ss)=100, ss表示在内存中预分配的大小,100*1。strlen(ss)=10,它的内部是用 阅读全文
posted @ 2012-12-11 22:23 猿人谷 阅读(611) 评论(1) 推荐(0)
摘要: 介绍: 设计内存池的目标是为了保证服务器长时间高效的运行,通过对申请空间小而申请频繁的对象进行有效管理,减少内存碎片的产生,合理分配管理用户内存,从而减少系统中出现有效空间足够,而无法分配大块连续内存的情况。目标: 此次设计内存池的基本目标,需要满足线程安全性(多线程),适量的内存泄露越界检查,运行效率不太低于malloc/free方式,实现对4-128字节范围内的内存空间申请的内存池管理(非单一固定大小对象管理的内存池)。内存池技术设计与实现 本内存池的设计方法主要参考SGI的alloc的设计方案,为了适合一般的应用,并在alloc的基础上做一些简单的修改。 Mempool的内存池设计方案如 阅读全文
posted @ 2012-12-11 22:19 猿人谷 阅读(617) 评论(0) 推荐(1)
摘要: 1.绝对的大牛,受益很多,特别是《数据结构》部分http://www.cnblogs.com/JCSU/articles/1305678.html在VMware Workstation的Ubuntu下安装和配置Hadoop与Ganglia:http://www.cnblogs.com/dolphin... 阅读全文
posted @ 2012-12-11 22:15 猿人谷 阅读(247) 评论(0) 推荐(0)
摘要: GSM就是Global System For Mobile Communication的缩写,意思是全球移动通信系统,就是我们所说的全球通。这个系统是由欧洲主要电信运营者和制造厂家组成的标准化委员会设计开发的。全球通是中国移动通信数字移动电话网所拥有的著名用户品牌。网号139,138,137,136,135都是属于GSM数字移动电话网的,只是 139,138最先开通使用,后来扩充到了这么多。他们的信号接收能力,拨号方法,覆盖范围,漫游地区,使用功能是一致的。下面来说说他的技术,他采用的是数字调制技术,关键之一是时分多址就是说每一个用户在某一个时间上选用载频并且只能在特定时间下收信息。GSM的主 阅读全文
posted @ 2012-12-11 22:14 猿人谷 阅读(977) 评论(0) 推荐(0)
摘要: c/c++ 宏中"#"和"##"的用法2007年05月14日 星期一 上午 10:19一、一般用法我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起.用法:#i nclude<cstdio>#i nclude<climits>using namespace std; #define STR(s) #s#define CONS(a,b) int(a##e##b)int main(){ printf(STR(vck)); // 输出字符串"vck" printf("%d\n", CO 阅读全文
posted @ 2012-12-11 22:01 猿人谷 阅读(250) 评论(0) 推荐(0)
摘要: endl是一个特殊值,称为操纵符(manipulator),将它写入输出流时,具有输出换行的效果,并刷新与设备相关联的缓冲区(buffer)。通过刷新缓冲区,用户可立即看到写入到流中的输出。程序员经常在调试过程中插入输出语句,这些语句都应该刷新输出流。忘记刷新输出流可能会造成输出停留在缓冲区中,如果程序崩溃,将会导致对程序崩溃位置的错误判断。 阅读全文
posted @ 2012-12-11 18:17 猿人谷 阅读(512) 评论(0) 推荐(0)
摘要: 1 #include<stdio.h> 2 int main() 3 { int n,i; 4 printf("please input a number:\n"); 5 scanf("%d",&n); 6 printf("%d=",n); 7 for(i=2;i<=n;i++) 8 { 9 while(n!=i)10 { 11 if(n%i==0)12 { printf("%d*",i);13 n=n/i;14 }15 else16 ... 阅读全文
posted @ 2012-12-11 17:42 猿人谷 阅读(278) 评论(0) 推荐(0)
摘要: 题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数 本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。2.程序源代码: 1 #include<stdio.h> 2 int main() 3 { int i,j,k,n; 4 printf("water flower number is:\n"); 5 for(n=100;n<1000;n++) 6 { i=n/100; /*分解出百位*/ 7 k=n/1 阅读全文
posted @ 2012-12-11 17:28 猿人谷 阅读(334) 评论(0) 推荐(0)
摘要: #include<stdio.h>int main(){ int n,k,p; printf("输入阶乘的基数n:\n"); scanf("%d",&n); p=1; k=1; while(k<=n) { p=p*k; k++; } printf("%d的阶乘为%d\n",n,p); return 0;} 阅读全文
posted @ 2012-12-11 17:14 猿人谷 阅读(294) 评论(0) 推荐(0)
摘要: 1 #include<stdio.h> 2 int main() 3 { int i,j; 4 for(i=1;i<=9;i++) 5 { 6 for(j=1;j<=i;j++) 7 printf("%d*%d=%-4d",j,i,i*j); 8 printf("\n"); 9 }10 return 0;11 } 阅读全文
posted @ 2012-12-11 17:04 猿人谷 阅读(348) 评论(0) 推荐(0)
摘要: 题目:打印楼梯,同时在楼梯上方打印两个笑脸。 1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。2.程序源代码: 1 #include<stdio.h> 2 int main() 3 { int i,j; 4 printf("\1\1\n"); //打印两个笑脸 5 for(i=1;i<11;i++) 6 { for(j=1;j<i;j++) 7 printf("%c%c",23,23); //23这个数字可以是任意数,代表该数所对应的符号 8 printf("\n"); 9 }10 阅读全文
posted @ 2012-12-11 16:53 猿人谷 阅读(1221) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1049 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int n,u,d,s,t; 6 while (cin >> n >> u >> d) 7 { 8 if (n==0) 9 return 0;10 else11 {12 s=0;13 t=0;14 while ... 阅读全文
posted @ 2012-12-11 16:36 猿人谷 阅读(232) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2393 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int n,a,b,c; 6 cin >> n; 7 for (int i=1;i<=n;i++) 8 { 9 cin >> a >> b >> c;10 if (a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a)11 {12 cou... 阅读全文
posted @ 2012-12-11 16:35 猿人谷 阅读(402) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2560Problem DescriptionWe divide the HZNU Campus into N*M grids. As you can see from the picture below, the green grids represent the buidings. Given the size of the HZNU Campus, and the color of each grid, you should count how many green grids in the N*M gr 阅读全文
posted @ 2012-12-11 16:33 猿人谷 阅读(270) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1096Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of inpu 阅读全文
posted @ 2012-12-11 16:31 猿人谷 阅读(344) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1095Problem DescriptionYour task is to Calculate a + b.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and 阅读全文
posted @ 2012-12-11 16:29 猿人谷 阅读(595) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1094Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.OutputFor each test case you should outp 阅读全文
posted @ 2012-12-11 16:28 猿人谷 阅读(468) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1093Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of inpu 阅读全文
posted @ 2012-12-11 16:27 猿人谷 阅读(434) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1092Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this tes 阅读全文
posted @ 2012-12-11 16:25 猿人谷 阅读(344) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1091Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be 阅读全文
posted @ 2012-12-11 16:23 猿人谷 阅读(461) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1090Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of i 阅读全文
posted @ 2012-12-11 16:21 猿人谷 阅读(562) 评论(0) 推荐(0)
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1089Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim. 阅读全文
posted @ 2012-12-11 16:20 猿人谷 阅读(473) 评论(0) 推荐(0)
摘要: 一、时间类型。Linux下常用的时间类型有4个:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm.(1) time_t是一个长整型,一般用来表示用1970年以来的秒数.该类型定义在<sys/time.h>中.一般通过 time_t time = time(NULL); 获取.(2) struct timeb结构: 主要有两个成员, 一个是秒, 另一个是毫秒, 精确度为毫秒.1 struct timeb2 {3 time_t time;4 unsigned short millitm;5 .. 阅读全文
posted @ 2012-12-11 15:37 猿人谷 阅读(545) 评论(0) 推荐(0)
摘要: 前阵子接触到一道关于数组内部链表(多用于内存池技术)的数据结构的题, 这种数据结构能够比普通链表在cache中更容易命中, 理由很简单, 就是因为其在地址上是连续的(=.=!), 借这个机会, 就对cpu cache进行了一个研究, 今天做一个简单的分享, 首先先来普及一下cpu cache的知识, 这里的cache是指cpu的高速缓存. 在我们程序员看来,缓存是一个透明部件. 因此, 程序员通常无法直接干预对缓存的操作. 但是, 确实可以根据缓存的特点对程序代码实施特定优化, 从而更好地利用高速缓存.高速缓存的置换策略会尽可能地将 访问频繁的数据放入cache中, 这是一个动态的过程, 所以 阅读全文
posted @ 2012-12-11 15:32 猿人谷 阅读(886) 评论(0) 推荐(0)
摘要: 在头文件<ctype.h>中定义了一些测试字符的函数。在这些函数中,每个函数的参数都是整型int,而每个参数的值或者为EOF,或者为char类型的字符。<ctype.h>中定义的标准函数列表如下:函数定义函数功能简介int isalnum(int c)检查字符是否是字母或数字int isalpha(int c)检查字符是否是字母int isascii(int c)检查字符是否是ASCII码int iscntrl(int c)检查字符是否是控制字符int isdigit(int c)检查字符是否是数字字符int isgraph(int c)检查字符是否是可打印字符int 阅读全文
posted @ 2012-12-11 13:39 猿人谷 阅读(350) 评论(0) 推荐(0)
摘要: 1. overload 仅仅返回值类型不同的函数不能被重载.2. 所谓私有,是指该成员只能被类本身的方法访问----即使是这个类的对象也不能直接访问.3. 类的对象所占用的空间是其成员变量所用内存的总和(可sizeof求证),对象指针指向其首个成员变量地址.对象的函数不占用对象的内存空间(即使函数中有局部变量)4. const关键字的主要作用是利用编译程序进行排错.和限制对成员的改变.表现在以下方面:(1)const成员函数:const位于形参括号之后,分号之前.该函数不能修改成员变量.(2)const类:对一个类说明使用了const,实际上是说明了一个const型的this指针,该指针只能调 阅读全文
posted @ 2012-12-11 13:31 猿人谷 阅读(273) 评论(0) 推荐(0)
摘要: 题目:输入某年某月某日,判断这一天是这一年的第几天?1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊 情况,闰年且输入月份大于3时需考虑多加一天。2.程序源代码: 1 #include<stdio.h> 2 int main() 3 { 4 int day,month,year,sum,leap; 5 printf("\nplease input year,month,day\n"); 6 scanf("%d,%d,%d",&year,&month,&day); 7 switc 阅读全文
posted @ 2012-12-11 13:26 猿人谷 阅读(2006) 评论(0) 推荐(1)
摘要: 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 2.程序源代码: 1 #include<stdio.h> 2 int main() 3 { int i,j,k; 4 printf("\n"); 5 for(i=1;i<5;i++) /*以下为三重循环*/ 6 for(j=1;j<5;j++) 7 for(k=1;k<5;k++) 8 { if(i!=k&&i!=j&&j!=k 阅读全文
posted @ 2012-12-11 13:22 猿人谷 阅读(1506) 评论(0) 推荐(0)
摘要: 问题的提出:设计一个用于管理朋友信息的程序。将朋友信息(年龄、姓名、电话)存放在MyFrd.dat中,从文件读出这些信息并显示,并能按姓名(要求可简化输入,如只输入姓氏便可查询)进行查询,将查询信息输出屏幕。 1 #include 2 #include 3 #include 4 static... 阅读全文
posted @ 2012-12-11 10:52 猿人谷 阅读(334) 评论(0) 推荐(0)
摘要: 题目(习题3.13):读一组整数到vector对象,计算并输出每对相邻元素的和。如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。然后修改程序:头尾元素两两配对(第一个和最后一个,第二个和倒数第二个,以此类推),计算每对元素的和,并输出。修改程序后: 阅读全文
posted @ 2012-12-11 09:34 猿人谷 阅读(330) 评论(0) 推荐(0)
摘要: 题目(习题3.14):读入一段文本到vector对象,每个单词存储为vector中的一个元素。把vector对象中每个单词转化为大写字母。输出vector对象中转化后的元素,每八个单词为一行输出。 阅读全文
posted @ 2012-12-11 09:32 猿人谷 阅读(303) 评论(0) 推荐(0)
摘要: 结构体字节对齐 在用sizeof运算符求算某结构体所占空间时,并不是简单地将结构体中所有元素各自占的空间相加,这里涉及到内存字节对齐的问题。从理论上讲,对于任何 变量的访问都可以从任何地址开始访问,但是事实上不是如此,实际上访问特定类型的变量只能在特定的地址访问,这就需要各个变量在空间上按一定的规则排列, 而不是简单地顺序排列,这就是内存对齐。 计算结构变量的大小必须讨论数据对齐的问题。为了使CPU存取的速度最快(这同CPU取数操作有关),c++在处理数据时经常把结构变量中的成员的大小按照4或8的倍数计算,这就叫数据对齐(data alignment)。这样做可能会浪费一些内存,但在理论上CP 阅读全文
posted @ 2012-12-11 08:57 猿人谷 阅读(2199) 评论(4) 推荐(2)