how do I pass int array by reference? 
Author Message
 how do I pass int array by reference?

Sorry to ask such a simple question, but I've searched the documentation and
I cant work out the best solution.

In a managed C++ application, I want to pass a reference to an array of
integers to a method, what is the best way to do this?

Also, I want to be able to return arrays of integers from the method,
without knowing the size before I call the method.

If I have an array like this,

Int32 myarray[] = new Int32[10];

If I then call a method as follows:

myclass->updatedata(myarray);

Will  this be passed by value? I don't want to do this because it may be a
big array and I don't want to copy it, also I want to change both the
elements and the number of elements in the method. I don't necessarily need
the array to be dynamically sized, I don't mind creating a new pointer if
the size need increasing.

I guess I could create my own class to contain the array as follows:

class arrayclass{
  public: Int32 myarray[];

Quote:
}

and then pass this class as follows:
myclass->updatedata(myarrayclass);

By this seems very complicated, I'm sure there must be something built-in to
do this, please could you tell me what I have missed, I am sure I will be
very embarrassed when I find that I have missed a simple solution.

Martin



Tue, 05 Oct 2004 21:21:09 GMT  
 how do I pass int array by reference?
It is like unmanaged C++, you are passing by value a
reference to an object (an array of integers).

Example: (I have not access to the VC++7.0 compiler now,
please forgive any clerical errors)

void fn (Int32 arr[])
{
    arr[0] = 1;

Quote:
}

main ()
{
    Int32 myarray[] = new Int32[10];
    myarray[0] = 10;
    //-- The following line should print "1"
    System.Console.WriteLine (myarray[0]);

Quote:
}

Do you want to return a new array? C++ does not allow you
to return an array (even typedef'd). Managed C++ does not
lift that restriction, I think. So the following syntax do
not work:

Int32 fn ()[]
{
   Int32 ret[] = new Int32[10]; //-- the type of (new Int32
[]) is Int32*, not Int32[]).
   return ret;

Quote:
}

or

typedef Int32 arr[];
arr fn()
{
    arr ret = new Int32[10]; //-- the type of (new Int32
[]) is Int32*, not Int32[]).

Quote:
}

Use some array class from the framework instead of plain
arrays, like System.Collections.ArrayList, or the adequate
class for your problem (several times we try to solve a
problem with plain arrays and develop large programs when
we needed to use stacks, lists, hash tables or b-trees;
even plain structs sometimes do the job...). If you want
to use a plain struct in Managed C++ use the keyword
__value to force it to be a CLR struct and not a true
object if you desire it.

STL has good classes like vector<> etc. too.

Quote:
>-----Original Message-----
>Sorry to ask such a simple question, but I've searched

the documentation and
Quote:
>I cant work out the best solution.

>In a managed C++ application, I want to pass a reference
to an array of
>integers to a method, what is the best way to do this?

>Also, I want to be able to return arrays of integers from
the method,
>without knowing the size before I call the method.

>If I have an array like this,

>Int32 myarray[] = new Int32[10];

>If I then call a method as follows:

>myclass->updatedata(myarray);

>Will  this be passed by value? I don't want to do this
because it may be a
>big array and I don't want to copy it, also I want to
change both the
>elements and the number of elements in the method. I

don't necessarily need
Quote:
>the array to be dynamically sized, I don't mind creating
a new pointer if
>the size need increasing.

>I guess I could create my own class to contain the array
as follows:

>class arrayclass{
>  public: Int32 myarray[];
>}

>and then pass this class as follows:
>myclass->updatedata(myarrayclass);

>By this seems very complicated, I'm sure there must be

something built-in to

- Show quoted text -

Quote:
>do this, please could you tell me what I have missed, I
am sure I will be
>very embarrassed when I find that I have missed a simple
solution.

>Martin

>.



Tue, 05 Oct 2004 22:12:32 GMT  
 how do I pass int array by reference?
Sorry, I have noticed two errors:

void fn (Int32 arr[])
{
    arr[0] = 1;

Quote:
}

main ()
{
    Int32 myarray[10]; //<-- You can not assign a Int32*
to a  Int32[]
    myarray[0] = 10;
    //-- The following line should print "1"
    fn (myarray); //<-- This line was missing...
    System.Console.WriteLine (myarray[0]);

Quote:
}
>-----Original Message-----
>It is like unmanaged C++, you are passing by value a
>reference to an object (an array of integers).

>Example: (I have not access to the VC++7.0 compiler now,
>please forgive any clerical errors)

>void fn (Int32 arr[])
>{
>    arr[0] = 1;
>}

