473,242 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Dynamic Multidimensional Arrays not working

Hi there,

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

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;

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;


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
2 6565
9,065 Expert Mod 8TB
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];
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
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
  4.     Ver[0] = new float[2*5];
  5.     Ver[1] = Ver[0] + 5;
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
Nice, I'll give that a try. Thanks,

Sep 18 '06 #3

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

Similar topics

by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
by: Gregory L. Hansen | last post by:
I'm sure I saw this mentioned somewhere, but I can't find it. How can I dynamically allocate a multi-dimensional array in C++? My next question, if this gets figured out, is if I should use the...
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
by: Gregory.A.Book | last post by:
I'm working with displaying and manipulating very large image sets. The program handles anything from 2D images to 4D RGB volumes in a time-series. I've been using dynamically allocated arrays to...
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do...
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.