how to dinamically allocate memory for two dimensional array?

P

Pawel_Iks

Let's consider one dimensional array, which is defined like below:
int N=200;
int *tab=new int[N];
// do something with tab
delete tab;

I'd like to know why can I do the same for two dimensional array.
Please help me.


greetings, Pawel
 
K

Kavya

Pawel_Iks said:
Let's consider one dimensional array, which is defined like below:
int N=200;
int *tab=new int[N];
// do something with tab
delete tab;

It should be delete[] tab;
I'd like to know why can I do the same for two dimensional array.

int **ptr= new int*[N];
for(int i=0; i<N;++i)
ptr=new int[N];

for(int i=0; i<N;++i)
delete[] ptr;
delete[] ptr;
 
W

White Wolf

Pawel_Iks said:
Let's consider one dimensional array, which is defined like below:
int N=200;
int *tab=new int[N];
// do something with tab
delete tab;

That should read:
delete [] tab;
I'd like to know why can I do the same for two dimensional array.

int n=200, m=100;
int *tab=new int[n*m];
// do something with tab
delete []tab;

But I wonder: why don't you use std::vector?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top