zoukankan      html  css  js  c++  java
  • 函数 free 的原型

     函数 free 的原型如下:

    void free( void * memblock ); 为什么 free 函数不象 malloc 函数那样复杂呢?

    这是因为指针 p 的类型以及它所指 的内存的容量事先都是知道的,语句 free(p)能正确地释放内存。

    如果 p 是 NULL 指针, 那么 free 对 p 无论操作多少次都不会出问题。

    如果 p 不是 NULL 指针,那么 free 对 p 连续操作两次就会导致程序运行错误

     1 #include <iostream>
     2 
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 using namespace std;
     5 //声明引用参数的函数模板原型
     6 template <class T> void swap(T &x, T &y);
     7 
     8 //定义一个结构类型
     9 struct student {
    10     int n;
    11     char name[20];
    12     float grade;
    13 };
    14 
    15 
    16 int main(int argc, char** argv) {
    17         //交换两个int型变量中的数据
    18     int m=3,n=5;
    19     cout<<"m="<<m<<"  n="<<n<<endl;
    20     swap(m,n);
    21     cout<<"m="<<m<<"  n="<<n<<endl;
    22     cout<<"-------------------"<<endl;
    23 
    24     //交换两个double型变量中的数据
    25     double x=3.5,y=5.7;
    26     cout<<"x="<<x<<"  y="<<y<<endl;
    27     swap(x,y);
    28     cout<<"x="<<x<<"  y="<<y<<endl;
    29     cout<<"-------------------"<<endl;
    30 
    31     //交换两个char型变量中的数据
    32     char c1='A',c2='a';
    33     cout<<"c1="<<c1<<"  c2="<<c2<<endl;
    34     swap(c1,c2);
    35     cout<<"c1="<<c1<<"  c2="<<c2<<endl;
    36     cout<<"-------------------"<<endl;
    37     
    38     //交换两个结构变量中的数据
    39     student s1={1001,"ZhangHua",90};
    40     student s2={1011,"LiWei",95.5};
    41     cout<<"s1:  ";
    42     cout<<s1.n<<"  "<<s1.name<<"  "<<s1.grade<<endl;
    43     cout<<"s2:  ";
    44     cout<<s2.n<<"  "<<s2.name<<"  "<<s2.grade<<endl;
    45     swap(s1,s2);
    46     cout<<"swap(s1,s2):"<<endl;
    47     cout<<"s1:  ";
    48     cout<<s1.n<<"  "<<s1.name<<"  "<<s1.grade<<endl;
    49     cout<<"s2:  ";
    50     cout<<s2.n<<"  "<<s2.name<<"  "<<s2.grade<<endl;
    51     return 0;
    52 }
    53 
    54 //定义名为swap的函数模板用于交换两个变量中的数据
    55 template <class T> void swap(T &x, T &y)
    56 {
    57     T temp;
    58     temp=x;
    59     x=y;
    60     y=temp;
    61 }
  • 相关阅读:
    通过IP获取所在城市
    一次完整的HTTP请求过程
    Array.prototype.slice.call(arguments)
    移动web资源整理
    Null 和 Undefined
    JS正则表达式
    JavaScript和JSP的区别?
    JS判断图片上传时文件大小和图片尺寸
    JavaScript中的callee与caller的区别
    php结合redis实现高并发下的抢购、秒杀功能
  • 原文地址:https://www.cnblogs.com/borter/p/9413719.html
Copyright © 2011-2022 走看看