7

Structs are a composite data structure in the C programming language; they consist of primitives such as ints and pointers all placed in memory in an adjacent fashion, such as an array.

My question is, what are structs themselves made out of? Are they a type of array? For example, a hash table can be implemented as an array of linked-lists. In a similar way, what is a struct implemented as? If need be, please explain at the x86 assembly level. Thank you.

JFMR
  • 23,265
  • 4
  • 52
  • 76
the_endian
  • 2,259
  • 1
  • 24
  • 49
  • 1
    0s and 1s... this is a bit too broad as it is. – cs95 Aug 29 '17 at 06:10
  • Not like an array, as far as I know. An array is contiguous, but structs may contain padding. – Ilja Everilä Aug 29 '17 at 06:10
  • 1
    They aren't implemented "in the language itself". A C implementation uses a structure definition as a guide to access a piece of memory (in an implementation defined manner). After translation, any notion of human readable "structures" is gone. – StoryTeller - Unslander Monica Aug 29 '17 at 06:12
  • Made in memory from space for each member, close together, potential padding in between. Made in code by usual linking, i.e. replace name by fixed adress, add offset based on member name, access based on knowledge of width off each member separatly. – Yunnosch Aug 29 '17 at 06:14
  • I don't know if there's a standard that dictates exactly how a structure should look like in the assembly level, but with all major c compilers(MSVC, clang, gcc), the variables are laid out one after the other, similar to an array. In fact, if you have a struct with only a single type of variable, you wouldn't be able to tell that it's a struct and not an array on the assembly level. – savram Aug 29 '17 at 06:19
  • @savram so the struct itself must be metadata which basically states "make sure all of the above members are bound together in memory?" To the compiler – the_endian Aug 29 '17 at 06:21
  • 1
    They are not metadata, they are just data. You can look at it just as an abstraction, the same way arrays are. You only say something is an array if it makes sense for it to be, because of the operations performed on it, the access that is made. But you could very well just say they are different variables. In the assembly level there are in fact just variables. – savram Aug 29 '17 at 06:24
  • 1
    Wrong duplicate again. C and C++ are different languages and therefore the object layout has different rules altogether – Antti Haapala -- Слава Україні Aug 29 '17 at 09:25

1 Answers1

2

At assembly level Structure boils down to a address accessed by offset corresponding to structure member.

Depending on the alignment rule and storage class memory is allocated for instance of structure.

Example:

struct A
{
  int a,
  char b
}a1;

In above case if you write a1.b = 5 its assembly equivalent would be something:

MOV 5 TO ADDRESS OF a1 + 4 //assuming integer size is 4

Vagish
  • 2,520
  • 19
  • 32
  • 1
    Good explanation. The next level of complication is that compilers are allowed to optimize away `struct` variables, same as they can for other objects. The struct doesn't have to have an address; the member values could just be held in registers even if it's not optimized away. (e.g. after inlining a function that returns a struct by value). – Peter Cordes Aug 29 '17 at 07:04