Re: large files: when ubiquitous?

From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 05/08/04


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.


Relevant Pages

  • Re: A malloc question, part 2
    ... Using a pointer of type *X to access an object of ... The return value from mallocis guaranteed to be suitable for alignment ... The compiler would be very justified when generating code ... so misalignment doesn't cost ...
    (comp.lang.c)
  • Re: How does C cope if architecture doesnt address bytes?
    ... >>which the malloc() call occurs, figure out the type of the pointer which ... alignment in the implementation, rather than in either part of it. ... compiler and library are so strictly separate that there is no ... # The pointer returned if the allocation succeeds is suitably aligned so ...
    (comp.lang.c)
  • Re: TEA Implementation
    ... The second argument should be a pointer to pointer to ... It should be the address of a pointer to char. ... compiler won't even accept it at all. ... Seems like a rather confusing way of writing ...
    (comp.lang.c)
  • Re: Isnt it time there was a standard align statement?
    ... C language actually allowed some portable control over data ... /* per member alignment (obviously padding before the first ... fatal with compiler specific switches - and that's no business of the ...
    (comp.lang.c)
  • Re: large files: when ubiquitous?
    ... the pointer might be into shared ... compiler will make optimizations giving unexpected ... same with and without optimization, ... to a char pointer type or changing p to be a char ...
    (comp.os.linux.development.system)