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

From: Dave Vandervies (
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 <>,
    Richard Bos <> 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.

    void *debug_malloc(size_t size,char *file,int line)
        void *ptr=malloc(size);
            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++)

    /*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++)
    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 Vandervies */int main(void)/* */
    {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."

    Relevant Pages

    • 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 ...
    • Re: Question regarding malloc casing
      ... malloc because it is bug hider. ... still I am using malloc so compiler will throw warring ... If you ignore the bug, ... The compiler trusts you the programmer. ...
    • Re: Newbie question about malloc
      ... some minimum number of bytes at a time via malloc(). ... for which the C compiler compiles machine-level code.") Computer ... simulates some other machine that only has 16-bit pointers. ... the emulator, and can optionally run some code "native" intermixed ...
    • Re: gcc knows about malloc()
      ... While omitting the system header file is a mistake, ... malloc() function incorrectly, but by failing to cast it at all! ... It is a common mistake to think that casting the int to a pointer ... so you found a compiler bug - please report it to gcc via their usual ...
    • Re: sprintf leading up to bus error (signal 10)
      ... I don't know what exactly you're doing but this pointer cast makes me suspect that this is the case. ... value returned by malloc to the pointer you want to point to it. ... you are effectively telling the compiler you want space for N of the things ptr points to. ...