# Thread: Creating 2D arrays on heap

1. ## Creating 2D arrays on heap

If we have to create a single dimension array on heap, and then deallocate the memory assigned to it, the code would be somthing like this...
Code:
```char *array=new char[50];
//....
delete array;```
Similarly I need to create a 2D array on heap, and deallocate the memory using double pointers. Please help me out.

2. Am I going crazy? Didn't we just have a thread just like this a few days ago?

3. ## Re: Creating 2D arrays on heap

Originally posted by sundeeptuteja
Code:
```char *array=new char[50];
//....
delete array;```
Should actually be
Code:
```char *array=new char[50];
//....
delete[] array;```

4. you could search the board here. There are numerous posts on this topic. In general the rule is you need to use the delete [] command if you use the new[] command.

char * array;
//this may be a pointer to a single char or represent an array of
//char. we can't say on the basis of this information alone

array = new char[80];
//now we can say it is an array of 80 char

delete [] array;
//here we delete the memory for the 80 char in the array but the pointer, array still exits and isn't pointing at anything now. That is dangerous. Unless your program is closing immediately after the deletion you should assign NULL to array as below to stop array from dangling.

array = NULL;

Since a 2d array is an array of arrays you can do this:

char ** array;
//can't say if this is a pointer to a pointer to a single char or a 2D array yet

int x = 90;
int y = 33;

array = new char * [x];
/*Now there are 90 pointers to char arrays in array. But we have no idea what size each of the char arrays in array are. So we have to declare memory for each of the arrays, and each array must have the same size memory. Therefore using a loop seems reasonable:
*/

int i;
for(i = 0; i < x; i++)//do the body of this loop x times
{
array[i] = new char[y];
//each pointer in array is assigned memory for an array of 33 char.
}

and to delete the memory allocated we reverse the process:

for(i = 0; i < x; i++)
{
delete [] array[i];
//delete the memory for each 33 char array in array first
}
//then delete the memory to the array of 90 pointers
delete [] array;

//again array still exists after the deletions. Only the memory pointed to by array has been deleted. So NULL it out if your program isn't stopping immediately.

5. Elad, I tried your method. My problem is that everytime I try to use this technique to create an array of strings, and then try to sort the strings in alphabetical order, I get an access violation error. Why is that?

6. Originally posted by sundeeptuteja
Elad, I tried your method. My problem is that everytime I try to use this technique to create an array of strings, and then try to sort the strings in alphabetical order, I get an access violation error. Why is that?
1-One reason could be that you tried to access an index that did not have a string in it.
2-another reason could be that you have gone past the boundaries of the array of strings.
3-finally, you could have just overwritten the string. are you accounting for null termination when you allocate them?