Dyn. alloc 2 dim float array not same as float[4][4]??

Started by
9 comments, last by HellRiZZer 22 years, 5 months ago
I was recently trying to make a 2 dim array for use with glMap2f(OpenGL) and making all its values same as the array float[4][4][3]. E.g I made typedef float glVertex3fv[3]; Then, I do: glVertex3fv **Grid; Then I allocate 2 dim array as 4x4 Grid = new glVertex3fv*[4]; for (int i=0;i<4;i++) Grid = new glVertex3fv[4]; And here I just assign all values of float[4][4][3] to Grid[x][y][0,1,2]. When I go to glMap2f and pass it not as &float[0][0](starting array value), but as (float *)&Grid[0][0], it draws some buggy things, not the patch I had with float[4][4][3].. Why? Someone help please? Thanks.
Advertisement
FIY, dont use multidimensional array for 3d programs..its too slow.

to declare 4x4 marix array try to use linear array
such as matrix[16];

(always anonymous)
I know its slow, but most OpenGL functions use pointers to arrays, e.g glVertex3fv declared as
glVertex3fv(float *);
quote:Original post by HellRiZZer
most OpenGL functions use pointers to arrays,


And you just answered your own question. What your doing is creating an array of pointers, then filling those pointers with pointers to each row of the array. there''s no way that you can gaurentee that all the memory for the values will be allocated in a sequential uninterupted order. Therefore the first row of what your passing will be valid but unless the second row was allocated immediately after it the second row will be garbage.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
So, you are suggesting that if I got array of x=4, y=4, xyz=3, then I have to allocate it like:

Grid = new float[x*y*xyz];

Is that right?
Yes, that''s what he means and it should work (it does for me ).

[Resist Windows XP''s Invasive Production Activation Technology!]
Er, how much slower are multi-dim arrays??? I have an engine I''m working on that uses them for vertex arrays, is that going to be bad? Should I change them to linear? Can I? Optimization is very important, therefore if it makes a huge difference I need to change it. I never heard this before.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Dynamically allocated multidimensional arrays take more memory and in general are ''slower'' than their singledimensional counterparts. Why? Well, look at this:

This array: float **Array;
Points to this: [Memory Internal] float *, float *, float *, float *
And those point to these:
1. [Memory Internal] float, float, float, float
2. [Memory Internal] float, float, float, float
3. [Memory Internal] float, float, float, float
4. [Memory Internal] float, float, float, float

To access [1][1] you have to get the array at index 1, then jump to the memory address is points to and then get index 1 from that array. The memory internal is a 4 byte or so chunk help in RAM to allow for the memory to be freed (basically just the size of the allocated memory).

This array: float *Array;
Points to this: [Memory Internal] float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float

To access [1][1] you have to get the index at 5. Done. So, for arrays with the same width and height there''s almost no reason to use a multidimensional array. They take longer to allocate/free and can potentially take much longer to index.

[Resist Windows XP''s Invasive Production Activation Technology!]
Another good thing about single dimensional arrays: since their elements are consecutive in memory you can use pointer incrementing and memcpy with them. You can also pass them to OpenGL, which you can''t with a multidimensional array .

[Resist Windows XP''s Invasive Production Activation Technology!]
But I HAVE been passing multi-dim arrays to opengl vertex arrays with perfect results. And how much slower is is? Is is signifigantly slower? Also, is is possible to pass a linear array to opengl vertex arrays? I thought it was only
var[num_vertex][3];
but you can do
var[num_vertex*3];
???


Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"

This topic is closed to new replies.

Advertisement