public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: malloc and casts
@ 2005-08-17 13:08 Andrew Hardy
  2005-08-17 14:25 ` Manu Abraham
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Hardy @ 2005-08-17 13:08 UTC (permalink / raw)
  To: Manu Abraham <abraham.manu; +Cc: gcc-help


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






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

* Re: malloc and casts
  2005-08-17 13:08 malloc and casts Andrew Hardy
@ 2005-08-17 14:25 ` Manu Abraham
  0 siblings, 0 replies; 3+ messages in thread
From: Manu Abraham @ 2005-08-17 14:25 UTC (permalink / raw)
  To: Andrew Hardy; +Cc: gcc-help

Andrew Hardy wrote:

>I don't really undrstand what you're trying to achieve, but it looks a bit
>  
>

I am trying to parse a bitstream, with minimal CPU usage, ie without 
using too many a malloc().

>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.
>
>  
>

I think this was the problem i have been facing..

>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.
>
>  
>

Hmm, You mean to say, use the structs without the pointers ?

Thanks,
Manu

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

* malloc and casts
@ 2005-08-17 10:34 Manu Abraham
  0 siblings, 0 replies; 3+ messages in thread
From: Manu Abraham @ 2005-08-17 10:34 UTC (permalink / raw)
  To: gcc-help

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

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

end of thread, other threads:[~2005-08-17 14:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-17 13:08 malloc and casts Andrew Hardy
2005-08-17 14:25 ` Manu Abraham
  -- strict thread matches above, loose matches on Subject: below --
2005-08-17 10:34 Manu Abraham

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 read-only IMAP folder(s) and NNTP newsgroup(s).