Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

HellRiZZer

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

This topic is 6177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites
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!]

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!