zoukankan      html  css  js  c++  java
  • 动态分配

    动态分配:

    1.类型  *指针名 = new 类型;

    指针名指向的是申请内存的首地址;失败返回NULL
    2.类型* 指针名 = new 类型(初始化值);
    3.类型* 指针名 = new 类型[数组大小];
    4.类型* 指针名 = new 类型[数组大小]{初始化列表}

    内存释放:
    delete 指针; //单个内存情况
    delete[] 指针 // 对于数组的内存空间释放
    malloc---free new---- delete

    注意:

    1.只可以释放new的内存空间,不可以释放malloc的内存空间(malloc释放使用free释放)
    2.不可以对同一块内存多次delete.先判断指针是否为NULL

     1         int *a = new int;
     2     *a = 20;
     3     int *b = new int(20);
     4     int *p = new int[10];
     5     int *q = new int[10]{ 10, 20, 30 };
     6     
     7     cout << "*a = " << *a << endl;
     8     cout << "*b = " << *b << endl;
     9     for (int i = 0; i < 10; i++){
    10         cout << p[i] << " ";
    11     }
    12     cout << endl;
    13     for (int i = 0; i < 10; i++){
    14         cout << *q++ << " ";
    15     }
    16     cout << endl;
    17 
    18     delete a, b;
    19     delete[] p, q;    
    View Code

    名字空间
    为了避免出现同样的变量名或者函数名或者类名,用名字空间限定某一个范围(作用域),不同作用域的变量不冲突
    定义名字空间:
    namespace 名字空间名{ 变量定义; 函数定义; 类定义 }
    使用:
    1.名字空间名::成员
    2. 全局using namespace 名字空间名;
    ---表示从using开始的代码所使用的变量是名字空间的.

    注意:如果在两个名字空间中有相同的标识符,不可以同时使用using namespace.
    ----如果在局部有名字空间中的相同标识符,会优先使用局部
    3.局部using namespace 名字空间名;
    -----只表示在局部使用名字空间中的内容,其他地方不可以使用

    4.using 名字空间名::变量名;
    5.两个名字空间名相同,编译器会合并两个名字空间
    6.可以在名字空间中在定义名字空间,叫做名字空间嵌套
    7.名字空间的重命名 namespace 短名 = xxxx::000::xxxxxx;

     1 #include "stdafx.h"
     2 #include <iostream>
     3 
     4 namespace class1{
     5     int wyx = 12;  
     6     char ergouzi;
     7     void fun2(int x);
     8     namespace class1_class
     9     {
    10         int wyx = 30;
    11     }
    12 }
    13 
    14 namespace class2{
    15     int wyx = 30;
    16     char oyl;
    17     void fun2(int x);
    18 }
    19 
    20 namespace class2{
    21     int oyl = 30;
    22     char wyx_son;
    23     void fun3(int x);
    24 }
    25 
    26 //using namespace class1;
    27 using class2::wyx;
    28 
    29 using namespace std;
    30 int _tmain(int argc, _TCHAR* argv[])
    31 {
    32     //int danny = 120;
    33     //class2::fun2(20);
    34     namespace cls = class1::class1_class;
    35     cls::wyx = 40;
    36 
    37     printf("class1::wyx = %d class2:wyx = %d
    ",
    38         wyx, wyx);
    39     
    40     cout << wyx <<endl;
    41     cin >> wyx;
    42     cout << wyx << endl;
    43     getchar();
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    【Linux】ubuntu各文件夹简介
    【Linux】 ubuntu 12.04 iNode Client找不到库libjpeg和libtiff的解决方法
    【Coding】ant 的 javac标签 (归纳)
    【Coding】Ant脚本命令
    【Linux】Ubuntu使用技巧
    【Linux】ubuntu下词典软件Goldendict介绍(可屏幕取词)和StarDict(星际译王)的安装
    【Coding】Ubuntu/环境变量:修改/etc/environment 导致开机不能登录!
    备用访问映射
    开发Silverlight类型的WebPart部署到Sharepoint2010上(转)
    (转)通过Internet访问 SharePoint
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8343977.html
Copyright © 2011-2022 走看看