473,513 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

value structs: what do I pass as a parameter to a method so they can be changed?

This is possibly some other problem, but here goes anyway. I created a
'value struct'. I then created a stack semantic instance of it. I then
created a method that takes a pointer to such an instance, and changed one
of its components. I then tried this code, and it didn't change the
instance's component. Code is something like this:

value struct vStruct
{
int x ;
} ;

namespace MyGlobal
{
void Zero_vStruct( vStruct^ vs ) { vs->x = 0 ; }
} ;

void main()
{
vStruct vs ;
vs.x = 58 ;
MyGlobal::Zero_vStruct( %vs ) ;
int vsx = vs.x ; //still 58, not 0 !!!
return 0 ;
} ;

If I change vStruct to a 'ref struct' it works, but then I can't create an
array of such values without it being an array of pointers to such
instances. That is:

typedef array<vStruct> vStruct_Array_A ; //illegal if ref, legal if value
typedef array<vStruct^> vStruct_Array_B ; // always legal

So, I'm asking this: what do I pass to a method as a parameter so I can
change the component values of an instance of a value struct?

For context, I'm using VS C++.NET 2005 using clr/:pure syntax...

Thanks in advance for reponses! : )

[==P==]

Jan 4 '06 #1
2 949
Peter Oliphant wrote:
value struct vStruct
{
int x ;
} ;

namespace MyGlobal
{
void Zero_vStruct( vStruct^ vs ) { vs->x = 0 ; }
} ;

void main()
{
vStruct vs ;
vs.x = 58 ;
MyGlobal::Zero_vStruct( %vs ) ;
int vsx = vs.x ; //still 58, not 0 !!!


What happens there is automatic boxing. Your vStruct is boxed into a
temporary ref class, which is independent from vs. Here's a simpler example:

void main()
{
vStruct vs;
vs.x = 58;
vStruct^ pvs = %vs;
pvs->x = 0;
}

If you try to debug it, you'll see that "pvs" has "[vStruct]" inside,
and x is indeed set to 0, but pvs is an instance completely detached
from vs. When you zero pvs, vs is not getting zeroed.

So what is exactly pvs? It is a ref class that wraps a value class, a
*copy* of vs. So pvs is not a pointer to vs, but a new ref class copy
allocated with gcnew. When you write

vStruct^ pvs = %vs;

the compiler generates something that is roughly equivalent to this:

ref struct vStructWrapper
{
vStruct anonymous;
};

vStructWrapper^ pvs = gcnew vStructWrapper;
pvs->anonymous = vs;

pvs is absolutely not related to vs from that point.

Why is that? Because the caret (^) symbol doesn't mean "the address of a
chunk of memory", but rather it means "a handle to a ref class", and it
must always be a ref class, on the heap, not on the stack.

What is the solution? Use references instead:

void Zero_vStruct( vStruct% vs ) { vs.x = 0; }

This will work fine.

Tom
Jan 4 '06 #2
Peter Oliphant wrote:

Here is one more way of explaining it. Here
void Zero_vStruct( vStruct^ vs ) { vs->x = 0 ; }
vStruct^ is a gabage collected handle.

Here
vStruct vs ;
vs.x = 58 ;


vs is an object on the stack. There's no way to convert a stack object
to a GC handle, so the compiler boxes your value.

You can argue that it's confusing, and that the compiler shouldn't do
automatic boxing. You could say that something like this would be better:

vStrcut vs;
vStruct^ boxed = cli::box_cast<vStruct^>(vs); // imaginary code

and you would prefer if the following erred out:

vStruct^ boxed = %vs;

Although I didn't think this over that much. I think once you get used
to it, it will make sense. I'm sure the compiler team has spent a lot of
time thinking this over, and conculded that automatic boxing was a
reasonable solution.

Tom
Jan 4 '06 #3

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

Similar topics

1
1809
by: Rafael Veronezi | last post by:
Just to fix, correct me if I am wrong... With reference types (objects), if I assign an object to another, the assignment will be the address of the object, and not a copy of it's contents right?...
9
2289
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
10
2174
by: mdh | last post by:
Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; printf("\n\n\nThe value of x is %d\n", x);
13
2507
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7373
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,...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.