malloc and casts

Andrew Hardy Andrew.Hardy@marconi.com
Wed Aug 17 13:08:00 GMT 2005


I don't really undrstand what you're trying to achieve, but it looks a bit
suspicious that you have pointers as part of your structs, this may give
issues when you cast if you think you have structs wholely within structs.

Would something like this help??

char *ptr;

// where storage contains what you want in enough space to coopy it out
into your structs.
ptr = storage;

memcpy(somestruct, ptr, sizeof(somestruct));
ptr += sizeof(somestruct);

memcpy(somestruct, ptr, sizeof(somestruct));
ptr += sizeof(somestruct);

memcpy(somestruct, ptr, sizeof(somestruct));
ptr += sizeof(somestruct);

etc...

where your structures don't include pointers and where somestruct is the
current of a number structures or if you require current of a number of
substructures that you require to have in one struct.

I may be completely off from what you are trying to do, and if so then
please ignor,

Andrew H.





|---------+---------------------------->
|         |           Manu Abraham     |
|         |           <abraham.manu@gma|
|         |           il.com>          |
|         |           Sent by:         |
|         |           gcc-help-owner@gc|
|         |           c.gnu.org        |
|         |                            |
|         |                            |
|         |           17/08/2005 11:24 |
|         |                            |
|---------+---------------------------->
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |                                                                                                                                                  |
  |       To:       gcc-help@gcc.gnu.org                                                                                                             |
  |       cc:                                                                                                                                        |
  |       Subject:  malloc and casts                                                                                                                 |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|




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








More information about the Gcc-help mailing list