malloc and casts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I have been searching for some info for some time, but since i could not find any reasonable answers and hence my post.

My situation is like this.
I have my structs like this


struct loop_x {
   uint16_t loop_x_elem_1: 12;
   uint16_t loop_x_elem_2: 4;
   uint8_t loop_x_elem_3: 1;
   uint8_t loop_x_elem_4: 2;
   uint8_t loop_x_elem_5: 4;
   uint8_t loop_x_elem_6: 1;
};

struct loop_y {
   uint8_t loop_y_elem_1;
   uint8_t loop_y_elem_2;
   struct loop_x *loop_x_elem_2;
};

struct abc {
   uint16_t block_length;
   uint8_t elem_1: 2;
   uint8_t elem_2: 5;
   uint8_t elem_3: 1;

   struct loop_x *loop_x_elem_1;
   struct loop_y *loop_y_elem_2;
};


Here block_length refers to the entire length of the block in bytes.

So in my application i can do a

storage = malloc(sizeof(uint8_t) * block_length);

which will allocate all my storage needs.
Now, i am trying to parse a stream and store the parsed details into the structs.

So would i be right in the case that i cast the allocated memory to the required struct to do the storage ?
ie,

for parsing the the first part, ie struct abc,
i would cast memory to struct abc and parse struct abc,

I now know how many bytes i have parsed, to parse struct loop_x.
i would cast memory from the offset of struct abc (since i know how many bytes i already parsed) to struct loop_x and a similar way for struct loop_y too.

Would it be correct, if i do it in this fashion ? The reason for my question is to avoid multiple malloc() calls, as malloc() would be quite expensive on the CPU cache (especially on small/embedded systems) and hence my question.

Any suggestions would be appreciated.


Manu


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux