473,396 Members | 1,784 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,396 software developers and data experts.

3D Arrays

OK, brain freeze (don't laugh). Stupid question, but how do I dynamically
declare and initialize a 3-D array in C++? Here's what I want:

Array with 2000 elements
|
|--------Array with 38 elements
|
|-----------Array of 256 TCHARs

It's supposed to represent 2000 rows of 38 columns, each a 256-wchar_t
string.

Thanks.
Sep 15 '06 #1
6 1542

Mike C# wrote:
OK, brain freeze (don't laugh). Stupid question, but how do I dynamically
declare and initialize a 3-D array in C++? Here's what I want:

Array with 2000 elements
|
|--------Array with 38 elements
|
|-----------Array of 256 TCHARs

It's supposed to represent 2000 rows of 38 columns, each a 256-wchar_t
string.

Thanks.
If you can use Boost (www.boost.org), your best bet would be :
boost::multi_array<wchar_t, 3MyArray (boost::extents[2000][38][256]);

Otherwise, you should use std::vector :
typedef <std::vector<std::vector<std::vector<wchar_t ArrayType;
ArrayType MyArray (2000);
//double loop to resize internal vectors....

Of course, there is also the "C-style" definition, but it is
error-prone, especially if you must pass the array as a function
parameter, since you will then loose size information :

wchar_t MyArray [2000][38][256];

Arnaud
MVP - VC

Sep 15 '06 #2

<ad******@club-internet.frwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
If you can use Boost (www.boost.org), your best bet would be :
boost::multi_array<wchar_t, 3MyArray (boost::extents[2000][38][256]);

Otherwise, you should use std::vector :
typedef <std::vector<std::vector<std::vector<wchar_t ArrayType;
ArrayType MyArray (2000);
//double loop to resize internal vectors....
Great! I need a little further guidance: once I implement either of these
options, how do I pass the arrays to ODBC functions that are expecting
C-style arrays of strings?
Of course, there is also the "C-style" definition, but it is
error-prone, especially if you must pass the array as a function
parameter, since you will then loose size information :

wchar_t MyArray [2000][38][256];
Hmm. Do I really want to increase the stack size to accomodate the 38 MB
required for this non-dynamic method? As mentioned, I want to "dynamically"
declare and initialize a 3-D array (i.e., "new" and "delete"). Since I'm
calling ODBC functions that are using these arrays, and they are expecting
the "error-prone C-style" arrays, I'd prefer to use them.

Thanks.
Sep 15 '06 #3

Mike C# wrote:
<ad******@club-internet.frwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
If you can use Boost (www.boost.org), your best bet would be :
boost::multi_array<wchar_t, 3MyArray (boost::extents[2000][38][256]);

Otherwise, you should use std::vector :
typedef <std::vector<std::vector<std::vector<wchar_t ArrayType;
ArrayType MyArray (2000);
//double loop to resize internal vectors....

Great! I need a little further guidance: once I implement either of these
options, how do I pass the arrays to ODBC functions that are expecting
C-style arrays of strings?
If you need C-style array binary compatibility, std::vector won't work
: You need to use boost::multi_array or std::valarray (but I believe
valarray API is more difficult to grasp).

To access the raw data of a multi_array, use multi_array::data.
Of course, there is also the "C-style" definition, but it is
error-prone, especially if you must pass the array as a function
parameter, since you will then loose size information :

wchar_t MyArray [2000][38][256];

Hmm. Do I really want to increase the stack size to accomodate the 38 MB
required for this non-dynamic method? As mentioned, I want to "dynamically"
declare and initialize a 3-D array (i.e., "new" and "delete"). Since I'm
calling ODBC functions that are using these arrays, and they are expecting
the "error-prone C-style" arrays, I'd prefer to use them.
Can't you declare the object either as static-duration, either as
member of a class?

Arnaud
MVP - VC

Sep 15 '06 #4

<ad******@club-internet.frwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
If you need C-style array binary compatibility, std::vector won't work
: You need to use boost::multi_array or std::valarray (but I believe
valarray API is more difficult to grasp).

To access the raw data of a multi_array, use multi_array::data.
I don't have Boost, and I checked on it: I cannot install/use Boost library
here. The std::valarray doesn't appear to return a C-style array (not
negotiable).
Can't you declare the object either as static-duration, either as
member of a class?
The object? static-duration? I must be dense this morning, cause I'm not
seeing how that gets me from point A (no array) to point B (a 39x200x256
three-dimensional dynamic whcar_t array)?

I'm calling ODBC functions here and they require C-style arrays. Here's a
link to the function I'm trying to work with now:
http://msdn.microsoft.com/library/de...dparameter.asp.
I have no intention of re-writing the SQL Server ODBC drivers to make them
work with std::vector, std::valarray or any other C++ objects or templates.

I'm attempting to use the SQL_PARAMETER_BIND_BY_COLUMN binding type, which
requires two C-style arrays for each parameter: one C-style array for the
values and a second C-style array for the length/indicator. Since I have 39
parameters, I thought it would be useful to set them up as two 39 column
arrays as opposed to 78 separate arrays. Looks like the best option is to
just bite the bullet and set up 78 separate one-dimensional C-style arrays
and be done with it. Then repeat for the other 13 items that I'm trying to
do parameter binding on.

Thanks.
Sep 15 '06 #5
Ray
Mike C# wrote:
OK, brain freeze (don't laugh). Stupid question, but how do I dynamically
declare and initialize a 3-D array in C++? Here's what I want:

Array with 2000 elements
|
|--------Array with 38 elements
|
|-----------Array of 256 TCHARs

It's supposed to represent 2000 rows of 38 columns, each a 256-wchar_t
string.
You mean something like this?

wchar_t*** pointerTo2000ElementArray = new wchar_t**[2000];
for(int i = 0; i < 2000; i++) {
wchar_t** pointerTo38ElementArray = new wchar_t*[38];
for(int j = 0; j < 38; j++) {
pointerTo38ElementArray[j] = new wchar_t[256];
}
pointerTo2000ElementArray[i] = pointerTo38ElementArray;
}
>
Thanks.

Sep 17 '06 #6
Yep, that's the one. Thanks Ray.

"Ray" <ra********@yahoo.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Mike C# wrote:
>OK, brain freeze (don't laugh). Stupid question, but how do I
dynamically declare and initialize a 3-D array in C++? Here's what I
want:

Array with 2000 elements
|
|--------Array with 38 elements
|
|-----------Array of 256 TCHARs

It's supposed to represent 2000 rows of 38 columns, each a 256-wchar_t
string.

You mean something like this?

wchar_t*** pointerTo2000ElementArray = new wchar_t**[2000];
for(int i = 0; i < 2000; i++) {
wchar_t** pointerTo38ElementArray = new wchar_t*[38];
for(int j = 0; j < 38; j++) {
pointerTo38ElementArray[j] = new wchar_t[256];
}
pointerTo2000ElementArray[i] = pointerTo38ElementArray;
}
>>
Thanks.

Sep 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.