6

I had a job interview for probation(? I'm not sure if that's the word) and interviewer asked me to tell him what are the differences between structure and class.

So I told him everything I knew and everything that I've read on msdn.

And the guy said "not enough", I didn't have a clue. So he said:

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

Okay. So I was like - didn't hear about that.

But then, after the interview I was thinking about it and it doesn't really make sense to me. It would mean, that the struct size differs while we're changing the value of some variable in it. And it can't really be in the same place in memory, what if there is a collision while expanding. And we would have to write somewhere what bits we're skiping, not sure if it would give any optimization.

Also, he asked me at the begging what is the difference in struct and class in Java. To which I have answered, that there is no struct in Java and he said "Not for programmers, but Numeric types are structures" I was kind of like WTF.

Basically the question is:

Did this guy know something, which is very hard to get to know (I mean, I was looking for it on the web, can't find a thing)

or maybe he doesn't know anything about his job and tries to look cool at the job interviews.

user3212350
  • 401
  • 1
  • 6
  • 18
  • 2
    The guy certainly seems to like to showoff. For `difference between class and structure', check out this: http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net Everything you need to know is written in the won answer. – Kamil T Jun 03 '14 at 09:54
  • Thanks Kamil. I know what the difference is. I was prepared. I told everything I did read about it. But that thing with skiping bites, I can't find it anywhere so I was interested, if it is true at all – user3212350 Jun 03 '14 at 09:56
  • 4
    @user3212350 That guy was high as a kite. `int`s and `float`s are always 32 bit wide. – dcastro Jun 03 '14 at 09:57
  • To be honest, I don't really see how 'checking if bytes are identical and allocating them to same memory' could ever be more efficient than 'have two variables in their own place in memory'. And judging by those 'Numerics are structures' thing, I think that the guy was an 'old-proffessor who always knows the best' person... – Kamil T Jun 03 '14 at 09:59
  • About the Java part: the JVM pre-allocates `Integer` objects (not `int` primitives) for some range (I think 0-100), so that when you create 2 `Integer` objects for e.g. the value `1`, they reference to the same actual Object. see http://stackoverflow.com/questions/3131136/integers-caching-in-java Maybe he was referring to this... – king_nak Jun 03 '14 at 10:02
  • When you create two Integer objects, you always have two Integer objects. The caching is so `Integer.valueOf` can return the same object twice. – user253751 Jun 03 '14 at 10:04
  • @king_nak I think he was actually referring to primitive types (e.g., `int`), having value-type semantics in Java - even though programmers can't define their own value-types. I think he's right on that one, but the explanation could have been a little clearer. – dcastro Jun 03 '14 at 10:07
  • 1
    @dcastro: still, “Numeric types are structures” makes no sense at all. – Holger Jun 03 '14 at 10:11
  • @Holger Yeah, his explanation was crap. He was definitely showing off, he's not qualified to interview other people. – dcastro Jun 03 '14 at 10:14
  • I think he has gone bizarre... – Sunil Jun 03 '14 at 10:31

2 Answers2

4

The guy seems to be confused about the StructLayoutAttribute that can be applied to C# structs. It allows you to specify how the struct's memory is laid out, and you can, in fact, create a struct that has fields of varying types that all start at the same memory address. The part he seems to have missed is that you're going to only use one of those fields at a time. MSDN has more info here. Look at the example struct TestUnion toward the bottom of the page. It contains four fields, all with FieldOffset(0). If you run it, you can set an integer value to the i field and then check the d field and see that it has chnaged.

WarrenG
  • 3,084
  • 16
  • 10
  • Okay, now that is very intersting, although I'm not sure if he was talking about that precisely, it was just a general struct question, so I guess that he meant, that it applies always and from the start Thanks! – user3212350 Jun 03 '14 at 10:27
1

To me it looks like (one of you) was not talking about C# structs/classes but rather about more low-level or more general structs.

There is this special sort of memory optimization used e.g. in

1. C (unions)

and in

2. Pascal (variant records)

see e.g. article How do I translate a C union into Delphi? for an example.

Special form of this "structure" with dynamic polymorphic memory allocation is known as

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

and it was used heavily for inter-process data exchange in OLE automation APIs, in the pre-C# era (for decades in multitude of languages).

4. (s)he might be also talking about structure serialization format vs class in-memory format (see e.g. https://developers.google.com/protocol-buffers/docs/encoding for example of C# structure serialization)

5. you might be also talking about differences of internal JVM memory allocation (see e.g. http://blog.jamesdbloom.com/JVMInternals.html) which reminds me that you might be talking about the class file format and encoding of structs and special numeric literals vs encoding of classes (http://blog.jamesdbloom.com/JVMInternals.html#constant_pool)

So after 5 guesses I believe there is something lost in your translation of your talk with the interviewer and (s)he probably browsed into an area you claimed you know and it turned out you don't. It also might be that (s)he started talking bullshit and checked your reactions. Lying about your skills on the resume is not recommended in any job (e.g. http://www.softwaretestinghelp.com/5-common-interview-mistakes/). I'd vote for the interviewer knew the inteviewing job well enough

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54