[Gllug] Pointer arithmetic with void *
Alain Williams
addw at phcomp.co.uk
Thu Aug 15 19:54:35 UTC 2002
On Thu, Aug 15, 2002 at 06:17:17PM +0100, Tethys wrote:
>
> >> what does the above
> >> give you over just reading the appropriate amount of data directly
> >> from the mmap()ed region in C (either via a direct cast to a suitable
> >> data type, or via a memcpy)?
> >
> >Bloat?
>
> Which is exactly why I was asking. If there are any benefits in using
> it, then I'd like to know. Otherwise, I'll avoid the bloat, and stick
> with C...
Take care with something like:
void* p = mmap(....)
struct foo* fp;
int i;
p = (void*)((char*)p + some_number);
fp = (struct foo*)p;
i = fp->some_member;
The point is that types (like int) usually need certain alignments (eg must
be a multiple of 4). If some_number is not a multiple of 4, you could get a
bus error. To avoid this you should go:
struct foo f;
....
memcpy((void*)&f, p, sizeof(f));
i = f.some_member;
If you work on Intel then you won't in fact get a bus error, but it takes more
memory cycles to load non aligned data. Some chips are not so forgiving - don't
write CPU specific code, you never know when your code gets being run on something
else.
--
Alain Williams
#include <std_disclaimer.h>
--
Gllug mailing list - Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug
More information about the GLLUG
mailing list