struct - C: Dynamically synthesizing aggregate data types at runtime -


disclaimer: question general concept. i've "dumbed down" question can ask here - without needing provide entire context of it's actual application. can foresee bunch of comments "why ever that?" but, if question taken @ face value, i'd appreciate it!

suppose wanted dynamically synthesize data structures in c @ runtime out of pre-defined structs.

the best way know how ask question through code sample.

in following, we've defined 2 structs: foo , bar. define struct foobar illustrate @ least 1 difference between compile-time generated composite type , runtime generated "dynamically synthesized" type.

#include <stdlib.h> #include <stdio.h>  typedef struct foo {     char junk1;     char junk2; } foo;  typedef struct bar {     int junk3;     int junk4; } bar;  typedef struct foobar {     foo foo;     bar bar; } foobar;  int main() {     printf("sizes: %li, %li, %li\n", sizeof(foo), sizeof(bar), sizeof(foobar));     // prints: sizes: 2, 8, 12     // because foo aligned on 1-byte boundaries , has total size of 2 bytes.     // bar aligned on 4-byte boundaries , has total size of 8 bytes.     // foobar aligned on 4-byte boundaries due ints in foo. therefore,     // compiler added 2-bytes of padding after foo member.      // following "works", allocates 10 bytes, ,     // "bar" members "misaligned":     void * syntheticfoobar = malloc(sizeof(foo) + sizeof(bar));     ((foo*)syntheticfoobar)->junk1 = 1;     ((foo*)syntheticfoobar)->junk2 = 2;     ((bar*)(syntheticfoobar + sizeof(foo)))->junk3 = 3;     ((bar*)(syntheticfoobar + sizeof(foo)))->junk4 = 4;      free(syntheticfoobar);     return 0; } 

so questions be:

1.) how badly lack of proper data alignment affect performance? given overhead involved accessing "members" of synthetic structures, data-alignment significant contributing factor?

2.) there better way of doing given constraints of run-time synthesis?

1.) how badly lack of proper data alignment affect performance? given overhead involved accessing "members" of synthetic structures, data-alignment significant contributing factor?

this entirely dependent on cpu architecture , compiler. on systems may have performance penalty, on others crash.

2.) there better way of doing given constraints of run-time synthesis?

here's example of how might create aligned synthesized struct @ runtime:

#include <stdio.h> #include <stdlib.h>  typedef struct {   char junk1;   char junk2; } a;  typedef struct {   int junk3;   int junk4; } b;  typedef struct {   double junk5;   char junk6; } c;  static size_t roundup(size_t value,size_t factor) {   return value+factor-1-((value+factor-1)%factor); }  #define alignof(type) (sizeof(struct {type a;char b;})-sizeof(type))  int main(int argc,char **argv) {   size_t offsets[3];   size_t pos = 0;    pos = roundup(pos,alignof(a));   offsets[0] = pos;   pos += sizeof(a);    pos = roundup(pos,alignof(b));   offsets[1] = pos;   pos += sizeof(b);    pos = roundup(pos,alignof(c));   offsets[2] = pos;   pos += sizeof(c);    {     char *foobar = malloc(pos);     *a = (a *)(foobar + offsets[0]);     b *b = (b *)(foobar + offsets[1]);     c *c = (c *)(foobar + offsets[2]);     a->junk1 = 1;     a->junk2 = 2;     b->junk3 = 3;     b->junk4 = 4;     c->junk5 = 5;     c->junk6 = 6;     free(foobar);   }   return 0; } 

the alignment of particular struct determined creating struct original struct , char. compiler automatically add enough padding make sure necessary alignment preserved if use array of these structs, measuring difference in sizes, proper alignment.

using alignment information, can create table of each member of synthesized struct should live relative beginning. have make sure position of each member has proper alignment rounding position nearest multiple of alignment.

you should able generalize number of members.

note if wanted determine overall size if synthesized struct (what sizeof() might return), create array of these synthesized structs, need combined alignment requirement , roundup final pos factor. combined alignment least common multiple of individual alignments.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -