1. 程式人生 > >c/c++多維陣列的動態記憶體開闢與釋放

c/c++多維陣列的動態記憶體開闢與釋放

c語言版本

/************************************************************************/
/*
1、使用C語言方式模擬二維陣列的動態開闢與釋放 
2、使用C++ 語言方式模擬二維陣列的動態開闢與釋放 
3、擴充套件多維陣列的動態開闢與釋放                                                                     */
/************************************************************************/

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <vld.h>
//目標是出現No memory leaks detected

#define Type int 
#define ROW 3
#define COL 4

Type** _Malloc(int row, int col)
{
	Type **p = (Type**)malloc(sizeof(Type*) * row); //申請陣列指標空間
	assert(p != NULL);
	for (int i = 0; i < row; ++i)
	{
		p[i] = (Type*)malloc(sizeof(Type)* COL);
		assert(p[i] != NULL);

	}
	return p;
}
void _Assign(Type **p, int row, int col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			p[i][j] = i + j;

		}
		
	}
}
void _Print(Type **p, int row, int col)
{

	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			printf("%d ", p[i][j]);
		}
		printf("\n");
	}
}
void _Free(Type **p, int row)
{
	for (int i = 0; i < row; ++i)
	{
		free(p[i]);
	}
	free(p);

}

int main(int argc, char* argv[])
{
	Type **p = _Malloc(ROW, COL);
	_Assign(p, ROW, COL);
	_Print(p, ROW,COL);
	_Free(p, ROW);

	system("pause");
	return 0;
}
c++版本
/************************************************************************/
/*
1、使用C語言方式模擬二維陣列的動態開闢與釋放
2、使用C++ 語言方式模擬二維陣列的動態開闢與釋放
3、擴充套件多維陣列的動態開闢與釋放                                                                     */
/************************************************************************/

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <vld.h>
//目標是出現No memory leaks detected
#define Type int   //char int
#define ROW 3
#define COL 4

Type** _New(int row, int col)
{
	Type **p = new Type *[row];//申請陣列指標空間
	assert(p != NULL);
	for (int i = 0; i < row; ++i)
	{
		p[i] = new Type[col];
		assert(p[i] != NULL);

	}
	return p;
}
void _Assign(Type **p, int row, int col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			p[i][j] = i + j;

		}

	}
}
void _Print(Type **p, int row, int col)
{

	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			printf("%d ", p[i][j]);
		}
		printf("\n");
	}
}
void _Delete(Type **p, int row)
{
	for (int i = 0; i < row; ++i)
	{
		delete[]p[i];//c++ 釋放陣列空間
	}
	delete[]p;//c++釋放陣列

}

int main(int argc, char* argv[])
{
	Type **p = _New(ROW, COL);
	_Assign(p, ROW, COL);
	_Print(p, ROW, COL);
	_Delete(p, ROW);

	printf("c++\n");
	system("pause");
	return 0;
}

/************************************************************************/
/*
1、使用C語言方式模擬二維陣列的動態開闢與釋放
2、使用C++ 語言方式模擬二維陣列的動態開闢與釋放
3、擴充套件多維陣列的動態開闢與釋放                                                                     */
/************************************************************************/

#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <vld.h>
#include <iostream>
using namespace std;
//目標是出現No memory leaks detected
#define Type int   //char int
#define ROW 3
#define COL 4

typedef Type(*Arr)[COL];//陣列指標,開闢COL列陣列

Arr _New()
{
	Arr p = new Type[ROW][COL];

	return p;
}
void _Assign(Arr p)
{
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			p[i][j] = i + j;

		}

	}
}
void _Print(Arr p)
{

	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			cout << p[i][j]<<" ";
		}
		cout <<endl;
	}
}
void _Delete(Arr p)
{
	delete[]p;//c++釋放陣列

}

int main(int argc, char* argv[])
{
	Arr p = _New();
	_Assign(p);
	_Print(p);
	_Delete(p);
	system("pause");
	return 0;
}
/************************************************************************/
/*三維陣列                                                               */
/************************************************************************/

#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <vld.h>
#include <iostream>
using namespace std;
//目標是出現No memory leaks detected
#define Type int   //char int
#define ROW 3
#define COL 4
#define Z 3


typedef Type(*Arr)[ROW][COL];//三維陣列指標,開闢COL列陣列

Arr _New()
{
	Arr p = new Type[Z][ROW][COL];

	return p;
}
void _Assign(Arr p)
{
	for (int k = 0; k < Z; ++k)
	{
		for (int i = 0; i < ROW; ++i)
		{
			for (int j = 0; j < COL; ++j)
			{
				p[k][i][j] = i + j+k;
			}
		}
	}
}
void _Print(Arr p)
{

	for (int k = 0; k < Z; ++k)
	{
		for (int i = 0; i < ROW; ++i)
		{
			for (int j = 0; j < COL; ++j)
			{
				cout << p[k][i][j]<< " ";
			}			
		}
		cout << endl;
	}
}
void _Delete(Arr p)
{
	delete []p;//c++釋放陣列

}

int main(int argc, char* argv[])
{
	Arr p = _New();
	_Assign(p);
	_Print(p);
	_Delete(p);
	return 0;
}