linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* offsets of fields in a structure
@ 2007-10-19 15:27 Giulio Rossato
  2007-10-19 16:30 ` ninjaboy
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Giulio Rossato @ 2007-10-19 15:27 UTC (permalink / raw)
  To: linux-c-programming

Suppose that I define the following structure
struct astruct {
    int field1;
    float field2;
    char field3[10];
};

and declare the variable
struct astruct r;

The amount of memory specified by the structure is not the sum of the 
storage specified by each of its member types. This vary from one 
machine and C compiler to another. On most compuers, objects of certain 
types may not begin anywhere in memory but are constrained to start at 
certain boundaries. For example, an integer of length 4 bytes may have 
to start at an address divisible by 4, and a real number of length 8 may 
have to start at an address divisible by 8. Thus, in my example, if the 
starting address of r is 200, the integer occupies bytes 200 through 
203, but the real number cannot start at byte 204, since that location 
is not divisible by 8. Thus the real number must start at byte 208.
The C compiler associates to each member identifier of a structure an 
offset that specifies how far beyond the start of the structure the 
location of that field is. To calculate the location of a member in a 
structure, the offset of the member identifier is added to the base 
address of the structure variable.
Now the question.
I want write a function that receives as parameters a start address and 
a "description of a structure" and returns the offsets of the fields. 
The structure is not known at compile time. The offsets should be 
calculated at runtime and the code should be independent of the machine.
How should be written this function?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: offsets of fields in a structure
  2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
@ 2007-10-19 16:30 ` ninjaboy
  2007-10-19 17:35 ` LaPoint, Adam W
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: ninjaboy @ 2007-10-19 16:30 UTC (permalink / raw)
  To: Giulio Rossato; +Cc: linux-c-programming

2007/10/19, Giulio Rossato <giulio.rossato@infocamere.it>:
> Suppose that I define the following structure
> struct astruct {
>     int field1;
>     float field2;
>     char field3[10];
> };
>
> and declare the variable
> struct astruct r;
>
> The amount of memory specified by the structure is not the sum of the
> storage specified by each of its member types. This vary from one
> machine and C compiler to another. On most compuers, objects of certain
> types may not begin anywhere in memory but are constrained to start at
> certain boundaries. For example, an integer of length 4 bytes may have
> to start at an address divisible by 4, and a real number of length 8 may
> have to start at an address divisible by 8. Thus, in my example, if the
> starting address of r is 200, the integer occupies bytes 200 through
> 203, but the real number cannot start at byte 204, since that location
> is not divisible by 8. Thus the real number must start at byte 208.
> The C compiler associates to each member identifier of a structure an
> offset that specifies how far beyond the start of the structure the
> location of that field is. To calculate the location of a member in a
> structure, the offset of the member identifier is added to the base
> address of the structure variable.
> Now the question.
> I want write a function that receives as parameters a start address and
> a "description of a structure" and returns the offsets of the fields.
> The structure is not known at compile time. The offsets should be
> calculated at runtime and the code should be independent of the machine.
> How should be written this function?


maybe typeof() can help you, and use __attribute__((packed)) if you
want the real size of structure, this is an alignament thingie.

-- 
noone is alone.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: offsets of fields in a structure
  2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
  2007-10-19 16:30 ` ninjaboy
@ 2007-10-19 17:35 ` LaPoint, Adam W
  2007-10-19 18:07   ` LaPoint, Adam W
  2007-10-19 18:11 ` Glynn Clements
  2007-10-19 19:50 ` Jan Smout
  3 siblings, 1 reply; 6+ messages in thread
From: LaPoint, Adam W @ 2007-10-19 17:35 UTC (permalink / raw)
  To: Giulio Rossato, linux-c-programming

Maybe something like this?

#include <stdio.h>

struct astruct {
  int field1;
  float field2;
  char field3[10];
};

int main(void)
{
  struct astruct r, *s;
  int h,i,j,k;

  h = (long)s;
  i = (long)&(s->field1);
  j = (long)&(s->field2);
  k = (long)&(s->field3);

  printf("offset field1: %d\n", i-h);
  printf("offset field2: %d\n", j-h);
  printf("offset field3: %d\n", k-h);
  return(0);
}

You may have to change the casting based on your architecture.

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of Giulio
Rossato
Sent: Friday, October 19, 2007 11:28 AM
To: linux-c-programming@vger.kernel.org
Subject: offsets of fields in a structure


Suppose that I define the following structure
struct astruct {
    int field1;
    float field2;
    char field3[10];
};

and declare the variable
struct astruct r;

The amount of memory specified by the structure is not the sum of the 
storage specified by each of its member types. This vary from one 
machine and C compiler to another. On most compuers, objects of certain 
types may not begin anywhere in memory but are constrained to start at 
certain boundaries. For example, an integer of length 4 bytes may have 
to start at an address divisible by 4, and a real number of length 8 may 
have to start at an address divisible by 8. Thus, in my example, if the 
starting address of r is 200, the integer occupies bytes 200 through 
203, but the real number cannot start at byte 204, since that location 
is not divisible by 8. Thus the real number must start at byte 208.
The C compiler associates to each member identifier of a structure an 
offset that specifies how far beyond the start of the structure the 
location of that field is. To calculate the location of a member in a 
structure, the offset of the member identifier is added to the base 
address of the structure variable.
Now the question.
I want write a function that receives as parameters a start address and 
a "description of a structure" and returns the offsets of the fields. 
The structure is not known at compile time. The offsets should be 
calculated at runtime and the code should be independent of the machine.
How should be written this function?


