Problems returning an int Array pointer

P

Peter Oliphant

Below are the definitions of two classes. VList creates two static integer
arrays (v_0, v_1), creates an array of pointers to these arrays (vlist), and
has a public method to return a pointer to the corresponding integer array
based on its index into vlist (Element( index )) .

MyClass contains an instance of VList, and then tries to call Element(1) and
get a pointer to the appropriate integer list (v_1). But what comes out the
debugger says the result is an undefined variable.

What am I doing wrong? Alternately I'd like to return the copied values of
the appropriate int array to an external int array (in contrast to returning
a pointer to the internal int array), but not sure how to do this via a
return from a subroutine.

Note the 'static' aspect to much of VList.

Thanx in advance for any help...

[==P==]

//----------------------------------------------------
#typedef array<int> Int_Array ;
#typedef array<Int_Array> Int_Array_Array ;
//----------------------------------------------------
ref class VList
{
public:
VList(){}
~VList() {}

public:

static Int_Array^ Element( int i )
{
return vlist ;
}

private:

static Int_Array_Array^ vlist = gcnew Int_Array_Array(2)
{
v_0, v_1
} ;

static Int_Array^ v_0 = gcnew Int_Array(2){ 0x01, 0x02 } ;
static Int_Array^ v_1 = gcnew Int_Array(2){ 0x03, 0x04 } ;
} ;

//-------------------------------------------------------

ref class myClass
{
public:

myClass(){}
~myClass(){}

void Sub()
{
Int_Array^ info = m_Vlist.Element( 1 ) ;

int x = info[0] ; // error here - debugger says 'info' is an 'undefined
variable'!
}

private:

VList m_VList ;
} ;

//-------------------------------------------------------------
 
P

Peter Oliphant

