Field offsets? #6105
-
C-family languages have a specific macro that allows you to get a byte offset for a certain field in a structure: struct funny_struct {
int padding;
int value;
};
size_t offset = offsetof(struct funny_struct, value); // 4 In terms of C#, Framework provides What it could look like: struct FunnyStruct
{
int Padding;
int Value;
}
// ...
int offset = offsetof(FunnyStruct, Value); // 4 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 23 replies
-
How could |
Beta Was this translation helpful? Give feedback.
-
JIT can't optimize this into a constant though. |
Beta Was this translation helpful? Give feedback.
-
jnm2 said:
aye fair enough |
Beta Was this translation helpful? Give feedback.
-
The reservations expressed here refer to the danger of exposing details of managed memory but there's no reason to assume that this is going to refer to managed memory. We have the Without this operator it is cumbersome at best to determine the offset of a member field and since the padding and alignment are potentially unpredictable it involves making some assumptions or performing pointer subtraction. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
This was a friendly suggestion, made in good faith. Often it's good to just try things out and find out first hand what happens in the scenarios in question. :)
There was no misleading. It's an option, with its own set of caveats (just like all things). That's why i mentioned options to use, suggested you try them, but also pushed heavily that you seek out the best expertise on the topic (multiple times). That's not misleading, that's sharing what is known but also recommending a great path to get the most knowledge possible to take on a challenge :) Good luck! :) |
Beta Was this translation helpful? Give feedback.
jnm2 said:
aye fair enough