Why can't I use Marshal.SizeOf() to calculate the size of an instance of type 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
add a comment |
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 '18 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 '18 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 '18 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 '18 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 '18 at 1:14
add a comment |
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 '18 at 0:55
TheGeneral
28.4k63365
28.4k63365
asked Nov 20 '18 at 0:54
mikevgmikevg
234
234
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 1:14
2
2
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 '18 at 0:54
Sounds pretty definitive to me. what is the actual problem you are trying to solve
– TheGeneral
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 1:14
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
0
active
oldest
votes
0
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.
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 '18 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 '18 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 '18 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 '18 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 '18 at 1:14