using managed arrays ???

C

Chris

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
 
J

Jochen Kalmbach

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
 
B

Ben Schwehn

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.
 
C

Chris

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 said:
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.
 
B

Ben Schwehn

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();
}
 
C

Chris

oops ! you're right !

I've overlooked code I had :

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

for (i=0; i<MAX; i++)
pString = 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 said:
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();
}
 

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

Top