By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Dynamic Multidimensional Arrays not working

P: 2
Hi there,

I'm working in VC++ 2005 Express, and all I want is to have a dynamic multidimensional array of type

STRUCT CUSTOMVERTEX {
int x, y, z;
};
Here's my code:

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{

struct CUSTOMVERTEX { // Custom Vertex is simple 3D coordinates
int x,y,z;
};

float Vertic [2][5];
Vertic[0][0] = 1.0f;
Vertic[1][1] = 1.0f;

float** Ver; // Init Ver as a pointer to a pointer to a float
Ver = new float* [2]; // Init Ver as a pointer to a float array of size 2
Ver[0] = new float[5]; // Init Ver[0] as a float array of size 5
Ver[1] = new float[5];

Ver[0][0] = 1.0f;
Ver[1][1] = 2.0f;

CUSTOMVERTEX** Vertices = NULL;
Vertices = new CUSTOMVERTEX*[3];
Vertices[0] = new CUSTOMVERTEX[24];
Vertices[1] = new CUSTOMVERTEX[24];
Vertices[2] = new CUSTOMVERTEX[24];

Vertices[0][0].x = 1;
Vertices[0][1].x = 2;
Vertices[1][0].x = 3;

CUSTOMVERTEX Vert[2][2];

Vert[0][0].x = 1;
Vert[1][1].y = 2;

return 0;
}
Now, I have the 4 above variables in my watches window. It seems that Vert and Vertic form arrays, but Ver and Vertices do NOT.



Help a brother out! Please!
Sep 15 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Now, I have the 4 above variables in my watches window. It seems that Vert and Vertic form arrays, but Ver and Vertices do NOT.
That is just a property of the Watch window it does not mean that the memory has not been allocated.

The watch window has to display the variables by their type Ver and Vertices are of type T ** i.e. pointers to pointers and Vert and Vertic are type T[][] i.e. 2D arrays. The watch window does not know how much memory you have allocated to Ver and Vertices so can not display the as an array.

Also remember that in the array Vertic the memory for Vertic[1][0] is contiguous with Vertic[0][4] (that is there are right next to each other) however with Ver the same is not true Ver[1][0] is like to not be contiguous with Ver[0][4] (although there is a small chance it will be).


I note on declaring variable multi-dimensional arrays, I almost never use this method

Expand|Select|Wrap|Line Numbers
  1.     float** Ver;                // Init Ver as a pointer to a pointer to a float
  2.     Ver = new float* [2];   // Init Ver as a pointer to a float array of size 2
  3.     Ver[0] = new float[5];  // Init Ver[0] as a float array of size 5
  4.     Ver[1] = new float[5];
  5.  
I would do it like this

Expand|Select|Wrap|Line Numbers
  1.     float *Ver;                // Init Ver as a pointer to a float
  2.     Ver = new float[2*5]; // Init Ver a float array of size 2 * 5 = 10
  3.  
There are 2 main reasons.

1. The allocation is much simpler, 1 single allocation and deallocation much much easier to handle and get right.

2. The memory is allocated in the same pattern to what you get if you actually declare a static 2 dimensional float array. Because of this it is much easier to slip this allocation in place instead of a statically declared array. You have less chance that a surupticious memset somewhere will go wrong since you know your array memory is contiguous.

I would then use access it as

Ver[y * MAX_X + x];

This can be encapsulated in a function or class to make it easier.



There are other possibilities, a hybred for instance where you allocate an array of pointers to pointers and a contiguous block of memory and the initialise your array to point at the correct locations in the contiguous block. This has the all advantages of both approaches will out too much overhead.

Expand|Select|Wrap|Line Numbers
  1.     float **Ver;                // Init Ver as a pointer to a float
  2.     Ver = new float *[2];  // Init Ver a float array of size 2 * 5 = 10
  3.  
  4.     Ver[0] = new float[2*5];
  5.     Ver[1] = Ver[0] + 5;
  6.  
Only 2 allocations for any size array, memory is contiguous and can be treated like a statically allocated array. Can use C symantecs for accessing a 2D array.
Sep 15 '06 #2

P: 2
Nice, I'll give that a try. Thanks,

Gerbus
Sep 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.