如何在VC中创建动态数组

如何在VC中创建动态数组,第1张

怎样给多维数组动态分配内存

//Allocate:

int **p = new int* [m]

for(int i = 0 i <m i++)

p[i] = new int[n]

//Use:

for(int i = 0 i <mi++)

for(int j = 0 j <n j++)

p[i][j] = i * j

//Free:

for(int i = 0 i <m i++)

delete[] p[i]

delete[] p

1. 演示形为int[2][3]的二维动态数组

///////////////////////////////////////////////////////////////////

int n1, n2

const int DIM1 = 2

const int DIM2 = 3

// 构造数组

int **ppi = new int*[DIM1]

for(n1 = 0n1 <DIM1n1++)

{

ppi[n1] = new int[DIM2]

}

// 填充数据

for(n1 = 0n1 <DIM1n1++)

{

for(n2 = 0n2 <DIM2n2++)

{

ppi[n1][n2] = n1 * 10 + n2

}

}

// 输出

for(n1 = 0n1 <DIM1n1++)

{

for(n2 = 0n2 <DIM2n2++)

{

afxDump <<"ppi[" <<n1 <<"][" <<n2 <<"] = "

<<ppi[n1][n2] <<"/n"

}

}

// 释放数组

for(n1 = 0n1 <DIM1n1++)

{

delete [] ppi[n1]

}

delete [] ppi

2. 三维动态数组(int[2][3][4])

///////////////////////////////////////////////////////////////////

int n1, n2, n3

const int DIM1 = 2

const int DIM2 = 3

const int DIM3 = 4

// 构造数组

int ***ppi = new int**[DIM1]

for(n1 = 0n1 <DIM1n1++)

{

ppi[n1] = new int*[DIM2]

for(n2 = 0n2 <DIM2n2++)

{

ppi[n1][n2] = new int[DIM3]

}

}

// 填充数据

for(n1 = 0n1 <DIM1n1++)

{

for(n2 = 0n2 <DIM2n2++)

{

for(n3 = 0n3 <DIM3n3++)

{

ppi[n1][n2][n3] = n1 * 100 + n2 * 10 + n3

}

}

}

// 输出

for(n1 = 0n1 <DIM1n1++)

{

for(n2 = 0n2 <DIM2n2++)

{

for(n3 = 0n3 <DIM3n3++)

{

afxDump <<"ppi[" <<n1 <<"][" <<n2 <<"][" <<n3 <<"] = "

<<ppi[n1][n2][n3] <<"/n"

}

}

}

// 释放数组

for(n1 = 0n1 <DIM1n1++)

{

for(n2 = 0n2 <DIM2n2++)

{

delete [] ppi[n1][n2]

}

delete [] ppi[n1]

}

delete [] ppi

你为什么要在CArry::CArry(unsigned int length)前写个void?那是非法的,删掉它便可!

另:函数中的逻辑似乎不对,据题意应该是:

if (length>0)

int *m_Data=new int[m_nLength]

或者

int *m_Data

if (length>0)

m_Data=new int[m_nLength]

else

m_Data=NULL

才对。不然,将会产生给未定义指针赋值的运行时错误!

int n1, n2

const int DIM1 = 2

const int DIM2 = 3

// 构造数组

int **ppi = new int*[DIM1]

for(n1 = 0n1 <DIM1n1++){ppi[n1] = new int[DIM2]}// 填充数据

for(n1 = 0n1 <DIM1n1++){for(n2 = 0n2 <DIM2n2++){ppi[n1][n2] = n1 * 10 + n2}}// 输出for(n1 = 0n1 <DIM1n1++){for(n2 = 0n2 <DIM2n2++){afxDump <<"ppi[" <<n1 <<"][" <<n2 <<"] = "

<<ppi[n1][n2] <<"\n"}}// 释放数组

for(n1 = 0n1 <DIM1n1++){delete [] ppi[n1]}delete [] ppi

2. 三维动态数组(int[2][3][4])

///////////////////////////////////////////////////////////////////

int n1, n2, n3

const int DIM1 = 2

const int DIM2 = 3

const int DIM3 = 4

// 构造数组

int ***ppi = new int**[DIM1]

for(n1 = 0n1 <DIM1n1++){ppi[n1] = new int*[DIM2]

for(n2 = 0n2 <DIM2n2++){ppi[n1][n2] = new int[DIM3]}}// 填充数据

for(n1 = 0n1 <DIM1n1++){for(n2 = 0n2 <DIM2n2++){for(n3 = 0n3 <DIM3n3++){ppi[n1][n2][n3] = n1 * 100 + n2 * 10 + n3}}}// 输出for(n1 = 0n1 <DIM1n1++){for(n2 = 0n2 <DIM2n2++){for(n3 = 0n3 <DIM3n3++){afxDump <<"ppi[" <<n1 <<"][" <<n2 <<"][" <<n3 <<"] = "

<<ppi[n1][n2][n3] <<"\n"}}}// 释放数组

for(n1 = 0n1 <DIM1n1++){for(n2 = 0n2 <DIM2n2++){delete [] ppi[n1][n2]}delete [] ppi[n1]}delete [] ppi

如果是一维的,

int *arr//可以是其它类型 (char, float...)

arr = new int[n]//n 必须是整型变量

二维的呢,这样来,

int **arrint n,mcin n marr = new int*[n]上面的代码就可以通过动态输入n,m来实现二维数组的定义


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11636173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存