Re: How does C cope if architecture doesn't address bytes?

From: Dave Vandervies (dj3vande_at_csclub.uwaterloo.ca)
Date: 11/18/04

  • Next message: Richard Bos: "Re: Stuck with a strange core (C code) -- Please help."
    Date: Thu, 18 Nov 2004 08:28:06 +0000 (UTC)
    
    

    In article <419c5588.229587426@news.individual.net>,
    Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:

    >Well, the implementation is allowed to use whatever magic is available
    >to it. For example, TTBOMK it's legal for it to scan the statement in
    >which the malloc() call occurs, figure out the type of the pointer which
    >it is assigned to, and instead of writing CALL (malloc), @ACC, @PTR to
    >the object code, write CALL (mallocalign), @ACC, @SIZ, @PTR. However...

    To handle malloc alignment in the compiler rather than the library in
    the general case, the implementation magic would have to be more magical
    than that.

    Consider:
    --------
    void *debug_malloc(size_t size,char *file,int line)
    {
        void *ptr=malloc(size);
        if(global_params.debug_file)
            fprintf(global_params.debug_file,"Malloc %lu bytes at %s:%d: Got %p\n",(unsigned long)size,file,line,ptr);
        return ptr;
    }
    --------
    The alignment this call to malloc needs won't be known until run time,
    and the compiler won't know when it's compiling code that calls this
    function that it needs to know the alignment requirement.

    I believe it's also legal to do something like this:
    --------
    int i;
    short *s;
    double *d;

    s=malloc(sizeof *s * sizeof *d);
    for(i=0;i<sizeof *d;i++)
        s[i]=42*i;

    /*Is direct conversion short * -> double * allowed to do The Wrong Thing?
      I suspect it is, but forcing the conversion to go through void * is,
        if I'm not mistaken, required to get it right.
    */
    d=(void *)s;
    for(i=0;i<sizeof *s;i++)
        d[i]=42.0/i;
    --------
    so at the call to malloc the compiler would need to know the alignments
    of all the types the pointer might be used to point at.

    dave

    -- 
    /* Dave Vandervies */int main(void)/* dj3vande@csclub.uwaterloo.ca */
    {char *foo[]={" C ","t ","er","\n","a","J","us","h","not","ck"};
    int bar[]={5,6,1,4,8,7,2,0,7,4,9,2,3},i,printf(const char *,...);
    for(i=0;i<13;i++) printf(foo[bar[i]]);return 0;}
    

  • Next message: Richard Bos: "Re: Stuck with a strange core (C code) -- Please help."