One correction ( this is typo in previous post, not in code, so this
doesn't fix problem):

#typedef array<Int_Array> Int_Array_Array ;

should be:

#typedef array<Int_Array^> Int_Array_Array ;

Sorry about that. These things happen when you reduce code and change names
for posting... hehe

[==P==]
 
P

Peter Oliphant

As is typical, after I posted this question I figured out what is wrong.

The problem is that the list of arrays can be created before the arrays in
the list. Thus the list contains undefines. If I make sure I create the
arrays before putting them in the list, it all works out...

That is, it's all in the timimg! :)

[==P==]

Peter Oliphant said:
One correction ( this is typo in previous post, not in code, so this
doesn't fix problem):

#typedef array<Int_Array> Int_Array_Array ;

should be:

#typedef array<Int_Array^> Int_Array_Array ;

Sorry about that. These things happen when you reduce code and change
names for posting... hehe

[==P==]

Peter Oliphant said:
Below are the definitions of two classes. VList creates two static
integer arrays (v_0, v_1), creates an array of pointers to these arrays
(vlist), and has a public method to return a pointer to the corresponding
integer array based on its index into vlist (Element( index )) .

MyClass contains an instance of VList, and then tries to call Element(1)
and get a pointer to the appropriate integer list (v_1). But what comes
out the debugger says the result is an undefined variable.

What am I doing wrong? Alternately I'd like to return the copied values
of the appropriate int array to an external int array (in contrast to
returning a pointer to the internal int array), but not sure how to do
this via a return from a subroutine.

Note the 'static' aspect to much of VList.

Thanx in advance for any help...

[==P==]

//----------------------------------------------------
#typedef array<int> Int_Array ;
#typedef array<Int_Array> Int_Array_Array ;
//----------------------------------------------------
ref class VList
{
public:
VList(){}
~VList() {}

public:

static Int_Array^ Element( int i )
{
return vlist ;
}

private:

static Int_Array_Array^ vlist = gcnew Int_Array_Array(2)
{
v_0, v_1
} ;

static Int_Array^ v_0 = gcnew Int_Array(2){ 0x01, 0x02 } ;
static Int_Array^ v_1 = gcnew Int_Array(2){ 0x03, 0x04 } ;
} ;

//-------------------------------------------------------

ref class myClass
{
public:

myClass(){}
~myClass(){}

void Sub()
{
Int_Array^ info = m_Vlist.Element( 1 ) ;

int x = info[0] ; // error here - debugger says 'info' is an 'undefined
variable'!
}

private:

VList m_VList ;
} ;

//-------------------------------------------------------------

 
B

Ben Voigt [C++ MVP]

Peter Oliphant said:
As is typical, after I posted this question I figured out what is wrong.

The problem is that the list of arrays can be created before the arrays in
the list. Thus the list contains undefines. If I make sure I create the
arrays before putting them in the list, it all works out...

That is, it's all in the timimg! :)

You should use a type initializer (aka static constructor) to enforce the
order of initialization of static members.
[==P==]

Peter Oliphant said:
One correction ( this is typo in previous post, not in code, so this
doesn't fix problem):

#typedef array<Int_Array> Int_Array_Array ;

should be:

#typedef array<Int_Array^> Int_Array_Array ;

Sorry about that. These things happen when you reduce code and change
names for posting... hehe

[==P==]

Peter Oliphant said:
Below are the definitions of two classes. VList creates two static
integer arrays (v_0, v_1), creates an array of pointers to these arrays
(vlist), and has a public method to return a pointer to the
corresponding integer array based on its index into vlist (Element(
index )) .

MyClass contains an instance of VList, and then tries to call Element(1)
and get a pointer to the appropriate integer list (v_1). But what comes
out the debugger says the result is an undefined variable.

What am I doing wrong? Alternately I'd like to return the copied values
of the appropriate int array to an external int array (in contrast to
returning a pointer to the internal int array), but not sure how to do
this via a return from a subroutine.

Note the 'static' aspect to much of VList.

Thanx in advance for any help...

[==P==]

//----------------------------------------------------
#typedef array<int> Int_Array ;
#typedef array<Int_Array> Int_Array_Array ;
//----------------------------------------------------
ref class VList
{
public:
VList(){}
~VList() {}

public:

static Int_Array^ Element( int i )
{
return vlist ;
}

private:

static Int_Array_Array^ vlist = gcnew Int_Array_Array(2)
{
v_0, v_1
} ;

static Int_Array^ v_0 = gcnew Int_Array(2){ 0x01, 0x02 } ;
static Int_Array^ v_1 = gcnew Int_Array(2){ 0x03, 0x04 } ;
} ;

//-------------------------------------------------------

ref class myClass
{
public:

myClass(){}
~myClass(){}

void Sub()
{
Int_Array^ info = m_Vlist.Element( 1 ) ;

int x = info[0] ; // error here - debugger says 'info' is an 'undefined
variable'!
}

private:

VList m_VList ;
} ;

//-------------------------------------------------------------


 
P

Peter Oliphant

Thanx Ben!

I had heard of, but never really paid attention to, a 'static constructor'.
I guess you learn something new everyday... :)

Prompted by your post, I looked 'static constructor' up. I was interested in
how it differed from a 'normal constructor'. I discovered that it is a
constructor that is called only once, and used to intiailize static members.
I believe it is called no matter which actual contructor is used to create
the first instance, and the static constructor is called first. Then again,
the static constructor might be called at application start up, which would
be before ANY instances are created.

What are the exact rules for when a static constructor is called? Is it
called at applicaton start up? At creation of the first instance? At the
creation of an instance when there are currently no instances (this goes to
what happens to the static members if the last existing instance is
destroyed)?

[==P==]


Ben Voigt said:
Peter Oliphant said:
As is typical, after I posted this question I figured out what is wrong.

The problem is that the list of arrays can be created before the arrays
in the list. Thus the list contains undefines. If I make sure I create
the arrays before putting them in the list, it all works out...

That is, it's all in the timimg! :)

You should use a type initializer (aka static constructor) to enforce the
order of initialization of static members.
[==P==]

Peter Oliphant said:
One correction ( this is typo in previous post, not in code, so this
doesn't fix problem):

#typedef array<Int_Array> Int_Array_Array ;

should be:

#typedef array<Int_Array^> Int_Array_Array ;

Sorry about that. These things happen when you reduce code and change
names for posting... hehe

[==P==]

Below are the definitions of two classes. VList creates two static
integer arrays (v_0, v_1), creates an array of pointers to these arrays
(vlist), and has a public method to return a pointer to the
corresponding integer array based on its index into vlist (Element(
index )) .

MyClass contains an instance of VList, and then tries to call
Element(1) and get a pointer to the appropriate integer list (v_1). But
what comes out the debugger says the result is an undefined variable.

What am I doing wrong? Alternately I'd like to return the copied values
of the appropriate int array to an external int array (in contrast to
returning a pointer to the internal int array), but not sure how to do
this via a return from a subroutine.

Note the 'static' aspect to much of VList.

Thanx in advance for any help...

[==P==]

//----------------------------------------------------
#typedef array<int> Int_Array ;
#typedef array<Int_Array> Int_Array_Array ;
//----------------------------------------------------
ref class VList
{
public:
VList(){}
~VList() {}

public:

static Int_Array^ Element( int i )
{
return vlist ;
}

private:

static Int_Array_Array^ vlist = gcnew Int_Array_Array(2)
{
v_0, v_1
} ;

static Int_Array^ v_0 = gcnew Int_Array(2){ 0x01, 0x02 } ;
static Int_Array^ v_1 = gcnew Int_Array(2){ 0x03, 0x04 } ;
} ;

//-------------------------------------------------------

ref class myClass
{
public:

myClass(){}
~myClass(){}

void Sub()
{
Int_Array^ info = m_Vlist.Element( 1 ) ;

int x = info[0] ; // error here - debugger says 'info' is an 'undefined
variable'!
}

private:

VList m_VList ;
} ;

//-------------------------------------------------------------


 
B

Ben Voigt [C++ MVP]

Peter Oliphant said:
Thanx Ben!

I had heard of, but never really paid attention to, a 'static
constructor'. I guess you learn something new everyday... :)

Prompted by your post, I looked 'static constructor' up. I was interested
in how it differed from a 'normal constructor'. I discovered that it is a
constructor that is called only once, and used to intiailize static
members. I believe it is called no matter which actual contructor is used
to create the first instance, and the static constructor is called first.
Then again, the static constructor might be called at application start
up, which would be before ANY instances are created.

What are the exact rules for when a static constructor is called? Is it
called at applicaton start up? At creation of the first instance? At the
creation of an instance when there are currently no instances (this goes
to what happens to the static members if the last existing instance is
destroyed)?

The rule is "no later than the first time any member of the type is used".

In reality, it is at that exact point, just before the runtime loads and
JITs the type the first time. Often, this will be the first time you call a
constructor, but accessing a static member would also cause the type
initializer to run.

But the standard appears ambiguous enough to allow it to run when the
application starts, or when the assembly is first loaded, etc.
 

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