Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2007

Tip: Looking for answers? Try searching our database.

Problems returning  an int Array pointer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Oliphant - 20 Sep 2007 18:13 GMT
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[i] ;
}

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 ;
} ;

//-------------------------------------------------------------
Peter Oliphant - 20 Sep 2007 18:19 GMT
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),
[quoted text clipped - 67 lines]
>
> //-------------------------------------------------------------
Peter Oliphant - 20 Sep 2007 19:07 GMT
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==]

> One correction  ( this is typo in previous post, not in code, so this
> doesn't fix problem):
[quoted text clipped - 81 lines]
>>
>> //-------------------------------------------------------------
Ben Voigt [C++ MVP] - 20 Sep 2007 21:30 GMT
> As is typical, after I posted this question I figured out what is wrong.
>
[quoted text clipped - 3 lines]
>
> 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==]
>
[quoted text clipped - 84 lines]
>>>
>>> //-------------------------------------------------------------
Peter Oliphant - 21 Sep 2007 18:52 GMT
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==]

>> As is typical, after I posted this question I figured out what is wrong.
>>
[quoted text clipped - 95 lines]
>>>>
>>>> //-------------------------------------------------------------
Ben Voigt [C++ MVP] - 24 Sep 2007 23:17 GMT
> Thanx Ben!
>
[quoted text clipped - 14 lines]
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.