473,763 Members | 6,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Multidimensiona l Arrays not working

2 New Member
Hi there,

I'm working in VC++ 2005 Express, and all I want is to have a dynamic multidimensiona l 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
2 6596
Banfa
9,065 Recognized Expert Moderator Expert
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
Gerbus
2 New Member
Nice, I'll give that a try. Thanks,

Gerbus
Sep 18 '06 #3

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

Similar topics

5
6760
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 clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
6
9523
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 delete command to remove it, or would that be delete ? -- "What are the possibilities of small but movable machines? They may or may not be useful, but they surely would be fun to make."
2
1137
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 Terry
9
6672
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
21
4199
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 possible. The thing I am trying to do is to convert a double value to a string with 8 elements. 8 is fixed because of the files I work with. I will change this 8 character string with the one(8 character string) already in the file and so on. But I...
4
5530
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 accomplish this, but having recently added the ability to load and display 4D data, I've run into some significant performance issues. For a dataset of size 256x176x176x1, it takes about 30 seconds to allocate the array. Loading the data into...
5
10357
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 it somewhere. thanks
24
3132
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*));
9
4502
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 function: x = new int;
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.