acessing multi-dimensional array created via new

T

TrustyTif

After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];

(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!
 
P

Petec

TrustyTif said:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];

(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!

#include <vector>
std::vector<std::vector<CString> > your2darray(width,
std::vector<CString>(height));
your2darray[x][y] = "asdf";

Memory management is done automatically.

- Pete
 
A

Andrey Tarasevich

TrustyTif said:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

This doesn't make any sense. A multidimensional array with one or more a
zero-sized dimensions contains no elements. I don't see anything like
that on the linked web page.

If you really want to declare 'myTwoDimArray' as a _pointer_ to
two-dimensional array, you can allocate memory for it as follows

myTwoDimArray = new CString[1][3000][MAX_PARAMS];
// Note that 0 was replaced with 1
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;

But this is rather strange way to do this, to say the least.

You could also do this

myTwoDimArray =
(CString(*)[3000][MAX_PARAMS]) new CString[3000][MAX_PARAMS];
// Only two dimensions are really needed
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] (CString(*)[MAX_PARAMS]) myTwoDimArray;

But this is also as ugly as it gets. And, strictly speaking, this code's
behavior is implementation defined since it relies on
'reinterpret_cast's. (I'll probably burn in hell for writing this one.)

If you really want to stick with built-in arrays, the proper way to do
it is to declare 'myTwoDimArray' as a pointer to a single-dimensional array

CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];
...
// Accessing elements
assert(3 < MAX_PARAMS);
myTwoDimArray[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;

Looks much cleaner, doesn't it?

And, finally, you can make the whole thing to look even more clean if
you use 'std::vector's instead of built-in arrays (see Petec's response).
SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!

See above.
 
J

John Harrison

TrustyTif said:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];

(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!

Actually you have a three dimensional array, count the pairs of square
brackets in the new statement.

You aren't going to be able to allocate a 2D array without understanding
pointers well. So I suggest you get a book and C or C++ and start reading.

On the other hand if you just want some code

CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];

myTwoDimArray[1][1] = "abc";

delete[] myTwoDimArray;

john
 
T

TrustyTif

Andrey Tarasevich said:
TrustyTif said:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];
(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

This doesn't make any sense. A multidimensional array with one or more a
zero-sized dimensions contains no elements. I don't see anything like
that on the linked web page.

If you really want to declare 'myTwoDimArray' as a _pointer_ to
two-dimensional array, you can allocate memory for it as follows

myTwoDimArray = new CString[1][3000][MAX_PARAMS];
// Note that 0 was replaced with 1
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;

But this is rather strange way to do this, to say the least.

You could also do this

myTwoDimArray =
(CString(*)[3000][MAX_PARAMS]) new CString[3000][MAX_PARAMS];
// Only two dimensions are really needed
...
// Accessing elements
assert(3 < MAX_PARAMS);
(*myTwoDimArray)[10][3] = "test";
...
// Deallocating memory
delete[] (CString(*)[MAX_PARAMS]) myTwoDimArray;

But this is also as ugly as it gets. And, strictly speaking, this code's
behavior is implementation defined since it relies on
'reinterpret_cast's. (I'll probably burn in hell for writing this one.)

If you really want to stick with built-in arrays, the proper way to do
it is to declare 'myTwoDimArray' as a pointer to a single-dimensional array

CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];
...
// Accessing elements
assert(3 < MAX_PARAMS);
myTwoDimArray[10][3] = "test";
...
// Deallocating memory
delete[] myTwoDimArray;

Looks much cleaner, doesn't it?

And, finally, you can make the whole thing to look even more clean if
you use 'std::vector's instead of built-in arrays (see Petec's response).
SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!

See above.

Thanks Andrey! Both of your suggestions worked wonders!
 
J

JKop

TrustyTif posted:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang9
8/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];

(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!


Would any sort of variation of the following work?:


(int[x][y]) *p2DArray;

p2DArray = new int[x][y];


p2DArray[3][3] = 4;

Just a thought!


-JKop
 
B

B. Gandhi

John Harrison said:
TrustyTif said:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensional array in C++...but, I
don't know how to use it, for alas, my brain still doesn't understand
pointer very well. Here's the site:

http://msdn.microsoft.com/library/d...n-us/vclang98/html/_pluslang_new_operator.asp

And here's what I've come up with thus far to have a 2 dimensional
array of CStrings.

CString (*myTwoDimArray)[3000][MAX_PARAMS];
myTwoDimArray = new CString[0][3000][MAX_PARAMS];

(p.s. if you need to know what that first [] is on the new statement,
see the above referenced website...even though it barely explains it).

SO! Anyone wanna' tell me how to actually put a value in my 2D
CString array? Or how to delete it once I'm done?!

Actually you have a three dimensional array, count the pairs of square
brackets in the new statement.

You aren't going to be able to allocate a 2D array without understanding
pointers well. So I suggest you get a book and C or C++ and start reading.

On the other hand if you just want some code

CString (*myTwoDimArray)[MAX_PARAMS];
myTwoDimArray = new CString[3000][MAX_PARAMS];

myTwoDimArray[1][1] = "abc";

delete[] myTwoDimArray;

john

A good idea to create a 2d array would be to do as follows:

CString **myTwoDimArray;

myTwoDimArray = new CString*[3000]; //an array of pointers.

for(int i=0;i<3000;i++){
myTwoDimArray = new CString[MAX_PARAMS]; //init each pointer.
}

To use elements in this array:
myTwoDimArray[0][0] = "Str1";
and so on..

To cleanly de-allocate:
for(int i=0;i<3000;i++){
delete []myTwoDimArray;
}

delete []myTwoDimArray;

Hope this helps.

-BG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top