Multi dynamic array with diferent dimenzions help

Hello, I'm having problems reading members of my array...
error while reading some memory locations....

this code should create 3D array where each second and third dimenzion has diferent number of pointers (dimenzions) for each array.
size of each dimenzion/pointer array will be choosen by user...

if somebody has lilte time that will be great :) (comments will explain you details.)
here's code:

------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

int main () {
	int first, second, third; //DIMENZIONS
	short i, j, k; //FOR LOOPS

	cout << "(prt->ptr->ptr->array) ////->\a";
	cin >> first;
	int ***master = new int** [first]; //master pointer points to first dimenzion ( array of **pointers)
	
		for (i = 0; i < first; i++) {
			cout << "ptr->ptr->array [" << i + 1 << "]  //--->\a";
			cin >> second;
			master [i] = new int* [second]; // create array of pointers to wich "first **pointer dimezion is pointing

			for (j = 0; j < second; j++) {
				cout << "ptr->array [" << i + 1 << "][" << j + 1 << "] member per array /-------->\a";
				cin >> third;
				master [i] [j] = new int [third + 1]; // create final array where last *pointer is pointing
				master [i] [j] [0] = third; //here holding the info of last dimenzion required for reading memory

				/*INITIALIZE MEMBERS WITH 0 EXCEPT FIRST MEMBER WICH HOLD THE INFO ABOUT THIRD DIMENZION 
				REQUIRED FOR READING MEMORY */
				for (k = 1; k <= third; k++) // <---- k = 1 skiping first !!!
					master [i] [j] [k] = 0;

			}
		}

	cout << endl << "------------------------------" << endl << "READING MEMBERS..." 
		<< endl << "------------------------------\a" << endl;

	//READING MEMBERS... HERE POBLEM OCURS :)  CANOT READ SOME MEMBERS
	short x = 0, y = 0, z = 1; // declaring variables before because first  for (...)  needs the vlue 'y' 
	for (; x < master [x] [y] [0]; x++) // <----here it is ( static short wont work as well)
		for (; y < master [x] [y] [0]; y++)
			for (; z <= master [x] [y] [0]; z++)
				cout << master [x] [y] [z] << endl;

	
		//DELETING POINTERS
		for (int i = 0; i <= third; i++)
			for (int j = 0; j < second; j++)
				delete [] master [i] [j];

					for (int i = 0; i < first; i++)
						delete [] master [i];

							delete [] master;

		cout << endl << "---------------------------" << endl << "DELETED!" << endl
			<< "-----------------------------\a" << endl << endl;

	system ("pause");
		return 0;
}
Last edited on
common someone...
this should be easy for alot of members :)
Arrays are supposed to be more "semetrical" then what you are trying to do here. I'm not sure I actually understand what you are trying to do, but if you don't mind clarifying it I will try to help.
I believe the reading code should look like this:

1
2
3
4
5
6
7
8
9
10
    //READING MEMBERS...
    //short x = 0, y = 0, z = 1;     //don't need this
     for (int x=0; x < first; x++) 

        for (int y=0; y < second; y++)

            for (int z=1; z <= master [x] [y] [0]; z++)
            {
                cout << master [x] [y] [z] << endl; 
            }


I haven't looked at the delete part yet - but that doesn't look right either

But like Computergeek01 , I assume you have your reasons for this setup
Last edited on
Hi sasanet,

yes, firstly you can declare the shorts 'i', 'j' and 'k' local to their loops since they are only required within their respective function scope and it is then easier not to get them mixed up with the 'i', 'j' and 'k' you declare during deletion of your pointers.

Your 3D array has the potential to be very asymmetrical, and the length of each of the int*[] arrays is determined by (a potentially) different value of 'second', defined by the user. This is also true of the int[] pointed to by each of the elements of the aforementioned int*[] array, with each int[] array having a user defined length equal to 'third' (again each int[] can be of different length).

Hence, when you come to delete the pointers you need to know the respective values of 'third' AND 'second' that the user entered for each int[] and int*[] array respectively. If not, then you will either get segmentation faults from assuming the array is too big or resource leaks from assuming it is too small.

So, like 'third' you need to store 'second' somehow (e.g., make the first element of each int*[] point to a 1 element int[] containing 'second') and recall its value come deletion.

Hope that helps, good luck!
@Computergeek01,
master ***POINTER should point to diferent numbers of **POINTERS
each **POINTER should point to diferent numbers of *POINTERS
each *LASTPOINTER should point to diferent numbers of members. (point to his final array)
each FIRST MEMBER of each *LASTPOINTER should have record on how many members are in THIS array.

@guestgullkan
wont work :(
I've entered 5 member total but only 3 has benn readed.

@dangrr888
I think you're right... cos I was trying to do that before but i'ts imposble
someone know how to do that?

Computergeek has ask what I'm I trying to do.
please look at this example it will show that my problem is posible when trying to do same thing with 2D array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main () {
	short **twoDArray;
	short i, rows, columns;

	cout << "enter how many rows: ";
	cin >> rows;
	twoDArray = new short* [rows];

	for (i = 0; i < rows; i++) {
		cout << "how many columns in THIS --> [" << (i + 1) << "] ROW";
		cin >> columns;  // how many columns for each row!
		twoDArray [i] = new short [columns + 1];
		twoDArray [i] [0] = columns;

		for (short j = 1; j <= columns; j++)
			twoDArray [i] [j] = 0;
	}
	cout << "output of members: " << endl;
	for (i = 0; i < rows; i++)
		for (short j = 0; j <= twoDArray [i] [0]; j++)
			cout << "[" << i << "][" << j << "] = " << twoDArray [i] [j] << endl;

	for (i = 0; i < rows; i++)
		delete [] twoDArray [i];
	delete [] twoDArray;

	system ("pause");
	return 0;
}


so the correct answer is: I wanna do the same thing with 3D array.
is that posible?
I mean it is posible but problem is how to read all the members corectly and dysplay them since the array is very nonsirmtrical.
Last edited on
you can't do it like that. On line 21 of your original post you store the information for the z direction. But you don't have any information for the x/y direction anymore. Line 36/37 are taking the z value which is simply the wrong value.

Better do it with a 1 dimensional array like so:

1
2
3
4
5
6
7
8
9
int *a = new int[x_dim * y_dim * z_dim + 3];
a[0] = x_dim;
a[1] = y_dim;
a[2] = z_dim;

void SetValue(int *a, int x, int y, int z, int value)
{
  a[3 + x * a[0] * y * a[1] + z] = value;
}
Not tested. Correct me if I'm wrong
I think the best solution for this is to set each array dimension for their maximum possible size then drop a terminating character into the "End Point" of each dimension and\or it's sub branches.

This doesn't actually accomplish what you are trying to do but if you're trying to fool somebody then wrap it all into a DLL and fake it ;D!
@Computergeek01
I'll try use DLL option once I get to that chaper :)

@coder777
that's intersting

***************
I'm early beginer to C++, I've learned second example from my book so I was just interesting if it is posible to create 3D array with diferent dimenzions in same way.

however, hope I'll newer need 3D array to use in this way :)

tnx all.
Last edited on
Topic archived. No new replies allowed.