C question - headers in mmaped files
Martin Pool
mbp at samba.org
Wed Apr 2 11:46:35 EST 2003
On 2 Apr 2003, Michael Still <mikal at stillhq.com> wrote:
> In addition to what everyone else said, lots of people solve this sort of
> problem with a struct and a union. Checkout this code which probably wont
> compile (pine needs a "compile my example" command):
>
> typedef struct{
> char magic[4];
> int version;
> float gronk;
> } dattype;
>
> typedef union{
> dattype data;
> char * raw;
> } jpfile;
>
> jpfile foo;
> foo->raw = mmap(...);
>
> foo.data.magic[0] = 'J';
> foo.data.magic[1] = 'P';
> foo.data.magic[2] = 'F';
> foo.data.magic[3] = 'L';
>
> ...etc...
Segmentation fault (core dumped)
I think a plain cast would be safer and nicer. It would at least
prevent you writing 'JPFL' into a pointer. And why introduce a new
datatype if you're only going to use it in place of a cast?
--
Martin
More information about the linux
mailing list