variable sized structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramkrishna1
    New Member
    • Apr 2007
    • 2

    #1

    variable sized structure

    Hi All,

    Consider the following piece of code:

    struct foo1
    {
    unsigned char tag;
    unsigned char length;
    unsigned char value[];
    };

    struct foo2
    {
    unsigned char tag;
    unsigned char length;
    unsigned char value[];
    };

    struct foo3
    {
    unsigned char tag;
    unsigned char length;
    unsigned char value[];
    };

    struct foo4
    {
    unsigned char tag;
    unsigned char length;
    unsigned char value[];
    };


    struct abc
    {

    struct foo1 foo_1;
    struct foo2 foo_2;
    struct foo3 foo_3;
    struct foo4 foo_4;

    };

    In 'C' struct foo1,foo2,foo3, foo4 cannot be members of struct abc as they declared with empty index. Is there any another method which solves the above scnerio to dynamically fill data into arrays in the structures.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You could change those unsigned char values[] arrays to simple unsigned char* values
    pointers and add a variable size array at the end of your abc struct. You do need
    to initialize your pointers in the modified abc struct:
    Code:
    struct abc* p= malloc( ... );
    p->foo_1.value= p->data;
    p->foo_2.value= foo_1.value+foo_1.length;
    p->foo_3.value= foo_2.value+foo_2.length;
    p->foo_4.value= foo_3.value+foo_3,length;
    I personally find it a bit 'mechanical' but that's what you get with those variable
    length structs ;-)

    kind regards,

    Jos

    Comment

    • ramkrishna1
      New Member
      • Apr 2007
      • 2

      #3
      hi Jos,
      Thanks for ur reply.

      we cannot use malloc() for our processor(AVR Series from atmel).is there any alternate way to allocate memory dynamically?.

      Best Regards
      Ramkrishna

      Originally posted by JosAH
      You could change those unsigned char values[] arrays to simple unsigned char* values
      pointers and add a variable size array at the end of your abc struct. You do need
      to initialize your pointers in the modified abc struct:
      Code:
      struct abc* p= malloc( ... );
      p->foo_1.value= p->data;
      p->foo_2.value= foo_1.value+foo_1.length;
      p->foo_3.value= foo_2.value+foo_2.length;
      p->foo_4.value= foo_3.value+foo_3,length;
      I personally find it a bit 'mechanical' but that's what you get with those variable
      length structs ;-)

      kind regards,

      Jos

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by ramkrishna1
        hi Jos,
        Thanks for ur reply.

        we cannot use malloc() for our processor(AVR Series from atmel).is there any alternate way to allocate memory dynamically?.

        Best Regards
        Ramkrishna
        If you use those little Atmels (an Atmega?) you are on your own, i.e. you can
        make that pointer p (see my previous reply) point to anything you like. You
        have to manage your own memory.

        kind regards,

        Jos

        Comment

        Working...