zoukankan      html  css  js  c++  java
  • 指向指针的指针的理解和应用

    总结:

    1. 申请内存,此处GetMeory参数不用指向指针的指针将无法得到内存,多次调用还会造成内存泄露。  

    当然此处的GetMeory可以用返回指针的方式,这样就可以不用指向指针的指针。

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    void GetMeory(char **p, int num)
    {
     *p = (char *)malloc(sizeof(char) * num);
     //*p = new char[num];  //C++当中
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
     char *str = NULL;
     GetMeory(&str, 100);
     strcpy(str,"Hello");
     cout << str << endl;
     return 0;
    }
    

    2. 二级指针还经常用在动态申请二维数组

    void main() 
    { 
      int m , n , **p; 
      scanf("%d%d" , &m , &n); 
      p = (int **)malloc(m * sizeof(int *)) 
      //C++中建议使用:p = new int* [m]; 
      for(i = 0 ; i < m ; i++) 
      p[i] = (int *)malloc(n * sizeof(int)); 
      //C++:p[i] = new int[n]; 

      // 释放
      for(i=0; i<m; i++)
      free(p[i]);
      delete [] p[i]; // C++ [] 不可少
      free(p);
      delete [] p;
    }

        

    参考:

    http://www.jb51.net/article/37516.htm

    http://blog.csdn.net/bzhxuexi/article/details/17230073

  • 相关阅读:
    RHEL7.2安装及配置实验环境
    VMwareworkstation 12安装
    Iterator主要有三个方法:hasNext()、next()、remove()详解
    httpclient
    http接口测试——Jmeter接口测试实例讲解
    java获取Excel的导出
    java获取Excel的导入
    java的post请求
    java的get请求
    Python3 列表(List)基础
  • 原文地址:https://www.cnblogs.com/swing07/p/7787711.html
Copyright © 2011-2022 走看看