-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: offsets of fields in a structure
  2007-10-19 17:35 ` LaPoint, Adam W
@ 2007-10-19 18:07   ` LaPoint, Adam W
  0 siblings, 0 replies; 6+ messages in thread
From: LaPoint, Adam W @ 2007-10-19 18:07 UTC (permalink / raw)
  To: LaPoint, Adam W, Giulio Rossato, linux-c-programming

Oops, make that:

#include <stdio.h>

struct astruct {
  int field1;
  float field2;
  char field3[10];
};

int main(void)
{
  struct astruct r;
  int h,i,j,k;

  h = (long)&r;
  i = (long)&(r.field1);
  j = (long)&(r.field2);
  k = (long)&(r.field3);

  printf("offset field1: %d\n", i-h);
  printf("offset field2: %d\n", j-h);
  printf("offset field3: %d\n", k-h);
  return(0);
}

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of LaPoint,
Adam W
Sent: Friday, October 19, 2007 1:35 PM
To: Giulio Rossato; linux-c-programming@vger.kernel.org
Subject: RE: offsets of fields in a structure


Maybe something like this?

#include <stdio.h>

struct astruct {
  int field1;
  float field2;
  char field3[10];
};

int main(void)
{
  struct astruct r, *s;
  int h,i,j,k;

  h = (long)s;
  i = (long)&(s->field1);
  j = (long)&(s->field2);
  k = (long)&(s->field3);

  printf("offset field1: %d\n", i-h);
  printf("offset field2: %d\n", j-h);
  printf("offset field3: %d\n", k-h);
  return(0);
}

You may have to change the casting based on your architecture.

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of Giulio
Rossato
Sent: Friday, October 19, 2007 11:28 AM
To: linux-c-programming@vger.kernel.org
Subject: offsets of fields in a structure


Suppose that I define the following structure
struct astruct {
    int field1;
    float field2;
    char field3[10];
};

and declare the variable
struct astruct r;

The amount of memory specified by the structure is not the sum of the 
storage specified by each of its member types. This vary from one 
machine and C compiler to another. On most compuers, objects of certain 
types may not begin anywhere in memory but are constrained to start at 
certain boundaries. For example, an integer of length 4 bytes may have 
to start at an address divisible by 4, and a real number of length 8 may 
have to start at an address divisible by 8. Thus, in my example, if the 
starting address of r is 200, the integer occupies bytes 200 through 
203, but the real number cannot start at byte 204, since that location 
is not divisible by 8. Thus the real number must start at byte 208.
The C compiler associates to each member identifier of a structure an 
offset that specifies how far beyond the start of the structure the 
location of that field is. To calculate the location of a member in a 
structure, the offset of the member identifier is added to the base 
address of the structure variable.
Now the question.
I want write a function that receives as parameters a start address and 
a "description of a structure" and returns the offsets of the fields. 
The structure is not known at compile time. The offsets should be 
calculated at runtime and the code should be independent of the machine.
How should be written this function?


-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: offsets of fields in a structure
  2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
  2007-10-19 16:30 ` ninjaboy
  2007-10-19 17:35 ` LaPoint, Adam W
@ 2007-10-19 18:11 ` Glynn Clements
  2007-10-19 19:50 ` Jan Smout
  3 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2007-10-19 18:11 UTC (permalink / raw)
  To: Giulio Rossato; +Cc: linux-c-programming


Giulio Rossato wrote:

> I want write a function that receives as parameters a start address and 
> a "description of a structure" and returns the offsets of the fields. 
> The structure is not known at compile time. The offsets should be 
> calculated at runtime and the code should be independent of the machine.
> How should be written this function?

Your code will need to express the rules which the compiler uses to
lay out structures; there is no mechanism to obtain this information
from the compiler.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: offsets of fields in a structure
  2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
                   ` (2 preceding siblings ...)
  2007-10-19 18:11 ` Glynn Clements
@ 2007-10-19 19:50 ` Jan Smout
  3 siblings, 0 replies; 6+ messages in thread
From: Jan Smout @ 2007-10-19 19:50 UTC (permalink / raw)
  To: Giulio Rossato; +Cc: linux-c-programming

On 19/10/2007, Giulio Rossato <giulio.rossato@infocamere.it> wrote:
> I want write a function that receives as parameters a start address and
> a "description of a structure" and returns the offsets of the fields.
> The structure is not known at compile time. The offsets should be
> calculated at runtime and the code should be independent of the machine.
> How should be written this function?

Have a look at the Linux manpage for the "offsetof(type,
member)"-macro from stddef.h. It will probably not be exactly what
you're looking for, but might be a starting point which gets you
going.

Jan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-10-19 19:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
2007-10-19 16:30 ` ninjaboy
2007-10-19 17:35 ` LaPoint, Adam W
2007-10-19 18:07   ` LaPoint, Adam W
2007-10-19 18:11 ` Glynn Clements
2007-10-19 19:50 ` Jan Smout

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).