Why can't I use Marshal.SizeOf() to calculate the size of an instance of type ValueTuple?
up vote
0
down vote
favorite
Why does the following code:
ValueTuple<double, double> origin = (0.0, 0.0);
Console.WriteLine($"Size of ValueTuple<double, double>: {Marshal.SizeOf(origin)}");
throw System.ArgumentException: 'Type
'System.ValueTuple`2[System.Double,System.Double]' cannot be marshaled
as an unmanaged structure; no meaningful size or offset can be computed.'
c# .net valuetuple
add a comment |
up vote
0
down vote
favorite
Why does the following code:
ValueTuple<double, double> origin = (0.0, 0.0);
Console.WriteLine($"Size of ValueTuple<double, double>: {Marshal.SizeOf(origin)}");
throw System.ArgumentException: 'Type
'System.ValueTuple`2[System.Double,System.Double]' cannot be marshaled
as an unmanaged structure; no meaningful size or offset can be computed.'
c# .net valuetuple
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Unless you're actually planning to send something into the unmanaged world,Marshal.SizeOf
may as well be replaced withRandom.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.
– Damien_The_Unbeliever
Nov 20 at 7:10
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
1
To avoid LOH limit, you need managed size of T, not marshalled size of T returned byMarshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.
– Ňuf
Nov 20 at 21:38
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Why does the following code:
ValueTuple<double, double> origin = (0.0, 0.0);
Console.WriteLine($"Size of ValueTuple<double, double>: {Marshal.SizeOf(origin)}");
throw System.ArgumentException: 'Type
'System.ValueTuple`2[System.Double,System.Double]' cannot be marshaled
as an unmanaged structure; no meaningful size or offset can be computed.'
c# .net valuetuple
Why does the following code:
ValueTuple<double, double> origin = (0.0, 0.0);
Console.WriteLine($"Size of ValueTuple<double, double>: {Marshal.SizeOf(origin)}");
throw System.ArgumentException: 'Type
'System.ValueTuple`2[System.Double,System.Double]' cannot be marshaled
as an unmanaged structure; no meaningful size or offset can be computed.'
c# .net valuetuple
c# .net valuetuple
edited Nov 20 at 0:55
TheGeneral
26.4k63163
26.4k63163
asked Nov 20 at 0:54
mikevg
234
234
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Unless you're actually planning to send something into the unmanaged world,Marshal.SizeOf
may as well be replaced withRandom.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.
– Damien_The_Unbeliever
Nov 20 at 7:10
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
1
To avoid LOH limit, you need managed size of T, not marshalled size of T returned byMarshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.
– Ňuf
Nov 20 at 21:38
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14
add a comment |
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Unless you're actually planning to send something into the unmanaged world,Marshal.SizeOf
may as well be replaced withRandom.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.
– Damien_The_Unbeliever
Nov 20 at 7:10
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
1
To avoid LOH limit, you need managed size of T, not marshalled size of T returned byMarshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.
– Ňuf
Nov 20 at 21:38
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14
2
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Unless you're actually planning to send something into the unmanaged world,
Marshal.SizeOf
may as well be replaced with Random.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.– Damien_The_Unbeliever
Nov 20 at 7:10
Unless you're actually planning to send something into the unmanaged world,
Marshal.SizeOf
may as well be replaced with Random.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.– Damien_The_Unbeliever
Nov 20 at 7:10
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
1
1
To avoid LOH limit, you need managed size of T, not marshalled size of T returned by
Marshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.– Ňuf
Nov 20 at 21:38
To avoid LOH limit, you need managed size of T, not marshalled size of T returned by
Marshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.– Ňuf
Nov 20 at 21:38
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384726%2fwhy-cant-i-use-marshal-sizeof-to-calculate-the-size-of-an-instance-of-type-va%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 at 0:54
Unless you're actually planning to send something into the unmanaged world,
Marshal.SizeOf
may as well be replaced withRandom.Next
. It tells you then size of the unmanaged representation of the type, which is only meaningful if you'll create such a thing, and as it says here, you can't create an unmanaged representation of a valuetuple.– Damien_The_Unbeliever
Nov 20 at 7:10
I'm not attempting to marshal a ValueTuple to unmanaged code. However, the actual motivation is probably controversial. I have my own implementation of IList<T> called BigList<T> that uses a pool of buffers that are small enough to stay off the Large Object Heap. This requires knowing the size in bytes of T. I've been using Marshal.SizeOf() for this purpose.
– mikevg
Nov 20 at 16:00
1
To avoid LOH limit, you need managed size of T, not marshalled size of T returned by
Marshall.SizeOf()
, which can be different (What’s the difference? sizeof and Marshal.SizeOf). Some workarounds here and here. Also remember that array size is not just size_of_T * array_length but array needs some additional memory (to store array length, dimension, object headers) which is also runtime-dependent.– Ňuf
Nov 20 at 21:38
I knew that the "managed" and "unmanaged" sizes of T could be different but my situation is limited to simple value types T and my implementation is not particularly sensitive to errors using Marshal.SizeOf(). My buffers are actually arrays of T and I can tell if they end up on LOH. What I was missing is that I can Emit OpCodes.Sizeof to get the "managed" size. This fixes my problem and I can now create BigList<T> where T is a ValueTuple. Thanks!
– mikevg
Nov 21 at 1:14