473,396 Members | 1,789 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.

using managed arrays ???

Hi,

1) I find the notation for managed arrays in C++.NET very confusing :

Sometimes is it not necessary to use the pointer notation ==>
short pS2 __gc[] = new short __gc[MAX];
Sometimes it is !!! (aparently when declaring an array of managed types) ==>
String* pString[] = new String*[MAX];

Why this difference in notations ?

2) Then, I try to allocate an array of custom managed objects but can't get
the syntax right. I get a runtime error.
__gc class Date
{
public:
Date () {
m_mo = m_da = m_yr = 0;
}
void Date :: Display () {
Console::WriteLine("{0}/{1}/{2}", m_mo.ToString(), m_da.ToString(),
m_yr.ToString());
}
private:
int m_mo, m_da, m_yr;
};

void main()
{
Date pdate __gc[] = new Date __gc[MAX]; --> COMPILER ERROR

Date* pdate __gc[] = new Date* __gc[4]; --> OK BUT ....
pdate[0]->Display(); --> .... RUNTIME
ERROR
}

What is the correct use ?

Many thanks
Chris
Nov 17 '05 #1
5 1070
Hello Chris,
Date* pdate __gc[] = new Date* __gc[4]; --> OK BUT ....
pdate[0]->Display(); --> ....
RUNTIME
ERROR
}


Your call only initializes the ARRAY! It does NOT create instances of
objects!!! For this you need to call new again!
Date* pdate __gc[] = new Date* __gc[4];
pdate[0] = new Date;
pdate[0]->Display();

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Nov 17 '05 #2
> Sometimes is it not necessary to use the pointer notation ==>
short pS2 __gc[] = new short __gc[MAX];
Sometimes it is !!! (aparently when declaring an array of managed types) ==>
String* pString[] = new String*[MAX];

Why this difference in notations ?


because short is a value type and String isn't?
First creates a array of shorts, second an array of handles to String.
Same as in native c++ really.
Managed types are under the control of the garbage collector. How could
you possibly just put them into an array? You can't create them on the
stack either. The syntax is getting nicer in framework version 2.0 btw.

--
Ben
http://bschwehn.de
Nov 17 '05 #3
Hi,

Jochen said I need to do the following :
Date* pdate __gc[] = new Date* __gc[4];
pdate[0] = new Date;


Ok, so I guess then that this is because the first line only creates an
array of Date-pointers and the second line makes sure that the pointers
(well the first one at least) point to a valid object, as in 'normal' C++.

But why then don't I need these two steps when working with String as in :
String* pString[] = new String*[MAX];
I can immediately use an object :
pString[0].ToString();

Ben says because String is a reference type :
is it because when creating the array of String that he automatically
creates the objects as well ?

but why not so with the array of Date then ?

Many thanks
Chris
"Ben Schwehn" <b.*******@gmx.net> wrote in message
news:O0**************@TK2MSFTNGP12.phx.gbl...
Sometimes is it not necessary to use the pointer notation ==>
short pS2 __gc[] = new short __gc[MAX];
Sometimes it is !!! (aparently when declaring an array of managed types) ==> String* pString[] = new String*[MAX];

Why this difference in notations ?


because short is a value type and String isn't?
First creates a array of shorts, second an array of handles to String.
Same as in native c++ really.
Managed types are under the control of the garbage collector. How could
you possibly just put them into an array? You can't create them on the
stack either. The syntax is getting nicer in framework version 2.0 btw.

--
Ben
http://bschwehn.de

Nov 17 '05 #4
> String* pString[] = new String*[MAX];
I can immediately use an object :
pString[0].ToString();

Are you sure about this?
Because I immediately get an NullReferenceException when trying this:

#include "stdafx.h"

using namespace System;

int _tmain()
{
String* pString[] = new String*[100];
pString[0]->ToString();
}
--
Ben
http://bschwehn.de
Nov 17 '05 #5
oops ! you're right !

I've overlooked code I had :

String* pString[] = new String*[MAX];

for (i=0; i<MAX; i++)
pString[i] = String::Format("String {0}", i.ToString()); --> this
line

String *str = pString[0]->ToString(); --> and indeed (since it's a
pointer)

I get it now, as with the array of date-objects : I create an array of (e.g.
Date-) pointers but then do I need to fill the array with addresses of (e.g.
Date-)objects.
I got confused by the line I overlooked.

thnx
Chris

"Ben Schwehn" <b.*******@gmx.net> wrote in message
news:uJ*************@tk2msftngp13.phx.gbl...
String* pString[] = new String*[MAX];
I can immediately use an object :
pString[0].ToString();

Are you sure about this?
Because I immediately get an NullReferenceException when trying this:

#include "stdafx.h"

using namespace System;

int _tmain()
{
String* pString[] = new String*[100];
pString[0]->ToString();
}
--
Ben
http://bschwehn.de

Nov 17 '05 #6

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

Similar topics

1
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is...
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
2
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using a COM DLL in my C# application (a single process). This COM DLL generates a data array, and by using the interop DLL (generated by VS/TlbImp.exe), I'm getting this array to a safe Array...
5
by: Bob Altman | last post by:
Hi all, I'm writing C++/CLI code (compiled with /clr) that accepts a native char array (and its length) and calls a managed routine that expects an array of System::Byte. I was just wondering...
0
by: oleop | last post by:
I am trying to call dll from the managed code.eveyrting works OK until i'm trying to use fucntions that use complex (containing arrays of structures) structures as a parameters. Let's say ...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.