473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems returning an int Array pointer

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

//-------------------------------------------------------------
Sep 20 '07 #1
3 1752
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" <po*******@roun dtripllc.comwro te in message
news:eM******** *******@TK2MSFT NGP04.phx.gbl.. .
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 ;
} ;

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


Sep 20 '07 #2
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" <po*******@roun dtripllc.comwro te in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
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" <po*******@roun dtripllc.comwro te in message
news:eM******** *******@TK2MSFT NGP04.phx.gbl.. .
>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 ;
} ;

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



Sep 20 '07 #3

"Peter Oliphant" <po*******@roun dtripllc.comwro te in message
news:OK******** ******@TK2MSFTN GP03.phx.gbl...
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.
Sep 24 '07 #4

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

Similar topics

14
2107
by: Dave | last post by:
Hello all, Can anybody help with the problem below? I'm trying to define a conversion function that converts objects to function pointers and am getting a compile error on the line indicated below. The compiler interprets this is a function returning a function, which, of course is illegal... What is the correct syntax to accomplish this? Thanks, Dave
5
3103
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
10
10294
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
41
3826
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
10
3170
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
3
1851
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do next is: return, from funktion, pointer value to array were positions ( of found char) is stored. and print that array from main. but I only manage to print memory adress to...
17
3260
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
9
1886
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I want to create inside that function a matrix (i.e. int a) and than passing the reference to it I can simply returning that value: in main()
3
5311
by: O.B. | last post by:
Below is a program that shows a test for marshaling data from a byte array to a class structure. Unfortunately, there are two annoying problems (bugs?) that I can't seem to get around. The first is that if I set the Articulation array to an offset of 22 in the EntityState class instead of 24, I get the following error. The thing is, it is properly aligned. And the amount of data to be copied is smaller than the index of 22 anyway. ...
0
9389
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
10149
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
10003
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...
0
9828
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7370
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
5271
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
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.