Array of value types

D

Dennis Myrén

Hi.

I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;


Please give me some thoughts about this.

Thank you!
 
R

Richard Blewett [DevelopMentor]

If this is the actual situation, you can change

STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

to

STRUCT [] a = new STRUCT [0xA];

as each element of the array will be a struct initialized as if the defauly constructor had run - you can read my blog entry at [1] for the gory details of value type initialization

If this isn't the situation, then yes you will get a copy, but any chance of posting code that is closer to the ral situation so we can advise more concretely

[1] http://staff.develop.com/richardb/weblog/PermaLink.aspx/309ff21d-5824-405c-a45c-b73640f24314

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hi.

I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;


Please give me some thoughts about this.

Thank you!



--
Regards,
Dennis JD Myr?n
Oslo Kodebureau



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
J

Jon Skeet [C# MVP]

Dennis Myrén said:
I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.

Yes.
This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

Does it really need to be a structure then?
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;

Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Please give me some thoughts about this.

I suggest the first thing to do is check whether you *really* need it
to be a struct rather than a class. I personally very rarely find a use
for defining my own value types.
 
D

Dennis Myrén

Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only be
applied
to structures, but as i tested making it a class now, it still compiles
without errors.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good answer)?
I suggest the first thing to do is check whether you *really* need it
to be a struct rather than a class.
I guess it really does not have to be a structure.
I will go for a class i think.

Thank you Jon for the response on this one.



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value
types?
I guess it is.
Yes.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

Does it really need to be a structure then?
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;

Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Please give me some thoughts about this.

I suggest the first thing to do is check whether you *really* need it
to be a struct rather than a class. I personally very rarely find a use
for defining my own value types.
 
J

Jon Skeet [C# MVP]

Dennis Myrén said:
Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only
be applied to structures, but as i tested making it a class now, it
still compiles without errors.

I wouldn't like to say whether you can still do it, to be honest.
Interop is far from a specialty of mine :(

However, if you have an array of references, you *probably* won't be
able to pass that as an array of that type by interop, because the data
won't be in the right place. Ask in the interop group for more
information.

On the other hand, if you're going to pass all of this via interop
anyway, then it's probably not going to be that much of an issue doing
the in-memory copy to start with.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good answer)?

I don't know of anything like that - it's just a case of balancing the
convenience of copying the whole structure in one go with the
increasingly marginal performance benefits of using indexing.
 
D

Dennis Myrén

Thank you Jon, for helping me out.

By now, i think i will just copy the struct to the array and leave it there.
It works, and after all, that is most important.
And, as you said, convinience is also a factor.


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only
be applied to structures, but as i tested making it a class now, it
still compiles without errors.

I wouldn't like to say whether you can still do it, to be honest.
Interop is far from a specialty of mine :(

However, if you have an array of references, you *probably* won't be
able to pass that as an array of that type by interop, because the data
won't be in the right place. Ask in the interop group for more
information.

On the other hand, if you're going to pass all of this via interop
anyway, then it's probably not going to be that much of an issue doing
the in-memory copy to start with.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good
answer)?

I don't know of anything like that - it's just a case of balancing the
convenience of copying the whole structure in one go with the
increasingly marginal performance benefits of using indexing.
 
D

Dennis Myrén

Just one more question regarding value types;
consider this snippet:
foreach (IFDENTRY e in ifd.entries)
{
....
}

Given that IFDENTRY is a structure, and ifd.entries is a field of type
System.Array
of type IFDENTRY, is this causing a copy as well for each item in the array?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only
be applied to structures, but as i tested making it a class now, it
still compiles without errors.

I wouldn't like to say whether you can still do it, to be honest.
Interop is far from a specialty of mine :(

However, if you have an array of references, you *probably* won't be
able to pass that as an array of that type by interop, because the data
won't be in the right place. Ask in the interop group for more
information.

On the other hand, if you're going to pass all of this via interop
anyway, then it's probably not going to be that much of an issue doing
the in-memory copy to start with.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good
answer)?

I don't know of anything like that - it's just a case of balancing the
convenience of copying the whole structure in one go with the
increasingly marginal performance benefits of using indexing.
 
R

Richard Blewett [DevelopMentor]

Yes

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Just one more question regarding value types;
consider this snippet:
foreach (IFDENTRY e in ifd.entries)
{
...
}

Given that IFDENTRY is a structure, and ifd.entries is a field of type
System.Array
of type IFDENTRY, is this causing a copy as well for each item in the array?

--
Regards,
Dennis JD Myr?n
Oslo Kodebureau
Dennis Myr?n said:
Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only
be applied to structures, but as i tested making it a class now, it
still compiles without errors.

I wouldn't like to say whether you can still do it, to be honest.
Interop is far from a specialty of mine :(

However, if you have an array of references, you *probably* won't be
able to pass that as an array of that type by interop, because the data
won't be in the right place. Ask in the interop group for more
information.

On the other hand, if you're going to pass all of this via interop
anyway, then it's probably not going to be that much of an issue doing
the in-memory copy to start with.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good
answer)?

I don't know of anything like that - it's just a case of balancing the
convenience of copying the whole structure in one go with the
increasingly marginal performance benefits of using indexing.

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
D

Dennis Myrén

OK. Thanks Richard.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Richard Blewett said:
Yes

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Just one more question regarding value types;
consider this snippet:
foreach (IFDENTRY e in ifd.entries)
{
...
}

Given that IFDENTRY is a structure, and ifd.entries is a field of type
System.Array
of type IFDENTRY, is this causing a copy as well for each item in the
array?

--
Regards,
Dennis JD Myr?n
Oslo Kodebureau
Dennis Myr?n said:
Does it really need to be a structure then?
Well, i thought so...
The structure directly represents a chunk of a binary file.
I use:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
to explicitly specify that the order is essential.
I use System.Runtime.InteropServices.Marshal class
to create the structure instance from a byte array.
Quite embarrasing, I was sure the StructLayout attribute could only
be applied to structures, but as i tested making it a class now, it
still compiles without errors.

I wouldn't like to say whether you can still do it, to be honest.
Interop is far from a specialty of mine :(

However, if you have an array of references, you *probably* won't be
able to pass that as an array of that type by interop, because the data
won't be in the right place. Ask in the interop group for more
information.

On the other hand, if you're going to pass all of this via interop
anyway, then it's probably not going to be that much of an issue doing
the in-memory copy to start with.
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you're going to set any significant
number of properties, wouldn't you be better off with a copy anyway?
Just out of interest, where would the breakpoint(array index accesses)
be where to rather do a copy than working with array indices
(i can test this by myself though, just if you already know a good
answer)?

I don't know of anything like that - it's just a case of balancing the
convenience of copying the whole structure in one go with the
increasingly marginal performance benefits of using indexing.

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
J

Jon Skeet [C# MVP]

Dennis Myrén said:
Just one more question regarding value types;
consider this snippet:
foreach (IFDENTRY e in ifd.entries)
{
...
}

Given that IFDENTRY is a structure, and ifd.entries is a field of type
System.Array
of type IFDENTRY, is this causing a copy as well for each item in the array?

Yes. It has to, partly because the value of "e" is on the stack,
whereas the array is on the heap.
 

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