1. ## Dynamic, multi-dimensional pointers!

Hi!

I think I'm beginning to figure these pointy things out:

Code:
```int loop;
int x = 5;
int y = 2;

// create first subscript
long **group_total;
group_total = malloc (x * sizeof (long));

if (!group_total)
{
printf ("\n\nError!  Not enough memory!\n\n");
}

//create second subscript
for (loop = 0; loop < x; loop++)
{
group_total[loop] = malloc (y * sizeof (long));

if (!group_total[loop])
{
printf ("\n\nError!  Not enough memory!\n\n");
}
}

//free pointer
for (loop = 0; loop < x; loop++)
{
free (group_total[loop]);
}```
I was just wondering:

1) Is this correct? It compiles and seems to run correctly, but there's no certainty there...

2) Is this everything? ie: Do I need to free the first subscript also?

2. 1) No. See second question.

2) Your number of malloc() statements should equal your number of free() statements. So yes, after you free the second (y) subscripts, you should free the first (x). It must be after, however, for if you free the first subscript first, you will have no way to access the second subscripts and free them.

3. Did I do anything wrong in my declaration?

mw

4. and for the first malloc, the space to be allocated should be of type long *
long **group_total;
group_total = malloc (x * sizeof (long *));

5. >> // create first subscript
>> long **group_total;
>> group_total = malloc (x * sizeof (long));
Close. What you need to create is an array of pointers to long. When you create an array of N elements, you need to allocate N times the size of each element. In this case, that's "x * sizeof(long*)".

And as Jken Veina pointed out, you need to free that array last.

gg

Antigloss is a quick one

Code:
```int loop, loop2;
int x = 5;
int y = 2;

// create first subscript
long **group_total;
group_total = malloc (x * sizeof (long *));

if (!group_total)
{
printf ("\n\nError!  Not enough memory!\n\n");
}

//create second subscript
for (loop = 0; loop < x; loop++)
{
group_total[loop] = malloc (y * sizeof (long));

if (!group_total[loop])
{
printf ("\n\nError!  Not enough memory!\n\n");
}
}

//free pointer
for (loop = 0; loop < x; loop++)
{
for (loop2 = 0; loop2 < y; loop2++)
{
free (group_total[loop][loop2]);
}

free (group_total[loop]);
}```

7. no. it should be
Code:
```for (loop = 0; loop < x; loop++)
{
free (group_total[loop]);

}
free (group_total);//free the space allocated by the first malloc```

8. Sorry 'bout that. ONE MORE TIME... :-)

Code:
```int loop;
int x = 5;
int y = 2;

// create first subscript
long **group_total;
group_total = malloc (x * sizeof (long *));

if (!group_total)
{
printf ("\n\nError!  Not enough memory!\n\n");
}

//create second subscript
for (loop = 0; loop < x; loop++)
{
group_total[loop] = malloc (y * sizeof (long));

if (!group_total[loop])
{
printf ("\n\nError!  Not enough memory!\n\n");
}
}

//free second malloc
for (loop = 0; loop < x; loop++)
{
free (group_total[loop]);
}

//free first malloc
free (group_total);```

9. I haven't put it into my compiler to check it, but it looks like you've got the hang of it so far.

EDIT: If you check a malloc and find there wasn't enough memory, you should probably do something about it instead of continuing to run through the program.

10. Ha! Yeah, good idea! :-)

How do I create a 3D array by adding a "z-axis"?

mw

11. > How do I create a 3D array by adding a "z-axis"?
Then add another loop to malloc each group_total[x][y]

12. So would that be:

Code:
```int loop, loop2;
int x = 5;
int y = 2;
int z = 3;

// create first subscript
long ***group_total;
group_total = malloc (x * sizeof (long *));

if (!group_total)
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1);
}
//create second subscript
for (loop = 0; loop < x; loop++)
{
group_total[loop] = malloc (y * sizeof (long *));

if (!group_total[loop])
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1)
}
}

//create third subscript
for (loop = 0; loop < x; loop++)
{
for (loop2 = 0; loop2 < y; loop2++)
{
group_total[loop][loop2] = malloc (z * sizeof (long));

if (!group_total[loop][loop2])
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1)
}
}
}

//free third malloc
for (loop = 0; loop < x; loop++)
{
for (loop2 = 0; loop2 < y; loop2++)
{
free (group_total[loop][loop2]);
}
}

//free second malloc
for (loop = 0; loop < x; loop++)
{
free (group_total[loop]);
}

//free first malloc
free (group_total);```
I changed the second malloc to:
group_total[loop] = malloc (y * sizeof (long *));

Is that right?

mw

13. but the first should be malloc(x * sizeof (long **))

14. And, you could combine the two loops.
Code:
```int loop, loop2;
int x = 5;
int y = 2;
int z = 3;

// create first subscript
long ***group_total;
group_total = malloc (x * sizeof (long **));

if (!group_total)
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1);
}

//create second subscript
for (loop = 0; loop < x; loop++)
{
group_total[loop] = malloc (y * sizeof (long *));

if (!group_total[loop])
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1)
}

//create third subscript
for (loop2 = 0; loop2 < y; loop2++)
{
group_total[loop][loop2] = malloc (z * sizeof (long));

if (!group_total[loop][loop2])
{
printf ("\n\nError!  Not enough memory!\n\n");
exit (1)
}
}
}

for (loop = 0; loop < x; loop++)
{
//free third malloc
for (loop2 = 0; loop2 < y; loop2++)
free (group_total[loop][loop2]);

//free second malloc
free (group_total[loop]);
}

//free first malloc
free (group_total);```

15. Thanks guys! Your help has been extremely efficient!

mw