Pavan Teja
Pavan Teja

Reputation: 3202

How does a compiler resolve addresses of structure members?

I am little bit confused about the memory organization of a structure compared to an array. Array elements can be accessed by memory address of the first element in an array and offset to desired index. Now how is the address of a structure member evaluated by the compiler?

struct name
{
    int a;
    float b;
};

int main()
{
    struct name *ptr,pt,p;

    p.a=4;
    p.b=4.5;
    ptr=&pt;
    ptr->a=5;
    ptr->b=10.5;
    return 0;
}

How does the compiler know, where to store value of member a in a structure variable p and how does the compiler evaluate the offset and address for member b

In the second case, ptr contains a reference to the structure variable pt. How does the compiler know the memory addresses of the members.

Upvotes: 1

Views: 617

Answers (5)

hackitect
hackitect

Reputation: 141

The symbol table that the compiler uses, has information about each element in a struct such as size and data type. coz structs are statically alloc'd, elements are packed together and their offsets are determined by each elements size. The address of the struct is the address of its first element

ptr->b=10.5; is an implicit pointer arithmetic using address of struct and size of element 'a' to determine location of element 'b'.

Upvotes: 1

user207421
user207421

Reputation: 310903

How does the compiler know the memory addresses of the members.

Because it allocates them in the first place. It's entirely the compiler's decision. In the symbol table, each member is accompanied by its type, its size, and its offset from the start if the struct.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

At compile time, the compiler knows the size of a struct and the offsets to its members. For your struct name, the compiler figures out the following information.

  1. Size of struct name
  2. The layout of the member data
  3. The offset of a, which is zero and offset of b, which is non-zero.

The layout of struct name might look something like:

   <--- Size of struct ---------------->

   +-----------------+-----------------+
   |                 |                 |
   +-----------------+-----------------+

   ^
   |
   Address of ptr

   ^                 ^
   |                 |
   offset of a (0)   offset of b (non-zero)

Given the address a pointer points to a struct name, the compiler knows exactly how much to offset to get to member a and how much to offset to get to member b.

Upvotes: 6

Sameera
Sameera

Reputation: 304

When you run this, program allocates 8 byte for p (4 for int, 4 for float). Address of member 'a' begins from the address of p. In second case you are directly assigning the address of pt to ptr, that means you are assigning the address of 'a' to ptr.

Upvotes: 1

Cramer
Cramer

Reputation: 1795

The compiler knows the memory of the struct's members because the compiler organises the struct itself. It figures out which bits should go where. Whenever a member is accessed the compiler needs to know the entire struct's definition, this is why, so it knows where the bits and pieces are. As to what the actual offsets are, well that depends on how the compiler wants to do it.

If you NEED to know the offsets, read about offsetof here.

Upvotes: 2

Related Questions