zoukankan      html  css  js  c++  java
  • 二级指针的三种内存模型

    第一种内存模型:

    /*
     Module:		二级指针第一种内存模型.cpp
     Notices:		Copyright (c) 2017 Landy Tan
    */
    
    
    #include <iostream>
    using namespace std;
    
    
    /////////////////////////////////////////////////
    
    
    #define SIZE(a) sizeof(a) / sizeof(a[0])
    int SortArray(char **pArray, int nLen);
    int OutputArray(char **pArray, int nLen);
    
    
    /////////////////////////////////////////////////
    
    
    int main()
    {
    	char* pArray[] = { "333", "111", "444", "222", "666" };
    	cout << "Before sorting..." << endl;
    	OutputArray(pArray, SIZE(pArray));
    
    	SortArray(pArray, SIZE(pArray));
    
    	cout << "After sorting..." << endl;
    	OutputArray(pArray, SIZE(pArray));
    
    	system("pause");
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int SortArray(char **pArray, int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    	{
    		for (int j = i; j < nLen; j++)
    		{
    			if (strcmp(*(pArray + i), *(pArray + j)) > 0)
    			{	// Modify the pointer to the point.
    				char *pTmp = *(pArray + i);
    				*(pArray + i) = *(pArray + j);
    				*(pArray + j) = pTmp;
    			}
    		}
    	}
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int OutputArray(char **pArray, int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    		cout << *(pArray + i) << endl;
    	return 0;
    }
    
    
    //////////////// End of File ////////////////////
    

      

    第二种内存模型:

    /*
     Module:		二级指针第二种内存模型.cpp
     Notices:		Copyright (c) 2017 Landy Tan
    */
    
    
    #include <iostream>
    using namespace std;
    
    
    /////////////////////////////////////////////////
    
    
    #define SIZE(a) sizeof(a) / sizeof(a[0])
    int SortArray(char(*pArray)[30], int nLen);
    int OutputArray(char(*pArray)[30], int nLen);
    
    
    /////////////////////////////////////////////////
    
    
    int main()
    {
    	char pArray[][30] = { "333", "111", "444", "222", "666" };
    	cout << "Before sorting..." << endl;
    	OutputArray(pArray, SIZE(pArray));
    
    	SortArray(pArray, SIZE(pArray));
    
    	cout << "After sorting..." << endl;
    	OutputArray(pArray, SIZE(pArray));
    
    	system("pause");
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int SortArray(char(*pArray)[30], int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    	{
    		for (int j = i; j < nLen; j++)
    		{
    			if (strcmp(*(pArray + i), *(pArray + j)) > 0)
    			{	// Modify the data pointed to by the pointer.
    				char sz[30] = { 0 };
    				strcpy_s(sz, *(pArray + i));
    				strcpy_s(*(pArray + i), *(pArray + j));
    				strcpy_s(*(pArray + j), sz);
    			}
    		}
    	}
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int OutputArray(char(*pArray)[30], int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    		cout << *(pArray + i) << endl;
    	return 0;
    }
    
    
    //////////////// End of File ////////////////////
    

      

    第三种内存模型:

    /*
    Module:			二级指针第三种内存模型.cpp
    Notices:		Copyright (c) 2017 Landy Tan
    */
    
    
    #include <iostream>
    using namespace std;
    
    
    /////////////////////////////////////////////////
    
    
    int SortArray(char **pArray, int nLen);
    int OutputArray(char **pArray, int nLen);
    int NewBuffer(char ***pBuf, int nLen1, int nLen2);
    void DeleteBuffer(char ***pBuf, int nLen1);
    
    /////////////////////////////////////////////////
    
    
    int main()
    {
    	char **pBuf;
    	NewBuffer(&pBuf, 5, 30);
    	if (pBuf == NULL)
    	{
    		cout << "New buffer error." << endl;
    		system("pause");
    		return -1;
    	}
    	strcpy_s(*(pBuf + 0), 30, "333");
    	strcpy_s(*(pBuf + 1), 30, "111");
    	strcpy_s(*(pBuf + 2), 30, "444");
    	strcpy_s(*(pBuf + 3), 30, "222");
    	strcpy_s(*(pBuf + 4), 30, "666");
    
    
    	cout << "Before sorting..." << endl;
    	OutputArray(pBuf, 5);
    
    	SortArray(pBuf, 5);
    
    	cout << "After sorting..." << endl;
    	OutputArray(pBuf, 5);
    
    	DeleteBuffer(&pBuf, 5);
    	system("pause");
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int SortArray(char **pArray, int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    	{
    		for (int j = i; j < nLen; j++)
    		{
    			if (strcmp(*(pArray + i), *(pArray + j)) > 0)
    			{	
    
    			}
    		}
    	}
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int OutputArray(char **pArray, int nLen)
    {
    	if (pArray == NULL || nLen <= 0)
    		return -1;
    
    	for (int i = 0; i < nLen; i++)
    		cout << *(pArray + i) << endl;
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    int NewBuffer(char ***pBuf, int nLen1, int nLen2)
    {
    	if (pBuf == NULL || nLen1 <= 0 || nLen2 <= 0)
    		return -1;
    	char **p = *pBuf;
    	p = new char*[nLen1];
    	for (int i = 0; i < nLen1; ++i)
    	{
    		p[i] = new char[nLen2]{ 0 };
    	}
    	*pBuf = p;
    	return 0;
    }
    
    
    /////////////////////////////////////////////////
    
    
    void DeleteBuffer(char ***pBuf, int nLen1)
    {
    	if (pBuf == NULL)
    		return ;
    
    	char **p = *pBuf;
    	for (int i = 0; i < nLen1; i++)
    	{
    		delete[] p[i];
    		p[i] = NULL;
    		
    	}
    	delete[] p;
    	p = NULL;
    
    	*pBuf = p;
    	return;
    }
    
    
    //////////////// End of File ////////////////////
    

      

  • 相关阅读:
    Cookie 学习笔记
    IEDA的图片视频等文件在Tomcat上部署不能访问的问题
    JavaWeb 的路径访问问题,以及路径命名规则。
    HttpServlet 的 Request 方法 学习笔记
    Servlet概念 ,体系结构及配置 HTTP概念
    XML学习笔记
    IDEA创建Tomcat服务器,以及新创建的JaveWeb项目(JavaEE)如何部署在Tomcat上,以及出现404的原因,以及一些设置。
    IDEA将多个项目创建在同一个文件夹下,及创建JavaWeb项目方法
    Spring JDBCTemplate对象的增删查改写法 学习笔记
    Druid 数据库连接池技术(JDBCUtils工具类的编写) 学习笔记
  • 原文地址:https://www.cnblogs.com/LandyTan/p/6828182.html
Copyright © 2011-2022 走看看