Re: large files: when ubiquitous?
From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 05/08/04
- Next message: P.T. Breuer: "Re: large files: when ubiquitous?"
- Previous message: P.T. Breuer: "Re: large files: when ubiquitous?"
- In reply to: P.T. Breuer: "Re: large files: when ubiquitous?"
- Next in thread: P.T. Breuer: "Re: large files: when ubiquitous?"
- Reply: P.T. Breuer: "Re: large files: when ubiquitous?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 08 May 2004 16:23:54 +0200
"P.T. Breuer" wrote:
>
> It says reading anything from a char* is always OK (as far as I can
> make out), but I would disagree - surely a char* can have any
> alignment? Not necessarily one that matches the alignment of the
> type you want to read.
The discussion is not about alignment at all. It is about
optimizations performed by the compiler. If you have a
pointer to a float, and another pointer to a long, the
compiler may assume chaning the one would not change the
other.
Eg. this program print 1078523331 when compiled without
optimization, and print 42 when compiled with -O3
#include <stdio.h>
#include <stdlib.h>
long f(long *a, float *b)
{
*a=42;
*b=3.14;
return *a;
}
int main()
{
void *p=malloc(42);
printf("%ld\n",f(p,p));
return 0;
}
> Wouldn't reading "anything" from a void* be safer?
Dereferencing a void shouldn't be allowed at all.
The reason a char pointer is not assumed to point at
different address than any other pointer is, that you
can do stuff like reading, writing, copying data of
any type by using a char pointer.
> It's certainly common in the kernel to pass foo(void *data)
> and then at the receiving end read mystruct *x = data;
But by the time you derefernce the pointer it is no
longer a void pointer, so the discussion about type
punning doesn't apply at all. Actually that variable
might as well have been passed as a char pointer. It
would be ugly, but it would work exactly the same.
-- Kasper Dupont -- der bruger for meget tid paa usenet. For sending spam use abuse@mk.lir.dk and kasperd@mk.lir.dk I'd rather be a hammer than a nail.
- Next message: P.T. Breuer: "Re: large files: when ubiquitous?"
- Previous message: P.T. Breuer: "Re: large files: when ubiquitous?"
- In reply to: P.T. Breuer: "Re: large files: when ubiquitous?"
- Next in thread: P.T. Breuer: "Re: large files: when ubiquitous?"
- Reply: P.T. Breuer: "Re: large files: when ubiquitous?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|