473,804 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 multidimensiona l 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 3357
TrustyTif wrote:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensiona l 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<CStrin g> > your2darray(wid th,
std::vector<CSt ring>(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 multidimensiona l 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 multidimensiona l 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_ca st'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.goo gle.com...
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensiona l 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.sup ernews.com>...
TrustyTif wrote:
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensiona l 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 multidimensiona l 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_ca st'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 multidimensiona l 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_o perator.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.goo gle.com...
After surfing the google & MSDN for a few days I found an MSDN site
that explained how to "new" a multidimensiona l 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
3884
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
6
8186
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
17882
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
1
1429
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 { } void Foo::Display_Message()
5
5773
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 know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about transactions, but a compendium of possible approches to multi user programming would be very appreciated!
7
6141
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 since that would cause an exception in the foreach loop "foreach can not continue because the colelction was modifed". Now what is the least expensive and threadsafe way to make sure that no other threads modifies that collection. Since one of the...
0
1954
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
2332
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 Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
1
9323
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
3
1281
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 am facing a pblm since i had set a primary key in it, so duplicateion of record error.. at the time of updateing the record in a database. plz help me for this.. Thank you,
0
9577
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10569
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
10325
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
10315
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7615
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
6847
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.