# Thread: 2D Dynamically allocated pointer arrays

1. Does that code set up a 2D array correctly?

You're right, I don't really understand dynamic memory very well and I'm trying to learn.

mw

2. No. It just creates a single dimension array. That's what you were doing, and like I said, free wasn't the problem. Look at the bottom of page one, where someone asked me how to allocate one. You've been shown how already. You're just ignoring what everyone's been telling you.

You will of course have to free the memory in reverse of how you allocate it. (Free each row's data, then free the main pointer.)

Quzah.

3. Could you explain that example?

Code:
```char **twodee;
int numofrows = 5, numofcols = 10, x;

twodee = malloc( sizeof( char * ) * numofrows );
for( x = 0; x < numofrows; x++ )
twodee[ x ] = malloc( sizeof( char ) * numofcols );```
I don't understand the use of 2 asterisks in the first line.

Also, when you use the first malloc you have "char *", and in the second malloc you have "char" (without the asterisk). Why is that?

mw

PS: Just to clarify: the "rows" is the first subscript and the "columns" is the second subscript, right?

4. Originally Posted by Lionmane
I don't understand the use of 2 asterisks in the first line.
It is a pointer to a pointer, used to allocate an array of pointers.
Originally Posted by Dave_Sinkula
It is usually best to allocate an array of pointers, and then initialize each pointer to a dynamically-allocated ``row.'' Here is a two-dimensional example:
Code:
```#include <stdlib.h>

int **array1 = (int **)malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
array1[i] = (int *)malloc(ncolumns * sizeof(int));```
(In real code, of course, all of malloc's return values would be checked.)

5. Thanks Dave. I looked at that website, but I didn't follow what it was trying to say.

mw

6. Originally Posted by Lionmane
Thanks Dave. I looked at that website, but I didn't follow what it was trying to say.
From my first introduction to clc's FAQ to the present there have been and may yet be some things that I don't quite fully follow either. The more I learn, the more I find out that what is there is very succinctly put. These days I heed the advice first, and do my damndest to understand what may be confusing as I progress.

7. What you don't realize is that I've ALREADY heeded the advice twice (ie: I've already created 2 programs that use pointers and work correctly), but I have since discovered that I've been using them wrong.

Now I'm trying my DAMNDEST to find out how they work, but you guys seem to be more interested in insulting me or are too lazy to explain them. Don't get me wrong: I could care less about what you guys THINK of me, I'm just keeping this thread alive until I can find someone who's NOT too lazy or arrogant to actually explain to me in a way I can understand.

Quzah says I've been ignoring him, but he doesn't realize that when I ignore his insulting manner it will inevitably filter out useful information.

mw

8. You have now successfully manage to annoy two very knowledgable people who have tried to help you. This thread is closed.