C # memory structure optimization?

I had an interview for an internship ("I'm not sure this word is) and the interviewer asked me to tell him what the differences are between structure and class.

So, I told him everything that I knew, and everything that I read in msdn.

And the guy said "not enough," I had no idea. Therefore he said:

The structure is optimized, so if there are integer and float that have some bites, then they save this space, so struct with int=0 and float=0 is half the size of int=int.MAX , float=float.MIN .

Good. So I thought - did not hear about it.

But then, after the interview, I thought about it, and it makes no sense to me. This would mean that the size of the structure is different when we change the value of any variable in it. And it cannot really be in the same place in memory that, if there is a collision during expansion. And we would need to write some bits that we skip, I'm not sure that this will give any optimization.

He also asked me to ask what is the difference in structure and class in Java. To which I replied that in Java there is no structure, and he said: "Not for programmers, but for numeric types - structures." I was like WTF.

Basically the question arises:

Did this guy know something that is very difficult to find out (I mean, I searched him on the Internet, I can’t find anything)

or maybe he knows nothing about his work and is trying to look cool in an interview.

+6
source share
2 answers

The guy seems to be confused with StructLayoutAttribute , which can be applied to C # structures. This allows you to specify how structured memory will be laid out, and you can create a structure that has fields of different types that start with the same memory address. The part that he seems to have missed is that you are going to use only one of these fields at a time. MSDN has more details here . Take a look at the example struct TestUnion at the bottom of the page. It contains four fields, all with FieldOffset(0) . If you run it, you can set the integer value in field i , and then check field d and see that it has been executed.

+4
source

It seems to me that (one of you) was not talking about C # structs / classes, but rather about a lower level or more general structs .

This special memory optimization is used, for example. in

1. C ( unions )

and in

2. Pascal ( variant records )

see, for example, article How do I translate a C connection into Delphi? for example.

A special form of this “structure” with dynamic polymorphic memory distribution is known as

3. http://en.wikipedia.org/wiki/Variant_type

and it was used to exchange data between processes in the OLE Automation API in the era of previous C # (for decades in many languages).

4. (s) he can also talk about a serialization structuring format vs class in-memory format (see, for example, https://developers.google.com/protocol-buffers/docs/encoding , for example, for serializing a C # structure )

5. , you can also talk about differences in the JVM internal memory allocation (see, for example, http://blog.jamesdbloom.com/JVMInternals.html ), which reminds me that you can talk about class file format and structure encoding and special numeric literals, not about class coding ( http://blog.jamesdbloom.com/JVMInternals.html#constant_pool )

So, after 5 guesses, I believe that something has been lost in your translation in your translation of your interview with the interviewer, and (a) he probably looked at the area that, as you claimed, you know, it turned out, etc. Perhaps also that he started talking shit and checked your reactions. Lying about your skills in a resume is not recommended in any job (for example, http://www.softwaretestinghelp.com/5-common-interview-mistakes/ ). I voted for an interviewer who knew intuition work well.

+1
source

Source: https://habr.com/ru/post/970232/


All Articles