二级指针的三种内存模型

第一种内存模型:

/*
 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 ////////////////////

  

时间: 2024-08-06 08:45:24

二级指针的三种内存模型的相关文章

C++二级指针第三种内存模型

#include "stdio.h" #include "stdlib.h" #include "string.h" void main() { int i = 0, j = 0; char buf[100]; char **myarray = (char **)malloc(10*sizeof(char*)); //int array[10] if (myarray == NULL) { return; } for (i=0; i<10;

二级指针的3种内存模型

二级指针的内存模型 二级指针的第一种内存模型 Char*Accary [ ] ={ "aaaaaa", "bbbbbb", "ccccccc" }; //接口形参使用方式 Intprintfarr(char **ArrayStr,int iNUm) { For(i =0; i<iNUm; i++) { Printf("%s \n", ArrayStr[i]); } } //调用方式 Printfarr(Accary, 3

实现按行读取文件,把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出。

/* 2 编写一个业务函数,实现按行读取文件.把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出. 函数原型有两个,任意选择其一 要求1:请自己任意选择一个接口(函数),并实现功能:70分 要求2:编写测试用例.30分 要求3:自己编写内存释放函数 */ /********************************************************************** * 版权所有 (C)2015, Wu Yingqiang. * * 文件名称:ReadFi

二级指针三种内存模型综合训练

/*** point_practice.c ***/ #include<stdio.h> #include<string.h> #include<stdlib.h> int sort( char **myp1 /*in*/, int num1, char (*myp2)[30], int num2, char ***myp3, int *num3) { int i = 0, j = 0, k = 0; int tmplen; char **p3 = NULL; char

二级指针做输入的第三种内存模型:手工打造二维内存

声明局部变量p, p指向一段内存地址,这段内存地址存放这N个指针,每个指针都指向malloc的另一段内存. 内存模型图如下: p应该是二级指针 #define _CRT_SECURE_NO_WARNINGS#include<stdlib.h>#include<stdio.h>#include<string.h>#include<ctype.h> char**getMem(int num){ int i = 0, j = 0; char** p2 = NULL

C++二级指针第二种内存模型(二维数组)

C++二级指针第二种内存模型(二维数组) 二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. 定义 类型说明符 数组名[常量表达式][常量表达式] 例如: float a[3][4],b[5][10]; 二维数组元素地址 #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; int a[3][4]={ {1,2,3

C提高_day03_二级指针做输入第2种内存模型

#include <stdlib.h> #include <string.h> #include <stdio.h> //打印 排序 //封装成函数 void main() { int i = 0, j = 0; int num = 4; char tmpBuf[30]; char myArray[10][30] = {"aaaaaa", "ccccc", "bbbbbbb", "1111111111

阿里Java开发工程师理解的三种架构模型

常用的软件架构模型可以归类为三种架构模型:3/N层架构."框架+插件"架构.地域分布式架构. 一.三种架构模型 1.3/N层架构 这是经典的多层架构模型,对于稍微复杂一点或特别复杂的系统,不使用分层架构是很难想象的.下图是经典的3层架构: 如今,凡是个程序员都能侃侃而谈3/N层架构,这确实是解决系统复杂性的一种主流模式,但是,只要采用了3/N层架构是不是就一定能解决系统的复杂性了?不一定,关键在于你在你的系统中如何实作你的3/N层结构. 在采用了3/N层架构后,我们还是要解决以下非常重

用2种内存模型来排序字符串的的顺序,一种是交换内存地址,第二种是交换内存里面的值;

#define _CRT_SECURE_NO_WARNINGS #include"stdio.h" #include"stdlib.h" #include"string.h" void MyPrintf(char **); void MYSORT(char **, int); void SORTBUF(char **); void main() { int i = 0; int j = 0; char *buf; char **myarray;