程序人生

我一生的追求!!
随笔 - 19, 文章 - 0, 评论 - 21, 引用 - 1
数据加载中……

2008年2月19日

2008年2月编程语言排名

posted @ 2008-02-19 10:30 浴盆 阅读(2555) | 评论 (7)编辑

2008年2月7日

C++中关于sizeof的相关应用

#include<iostream>
using namespace std;

int main()
{
    
int arry[3= {1};

    cout
<<"sizeof(array)"<<"="<<sizeof(arry)<<endl;
    cout
<<"sizeof(arry[0])"<<"="<<sizeof(arry[0])<<endl;
    cout
<<"arry[1]"<<"="<<arry[1]<<endl;

    
return 0;
}
输出结果:
sizeof(array)=12
sizeof(arry[0])=4
arry[1]=0
我们可以知道sizeof(array)取的是整个数组的长度,sizeof(arry[0])是取数组中第一个元素的长度
arry[1]=0我们可以看出如果不初始化数组元素默认为0

posted @ 2008-02-07 16:12 浴盆 阅读(194) | 评论 (3)编辑

2008年2月3日

用泛型实现在数组模拟入栈出栈时多类型的使用

/*
 * Created by SharpDevelop.
 * User: ??
 * Date: 2008-2-3
 * Time: 9:14
 * 
 * ??????
 
*/


using System;
class Statck<T>
{
    
private T[] statck;
    
private int count;
    
public Statck(int size)
    
{
        statck 
= new T[size];
        count 
= 0;
    }

    
public void Push(T x)
    
{
        statck[count
++= x;
    }

    
public T Pop()
    
{
        
return statck[--count];
    }

}


class Test
{
    
static void Main()
    
{
        Statck
<int> s = new Statck<int>(10);
        s.Push(
1);
        s.Push(
2);
        Console.WriteLine(s.Pop()
+s.Pop());
    }

}

posted @ 2008-02-03 10:54 浴盆 阅读(143) | 评论 (2)编辑

2008年2月1日

C++中几种不同交换两个数的方法

#include<iostream>
using namespace std;
void swapr(int & a, int &b);
void swapp( int * a, int * b);
void swapv(int a,int b);

int main()
{
    
int a,b;

    cout
<<"请输入a"<<endl;
    cin
>>a;
    cout
<<"请输入b"<<endl;
    cin
>>b;

    cout
<<"交换前"<<endl;
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;

    cout
<<"使用引用交换"<<endl;
    swapr(a,b);
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;


    cout
<<"使用指针交换"<<endl;
    swapp(
&a,&b);
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;

    cout
<<"使用值交换"<<endl;
    swapv(a,b);
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;





    
return 0;
}





    
void swapr(int & a, int &b)
    
{
        
int temp;
        temp 
= a;
        a 
= b;
        b 
= temp;
    }

 

    
void swapp( int * a, int * b)
    
{
        
int temp;
        temp 
= *a;
        
*= *b;
        
*= temp;
    }


    
void swapv(int a,int b)
    
{
        
int temp;
        temp 
= a;
        a 
= b;
        b 
= temp;
    }
我们可以发现使用值传递并不能交换两个数

posted @ 2008-02-01 13:22 浴盆 阅读(163) | 评论 (0)编辑

C++中引用变量的用例

#include<iostream>
using namespace std;

int main()
{
    
int i = 1;

    
int &= i;           // 对变量I的引用

    cout
<<"i = "<<i<<"   "<<"address = "<<&i<<endl;
    cout
<<"x = "<<x<<"   "<<"address = "<<&x<<endl;

    
int y = 2;

    x 
= y;

    cout
<<"i = "<<i<<"   "<<"address = "<<&i<<endl;
    cout
<<"x = "<<x<<"   "<<"address = "<<&x<<endl;
    cout
<<"y = "<<y<<"   "<<"address = "<<&y<<endl;
    
    
return 0;
}
结果如下
i = 1 address = 0012ff6c
x= 1 address = 0012ff6c
i = 2 address = 0012ff6c
x= 2 address = 0012ff6c
y= 2address = 0012ff70

作为i的引用x,它们都指向相同的值和地址.
x=y;只是改变x的值,由于x是i的引用,所以,不能改变x的地址

posted @ 2008-02-01 12:54 浴盆 阅读(89) | 评论 (0)编辑

关于strlen/sizeof函数在char和string类型中的应用

#include<iostream>
using namespace std;

int main()
{
//    typedef struct student 
//{
//       char name[10];
//       char sex; 
//       long sno; 
//       float score [4]; 
//} STU; 
//
//STU a[5];
//
//cout<<sizeof(a)<<endl;
//
//return 0;

    
char ghost[15= "galloping";
    
char * str = "galloping";

    
int n1 = strlen(ghost);             //字符数组中字符的实际长度
    int n2 = strlen(str);               //指针指向的字符数组的实际长度
    int n3 = strlen("galloping");       //字符串中字符的实际长度

    
int n4 = sizeof(ghost);             //字符数组分配空间大小
    int n5 = sizeof(str);               //指针分配的空间大小
    int n6 = sizeof("galloping");       //字符串分配空间大小,注意最后一位要加上'\0'

    cout 
<<"n1 = "<<n1<<endl;
    cout 
<<"n2 = "<<n2<<endl;
    cout 
<<"n3 = "<<n3<<endl;

    cout 
<<"n4 = "<<n4<<endl;
    cout 
<<"n5 = "<<n5<<endl;
    cout 
<<"n6 = "<<n6<<endl;

}
 

posted @ 2008-02-01 10:01 浴盆 阅读(279) | 评论 (0)编辑

2008年1月31日

常用路由协议

RIP/OSPF/IGRP/BGP

posted @ 2008-01-31 21:24 浴盆 阅读(133) | 评论 (0)编辑

关于内存泄露

内存泄漏可以分为4类:
1. 常发性内存泄漏。发生内存泄漏的代码会被多次执行到,每次被执行的时候都会导致一块内存泄漏。
2. 偶发性内存泄漏。发生内存泄漏的代码只有在某些特定环境或操作过程下才会发生。常发性和偶发性是相对的。对于特定的环境,偶发性的也许就变成了常发性的。所以测试环境和测试方法对检测内存泄漏至关重要。
3. 一次性内存泄漏。发生内存泄漏的代码只会被执行一次,或者由于算法上的缺陷,导致总会有一块仅且一块内存发生泄漏。比如,在类的构造函数中分配内存,在析构函数中却没有释放该内存,所以内存泄漏只会发生一次。
4. 隐式内存泄漏。程序在运行过程中不停的分配内存,但是直到结束的时候才释放内存。严格的说这里并没有发生内存泄漏,因为最终程序释放了所有申请的内存。但是对于一个服务器程序,需要运行几天,几周甚至几个月,不及时释放内存也可能导致最终耗尽系统的所有内存。所以,我们称这类内存泄漏为隐式内存泄漏。

从用户使用程序的角度来看,内存泄漏本身不会产生什么危害,作为一般的用户,根本感觉不到内存泄漏的存在。真正有危害的是内存泄漏的堆积,这会最终消耗尽系统所有的内存。从这个角度来说,一次性内存泄漏并没有什么危害,因为它不会堆积,而隐式内存泄漏危害性则非常大,因为较之于常发性和偶发性内存泄漏它更难被检测到。

posted @ 2008-01-31 21:19 浴盆 阅读(140) | 评论 (0)编辑

不使用第三个变量交换两个变量

#include<iostream>
using namespace std;

int main()
{
    
int a,b;
    cin
>>a;
    cin
>>b;

    cout
<<"交换前"<<endl;
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;
    

    a 
= a+b;
    b 
= a -b;
    a 
= a -b;

    cout
<<"交换后"<<endl;
    cout
<<"a = "<<a<<endl;
    cout
<<"b = "<<b<<endl;
    
    
return 0;
}

posted @ 2008-01-31 20:34 浴盆 阅读(162) | 评论 (6)编辑

struct和union的大小问题

union类型以其中size最大的为其大小
struct类型以其中所有size大小之和为其大小
#include<iostream>
using namespace std;

int main()
{
    typedef union 
{long i; int k[5]; char c;} DATE;
    
struct data int cat; DATE cow; double dog;} too;
    DATE max;

    cout
<<"sizeof(struct date)+sizeof(max) = "<<sizeof(too)+sizeof(max)<<endl;
    cout
<<"sizeof(too) = "<<sizeof(too)<<endl;
    cout
<<"sizeof(max) = "<<sizeof(max)<<endl;
    cout
<<"struct data.cow size = "<<sizeof(too.cow)<<endl;
    cout
<<"union DATE.i size = "<<sizeof(max.i)<<endl;
    cout
<<"union char.c size = "<<sizeof(max.c)<<endl;

}
sizeof(struct date)+sizeof(max)返回52
#include<iostream>
using namespace std;

int main()
{
    typedef union student 
{
       
char name[10];
       
long sno; 
       
char sex; 
       
float score [4]; 
}
 STU; 

STU a[
5];

cout
<<sizeof(a)<<endl;

return 0;

}
 
初始化了一个含有5个UNION的数组,由于UNION以其中最大的元素float作为大小  16*5=80

#include<iostream>
using namespace std;

int main()
{
    typedef 
struct student 
{
       
char name[10];
       
long sno; 
       
char sex; 
       
float score [4]; 
}
 STU; 

STU a[
5];

cout
<<sizeof(a)<<endl;

return 0;

}
 
输出为180
自然对齐(natural alignment)即默认对齐方式,是指按结构体的成员中(类型)size最大的成员作为基本的分配单元,而且与其顺序有这密切的联系。size最大的是long,size是 4,所以,按照顺序,Char name[10];12个字节;Long sno; 4个字节;Char sex; 4个字节(这里对齐了);Float score [4]; 16个字节。于是(12+4+4+16)×5=180

#include<iostream>
using namespace std;

int main()
{
    typedef 
struct student 
{
       
char name[10];
       
char sex; 
       
long sno; 
       
float score [4]; 
}
 STU; 

STU a[
5];

cout
<<sizeof(a)<<endl;

return 0;

}
 

答案是:160. 为什么,只是换了顺序而已呀?关键就在顺序上。

结构体中,size最大的是long,size是 4,所以,按照顺序,Char name[10];12个字节;但是这12中多分配的2个字节可以包含后面的Char sex; (问题就在这Float score [4]; 16个字节。于是(12+4+16)×5=160

posted @ 2008-01-31 17:42 浴盆 阅读(264) | 评论 (0)编辑