>main ()
>{
>    Int32 myarray[] = new Int32[10];
>    myarray[0] = 10;
>    //-- The following line should print "1"
>    System.Console.WriteLine (myarray[0]);
>}

>Do you want to return a new array? C++ does not allow you
>to return an array (even typedef'd). Managed C++ does not
>lift that restriction, I think. So the following syntax
do
>not work:

>Int32 fn ()[]
>{
>   Int32 ret[] = new Int32[10]; //-- the type of (new
Int32
>[]) is Int32*, not Int32[]).
>   return ret;
>}

>or

>typedef Int32 arr[];
>arr fn()
>{
>    arr ret = new Int32[10]; //-- the type of (new Int32
>[]) is Int32*, not Int32[]).
>}

>Use some array class from the framework instead of plain
>arrays, like System.Collections.ArrayList, or the
adequate
>class for your problem (several times we try to solve a
>problem with plain arrays and develop large programs when
>we needed to use stacks, lists, hash tables or b-trees;
>even plain structs sometimes do the job...). If you want
>to use a plain struct in Managed C++ use the keyword
>__value to force it to be a CLR struct and not a true
>object if you desire it.

>STL has good classes like vector<> etc. too.

>>-----Original Message-----
>>Sorry to ask such a simple question, but I've searched
>the documentation and
>>I cant work out the best solution.

>>In a managed C++ application, I want to pass a reference
>to an array of
>>integers to a method, what is the best way to do this?

>>Also, I want to be able to return arrays of integers
from
>the method,
>>without knowing the size before I call the method.

>>If I have an array like this,

>>Int32 myarray[] = new Int32[10];

>>If I then call a method as follows:

>>myclass->updatedata(myarray);

>>Will  this be passed by value? I don't want to do this
>because it may be a
>>big array and I don't want to copy it, also I want to
>change both the
>>elements and the number of elements in the method. I
>don't necessarily need
>>the array to be dynamically sized, I don't mind creating
>a new pointer if
>>the size need increasing.

>>I guess I could create my own class to contain the array
>as follows:

>>class arrayclass{
>>  public: Int32 myarray[];
>>}

>>and then pass this class as follows:
>>myclass->updatedata(myarrayclass);

>>By this seems very complicated, I'm sure there must be
>something built-in to
>>do this, please could you tell me what I have missed, I
>am sure I will be
>>very embarrassed when I find that I have missed a simple
>solution.

>>Martin

>>.

>.



Tue, 05 Oct 2004 22:45:19 GMT  
 how do I pass int array by reference?
Anonymous

Thanks for your reply, I'm still a bit confused.

Quote:
> It is like unmanaged C++, you are passing by value a
> reference to an object (an array of integers).

In the book 'Microsoft Visual C++ Step be step' it shows the syntax:
Int32 gcArray[] = new Int32[10];
The book does not say so, but can the reference to this object be changed at
a later stage in the program? like this:
gcArray[] = new Int32[20];
If so could this be done inside a class, and will this change be seen
outside the class?

Martin



Wed, 06 Oct 2004 02:39:42 GMT  
 how do I pass int array by reference?
Yes and Yes.

A __gc array is a pointer to the array and that can be repointed to a new
array. The GC will take care of the orphaned one.

Ronald Laeremans
Visual C++ compiler and libraries team


Quote:
> Anonymous

> Thanks for your reply, I'm still a bit confused.

> > It is like unmanaged C++, you are passing by value a
> > reference to an object (an array of integers).

> In the book 'Microsoft Visual C++ Step be step' it shows the syntax:
> Int32 gcArray[] = new Int32[10];
> The book does not say so, but can the reference to this object be changed
at
> a later stage in the program? like this:
> gcArray[] = new Int32[20];
> If so could this be done inside a class, and will this change be seen
> outside the class?

> Martin



Thu, 07 Oct 2004 06:48:12 GMT  
 how do I pass int array by reference?
Hi Martin,

When you pass a array, by default it will be passed by reference.

Best Regards,

Billy Zhang
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net?  http://www.gotdotnet.com



Sat, 09 Oct 2004 15:43:03 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Pass an array as reference

2. Passing String arrays by reference?

3. Passing array objects by reference

4. passing 2d arrays by reference

5. passing multi-dimensional array by reference into a function

6. Passing array by reference

7. Passing arrays of srings by reference

8. passing of pointers to int arrays?

9. pass allocated 2d array by reference

10. passing 2d arrays by reference

11. Passing multiple arrays by reference

12. Passing an int array through argv WAS: my own Very revised aueston

 

 
Powered by phpBB® Forum Software