multi-dimesion pointer array

Started by
24 comments, last by Zakwayda 13 years, 12 months ago
Quote:Original post by WuTz
Here are some examples:
*** Source Snippet Removed ***

Here. Thats all.

the variable "Var" can hold up to 20*20*20*20 floats = 160000 floats.
You can access them like a chessboard.
I think you're confused about how multidimensional arrays work in C++. Your example may compile, and may even appear to work in some cases, but nevertheless it is incorrect.

Here's a little test program you can try:

#include <iostream>int main(){    float* Var = new float[20,20,20,20];        Var[2,7,4,9] = 100;    Var[3,7,4,9] = 200;        std::cout << Var[2,7,4,9] << std::endl;    std::cout << Var[3,7,4,9] << std::endl;    delete Var;}

If your method is correct, the output should be:
100200
However, if you run this, I think you'll see the following:
200200
I think what's throwing you off here is the behavior of the comma operator in C++, which is a little counterintuitive. (If you're using this method to create multidimensional arrays in your *own* code, you may want to revisit that ;).

Also, even if this were to work, it would need to be:
delete [] Var;
Not:
delete Var;
Advertisement
Quote:Original post by jyk
I'll go ahead and mention multi_array.


It's an excellent idea of using boost. I'm sure it would definitely work well.
But, if anyone knows the pointer stuff, please share. I found these things are really confusing.
delete[] ah, yeah, I know :) I just wrote it right out of my head.

I didn't used any multi dimensional arrays in my current project, but I did in older ones. And THERE it just worked good! Maybe because I just used two dimensional arrays?

I will test it again!
Quote:But, if anyone knows the pointer stuff, please share. I found these things are really confusing.
CaspianB's example is close, but I don't think it's entirely correct.

In any case, a simple 2-d example might look like this (typed into post, so no guarantee of correctness):
int** a = new int*[HEIGHT];for (int i = 0; i < HEIGHT; ++i) {    a = new int[WIDTH];}// ...for (int i = 0; i < HEIGHT; ++i) {    delete [] a;}delete [] a;
The code for a 4-d array would be similar, just much more verbose (and more error-prone!).

This type of multidimensional array is often called a 'jagged array', because the rows/columns don't necessarily have to be the same length (although they are in the above example). Using a jagged array where uneven row/column lengths are not needed has a few disadvantages (such as potential fragmentation), so when all rows/columns/etc. are to be the same length, it's more common to allocate a single block of memory and use 1-d indexing, e.g.:
int* a = new int[WIDTH * HEIGHT];// ...a = 0;</pre>Usually you would wrap this up in a class in order to make indexing more straightforward (e.g. by using a convenience function) and to make it possible to automate the management of dynamic memory. This is basically what <tt>multi_array</tt> does for you - it's a wrapper around a single contiguous block of memory that manages the details of allocation and deallocation for you, and provides indexed access via the <tt>[]</tt> operator (by using 'proxy' types, indexing of the form <tt>m<span style="font-weight:bold;">[j][k]</tt> is supported). Another common way to handle the indexing is to overload the <tt>()</tt> operator.<br><br>Note that I more or less ignored the fact that the last dimension in your array is fixed, but &#111;nce you switch over to using a single contiguous block of memory (which I recommend), that becomes more or less irrelevant.
So. I tested this code:

#include <iostream>using namespace std;int main(int argc, char* argv[]){	//int* Test = new int[10,10,10];	int Test[10][10][10];	int x,y,z;	int Count=0;	for(x=0;x<10;x++)	{		for(y=0;y<10;y++)		{			for(z=0;z<10;z++)			{				Test[x][y][z]=Count;				Count++;			}		}	}	for(x=0;x<10;x++)	{		for(y=0;y<10;y++)		{			for(z=0;z<10;z++)			{				cout << Test[x][y][z] << " "; 			}			cout << "\n";		}		cout << "\n";	}	//delete[] Test;	return 0;}


Works as it should! No problems!
It counts all numbers from 0 to 1000.

Maybe you note that I commented the Pointer+new variant. I will test more to get this working.
Also, I did not know after this long time, that I have to use [x][y][z], but now I do again.


So, when this works without being a pointer, it has to work even pointerish...

@jyk: I prefer this method, too when I have to use multidimensional arrays. But it isn't as simple as that what I said above (When dealing with it un-dynamic).

EDIT: Damn! Don't rate me down for this! Maybe it fails with pointers (Or it does not, I don't know) but it WORKS with non pointers! So whats the reason for this??? Test my code BEFORE doing such things like that!

[Edited by - WuTz on April 20, 2010 11:30:04 AM]
Wutz, now try creating a three dimensional array using your method where you prompt the user for the different dimensions then create an array based on those dimensions.

You're not fulfilling the dynamic sized array that the original poster was asking for.

[edit]
Quote:CaspianB's example is close, but I don't think it's entirely correct.


I tested it... You're right. I had the order of my indexes incorrect, and was getting seg-faults. I edited the above post and tried it and it seems to be working fine... Or, at least, no segfaults. :)
I know that, and I said that above. Maybe somehow it works, but I don't know. I just thought it is the same as with satic multi arrays. My fail.
But not a reason to lower my user rating THAT low. :(

I just wanted to help! And I'm not a bad coder, but everytime I want to help, someone lowers my rating! :(

Sorry, I did not know that with the [x,y,z] and pointers! Good that I know that now! But please, my user rating is so low, that everyone thinks that I'm a rowdy that can't code! :(

Maybe I should go into another forum and start again... :( I really like GameDev. :(
Quote:Original post by WuTz
I know that, and I said that above. Maybe somehow it works, but I don't know. I just thought it is the same as with satic multi arrays. My fail.
But not a reason to lower my user rating THAT low. :(

I just wanted to help! And I'm not a bad coder, but everytime I want to help, someone lowers my rating! :(

Sorry, I did not know that with the [x,y,z] and pointers! Good that I know that now! But please, my user rating is so low, that everyone thinks that I'm a rowdy that can't code! :(

Maybe I should go into another forum and start again... :( I really like GameDev. :(


I appreciate your effect. Btw, I didn't lower your ratings. I just want to know the solution of the problem.
Maybe you think back to me, if you have to use Static MultiArrays :)
So, in some wired ways my postings were good. :)

Thx!
WuTz
The problem is that you are obviously a beginner (at least in C++), but you keep being the "smart guy", giving advices about topics you don't know much about and in a intractably way.
That is just annoying.

Sometimes I give advices about topics I'm not pro at. But At least I state that it's just "IMHO", and I'm not insist to my opinion like I was a pro in it.
But you keep repeating the same shit again and again, even if it's clearly proven wrong.

I haven't rated you down (yet), but my fingers itch.

This topic is closed to new replies.

Advertisement