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

acessing multi-dimensional array created via new

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/de...w_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?!
Jul 22 '05 #1
6 3338
TrustyTif wrote:
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/de...w_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
Jul 22 '05 #2
TrustyTif wrote:
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/de...w_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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

"TrustyTif" <ti***********@motorola.com> wrote in message
news:ab**************************@posting.google.c om...
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/de...w_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
Jul 22 '05 #4
Andrey Tarasevich <an**************@hotmail.com> wrote in message news:<10*************@news.supernews.com>...
TrustyTif wrote:
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/de...w_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!
Jul 22 '05 #5
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/de.../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
Jul 22 '05 #6
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2i************@uni-berlin.de>...
"TrustyTif" <ti***********@motorola.com> wrote in message
news:ab**************************@posting.google.c om...
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/de...w_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[i] = 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[i];
}

delete []myTwoDimArray;

Hope this helps.

-BG
Jul 22 '05 #7

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

Similar topics

12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
1
by: Philip | last post by:
Hi, first of all I'm new to this whole .net thing so please forgive my ignorance. Now I have a C++ object with me, and I want to be able to call its methods from VB.net. e.g. Foo::Foo { }...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
0
by: ajitpsingh | last post by:
Hi, I recived Error message when acessing OLE Object. "Cannot start the source application for this object" in MS Excel.
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
3
by: swamimeenu | last post by:
hi, I am facing the problem in vb tht if more than one user accessing my program from various systems,if both of them acessing the progam at a same time (ie) for feeding data in it ,at tht time i...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.