What is the difference between these 2 structures

Tyler Durden

There are 2 ways I can define this structure which I wish to pass as an argument to a pinvoke function. I wanted to know what was the difference between the 2

[StructLayout(LayoutKind.Sequential)]
public struct Rect {
   public int left;
   public int top;
   public int right;
   public int bottom;
}   

[StructLayout(LayoutKind.Explicit)]
public struct Rect {
   [FieldOffset(0)] public int left;
   [FieldOffset(4)] public int top;
   [FieldOffset(8)] public int right;
   [FieldOffset(12)] public int bottom;
}

From the definitions of layouts I found here, shouldnt both look the same in memory? Any advantages of one over the other?

Servy

From the definitions of layouts I found here, shouldn't both look the same in memory?

Yes, they will look the same in memory.

Any advantages of one over the other?

One's faster to type out, and is easier to read.


The use of FieldOffset is of course a useful tool; it's not like it's always useless, but if you just so happen to use it to explicitly lay out the fields in the manor in which they would be laid out by default, then it's useless. If you use it to lay out fields in a manor other than their default value (for example, having the overlap, adding padding space, having the underlying representation be in a different order than the declaration order, etc.), then it's not useless.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is difference between Lightsail and EC2?

From Java

What is difference between derive attribute and implementing traits for structures by yourself?

From Java

What is the difference between () and (()) in the control structures?

From Java

What is difference between httpS and http/2?

From Dev

What is the difference between those 2 events in Magento?

From Dev

What's the difference between these 2 ifs?

From Dev

Difference between these Python structures

From Dev

What is the difference between signal(7) and signal(2)?

From Dev

What is the difference between 3/2 and -3/2?

From Dev

What's the difference between these 2 comparisons?

From Dev

What is the difference between component and directive in Angular 2?

From Dev

What is the difference between OnChanges and DoCheck in Angular 2?

From Dev

What is the difference between 'String' and 'Text' in Angular 2?

From Dev

What is the difference between RxJava 2 Cancellable and Disposable?

From Dev

What's the difference between these 2 sorting methods?

From Dev

Difference between similar sparse structures

From Dev

Difference between these Python structures

From Dev

The difference between various typedefs of structures

From Dev

What the difference between 2 name of input?

From Dev

What is the difference between Apache Karaf 2 and 3?

From Dev

difference between 2 structures types in perl

From Dev

what is the difference between >&2 and &>2

From Dev

What is difference between 2 functions

From Dev

What is the difference between these 2 HTTP Get definitions?

From Dev

what is the difference between these 2 MSBuild calls

From Dev

What is the difference between these 2 iptable rules?

From Dev

what is the difference between these 2 sql statements

From Dev

What is the difference between with and if in Python2?

From Dev

What is the difference between these 2 queries (end Result)

Related Related

  1. 1

    What is difference between Lightsail and EC2?

  2. 2

    What is difference between derive attribute and implementing traits for structures by yourself?

  3. 3

    What is the difference between () and (()) in the control structures?

  4. 4

    What is difference between httpS and http/2?

  5. 5

    What is the difference between those 2 events in Magento?

  6. 6

    What's the difference between these 2 ifs?

  7. 7

    Difference between these Python structures

  8. 8

    What is the difference between signal(7) and signal(2)?

  9. 9

    What is the difference between 3/2 and -3/2?

  10. 10

    What's the difference between these 2 comparisons?

  11. 11

    What is the difference between component and directive in Angular 2?

  12. 12

    What is the difference between OnChanges and DoCheck in Angular 2?

  13. 13

    What is the difference between 'String' and 'Text' in Angular 2?

  14. 14

    What is the difference between RxJava 2 Cancellable and Disposable?

  15. 15

    What's the difference between these 2 sorting methods?

  16. 16

    Difference between similar sparse structures

  17. 17

    Difference between these Python structures

  18. 18

    The difference between various typedefs of structures

  19. 19

    What the difference between 2 name of input?

  20. 20

    What is the difference between Apache Karaf 2 and 3?

  21. 21

    difference between 2 structures types in perl

  22. 22

    what is the difference between >&2 and &>2

  23. 23

    What is difference between 2 functions

  24. 24

    What is the difference between these 2 HTTP Get definitions?

  25. 25

    what is the difference between these 2 MSBuild calls

  26. 26

    What is the difference between these 2 iptable rules?

  27. 27

    what is the difference between these 2 sql statements

  28. 28

    What is the difference between with and if in Python2?

  29. 29

    What is the difference between these 2 queries (end Result)

HotTag

Archive